Solved

T-SQL expression - removing sting after spefified charactor

Posted on 2010-11-17
15
395 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +2
15 Comments
 
LVL 19

Expert Comment

by:MINDSUPERB
ID: 34162045
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
ID: 34162064
Hi review below ex.

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

0
 
LVL 19

Expert Comment

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

Left(YourField,Len(YourField)-2)

Ed
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 16

Expert Comment

by:EvilPostIt
ID: 34162173
This should do the trick.

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

Open in new window

0
 
LVL 16

Expert Comment

by:EvilPostIt
ID: 34162193
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
ID: 34168970
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
 
LVL 19

Expert Comment

by:MINDSUPERB
ID: 34170584
Try this

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

Ed

0
 
LVL 19

Expert Comment

by:MINDSUPERB
ID: 34170798
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
ID: 34170988
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
ID: 34183912
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 69

Assisted Solution

by:Qlemo
Qlemo earned 250 total points
ID: 34354752
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
ID: 34624686
I'm sorry that I didn't reply. I needed to work on something else...
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 34671253
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
point in time restore in SQL server 26 46
Searching a barcode number within a string. 7 27
SP result not being displayed 5 45
calculate days away 11 31
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

730 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