Solved

T-SQL expression - removing sting after spefified charactor

Posted on 2010-11-17
15
359 Views
Last Modified: 2012-05-10
Hello,
I have string value as follow.

50242/1

I would like to know the expression to remove after /
so string will be just

50242

However, I can't use string length for this because some time I will have
 
4433/1

4 digits instead of 5 digits.

Is there an expression to remove everything after specified letter? (in this case / )

I'm using SQL server 2008 express.


Thank you
0
Comment
Question by:jtuttle99
  • 4
  • 3
  • 3
  • +2
15 Comments
 
LVL 19

Expert Comment

by:MINDSUPERB
Comment Utility
If you there's only one character after /, then you can use the Rigth Function e.g. Right(Field,2).

See the link below:

http://msdn.microsoft.com/en-us/library/ms177532.aspx

Sincerely,
Ed
0
 
LVL 21

Expert Comment

by:Alpesh Patel
Comment Utility
Hi review below ex.

Select Left('123/5',CHARINDEX('/','123/5')-1)

0
 
LVL 19

Expert Comment

by:MINDSUPERB
Comment Utility
I am sorry. My fault. I misinterpreted the question. You may try this one instead:

Left(YourField,Len(YourField)-2)

Ed
0
 
LVL 16

Expert Comment

by:EvilPostIt
Comment Utility
This should do the trick.

SELECT REPLACE('12345/1','/','')

Open in new window

0
 
LVL 16

Expert Comment

by:EvilPostIt
Comment Utility
Sorry mis-read the question.

If i had read it correctly i would have done something much the same as PatelAlpesh.
0
 

Author Comment

by:jtuttle99
Comment Utility
I'm so sorry to tell you this but I can't use any length for this.
That's because sometimes I have string like

54165

so, if string includes /1, I want to get rid of the rest of it.

I.E.
string = convert to

54165/1    = 54165
54165/5    = 54165
54165       = 54165
5416         = 5416


Thank you

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 19

Expert Comment

by:MINDSUPERB
Comment Utility
Try this

LEFT(YourField, CHARINDEX('/', YourField) -1)

Ed

0
 
LVL 19

Expert Comment

by:MINDSUPERB
Comment Utility
Or you may use the IF Syntax as illustrated below:

IF CHARINDEX('/', YourField)>0
BEGIN
    SELECT LEFT(YourField, CHARINDEX('/', YourField) -1)
    FROM SomeTable
END

ELSE
BEGIN
    SELECT YourField    
FROM SomeOtherTable
END

Sincerely,
Ed
0
 
LVL 16

Accepted Solution

by:
EvilPostIt earned 250 total points
Comment Utility
This should do the trick.

SELECT CASE CHARINDEX('/',YourField)
WHEN 0 THEN YourField
ELSE LEFT(YourField,(CHARINDEX('/',YourField)-1)
END AS 'FieldName'
FROM YOURTABLE

Open in new window

0
 

Author Comment

by:jtuttle99
Comment Utility
Your Query worked in SELECT statement.
Is it possible to use it in Insert command? It's giving me error...


INSERT INTO mytable myfield Values CASE CHARINDEX('/', :job_number) WHEN 0 THEN :job_number ELSE LEFT(:job_number, CHARINDEX('/', :job_number) - 1) END AS job_number
0
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 250 total points
Comment Utility
Almost:
INSERT INTO mytable (myfield)
Values (CASE CHARINDEX('/', :job_number) WHEN 0 THEN :job_number ELSE LEFT(:job_number, CHARINDEX('/', :job_number) - 1) END)
0
 

Author Comment

by:jtuttle99
Comment Utility
I'm sorry that I didn't reply. I needed to work on something else...
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now