Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
wleonard
Starting Member
30 Posts |
Posted - 2013-12-23 : 18:07:00
|
Hello everyone. I'm trying to form a query that will select part of a result.To get an idea, I'm trying to take out the ending of results that end in '-PR'.Example:The original result is 'Jbbx32-PR'. I want to select it as 'Jbbx32'.Will Leonard |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-12-24 : 00:05:25
|
declare @col varchar(100) = 'Jbbx32-PR'SELECT SUBSTRING( @col, 1, CHARINDEX('-', @col)-1) -- extracts value before the '-' symbol--Chandu |
 |
|
wleonard
Starting Member
30 Posts |
Posted - 2013-12-24 : 12:47:02
|
quote: Originally posted by bandi declare @col varchar(100) = 'Jbbx32-PR'SELECT SUBSTRING( @col, 1, CHARINDEX('-', @col)-1) -- extracts value before the '-' symbol--Chandu
This did not work.Will Leonard |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2013-12-24 : 12:53:47
|
quote: Originally posted by wleonard
quote: Originally posted by bandi declare @col varchar(100) = 'Jbbx32-PR'SELECT SUBSTRING( @col, 1, CHARINDEX('-', @col)-1) -- extracts value before the '-' symbol--Chandu
This did not work.Will Leonard
How did it not work?Did you get an error? The wrong result? |
 |
|
wleonard
Starting Member
30 Posts |
Posted - 2013-12-24 : 13:08:11
|
quote: Originally posted by Lamprey
quote: Originally posted by wleonard
quote: Originally posted by bandi declare @col varchar(100) = 'Jbbx32-PR'SELECT SUBSTRING( @col, 1, CHARINDEX('-', @col)-1) -- extracts value before the '-' symbol--Chandu
This did not work.Will Leonard
How did it not work?Did you get an error? The wrong result?
declare @col varchar(100) = (SELECT sut_ORIGINAL_ITEM_NUMBER FROM tblSubstitution WHERE sut_ORIGINAL_ITEM_NUMBER LIKE '%-PR%')SELECT SUBSTRING( @col, 1, CHARINDEX('%-PR%', @col)-1)I want all results that end in '-PR'. What I want to do is take those results and remove the '-PR' at the end. The above code has errors. I am still trying to figure this out.Will Leonard |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-12-24 : 13:32:41
|
I want all results that end in '-PR'. What I want to do is take those results and remove the '-PR' at the end.then this is enoughSELECT LEFT(column,LEN(column)-3)FROM TableWHERE column LIKE '%-PR' ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
|
|
|
|
|