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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SELECT part of a string in a column

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
Go to Top of Page

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
Go to Top of Page

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?
Go to Top of Page

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
Go to Top of Page

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 enough


SELECT LEFT(column,LEN(column)-3)
FROM Table
WHERE column LIKE '%-PR'


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -