Solved

T-SQL expression - removing sting after spefified charactor

Posted on 2010-11-17
15
380 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
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.

813 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

18 Experts available now in Live!

Get 1:1 Help Now