February 19, 2010 at 6:46 am
I'm sure there's something simple for this, but I have 2 tables one has a column with text, a colon and more text and in my other table, it has everything in the first but only the text after the colon. Is there a command I can use to only return the characters after the colon in the first table? This is the first time I've had to do something like this so any help would be great.
Thanks,
Michael
February 19, 2010 at 6:50 am
Here's one way:
DECLARE @Text VARCHAR(100) = 'abc:xyz';
SELECT
REVERSE(
LEFT(
REVERSE(@Text),
CHARINDEX(':', REVERSE(@Text))-1));
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
February 19, 2010 at 6:55 am
I tried that with this command and receive a "Invalid length parameter passed to the SUBSTRING function."
SELECT
REVERSE(LEFT(REVERSE(PART_CODE), CHARINDEX(':', REVERSE(PART_CODE))-1))
FROM PRODUCT_MASTER
February 19, 2010 at 8:44 am
some of your part codes do not contain a colon then; add a WHERE statement:
WHERE CHARINDEX(':', PART_CODE) > 0
Lowell
February 19, 2010 at 10:10 pm
DECLARE @ProductMaster
TABLE (part_code VARCHAR(50) NOT NULL);
INSERT @ProductMaster VALUES ('S001:Sprocket');
INSERT @ProductMaster VALUES ('W562:Widget');
INSERT @ProductMaster VALUES ('Gearbox');
INSERT @ProductMaster VALUES ('B498:Banana');
INSERT @ProductMaster VALUES ('Q992:Petunia');
INSERT @ProductMaster VALUES ('Chain');
SELECT LEFT(part_code, ISNULL(NULLIF(Colon.position - 1, -1), DATALENGTH(part_code)))
FROM @ProductMaster
CROSS
APPLY (SELECT CHARINDEX(':', part_code COLLATE LATIN1_GENERAL_BIN)) Colon (position);
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
February 22, 2010 at 6:21 am
Paul, that returns everything left of the colon. He asked for everything right of it.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
February 22, 2010 at 7:10 am
GSquared (2/22/2010)
Paul, that returns everything left of the colon. He asked for everything right of it.
Ah, a small, but vital point - thanks.
Corrected version below.
DECLARE @ProductMaster
TABLE (part_code VARCHAR(50) NOT NULL);
INSERT @ProductMaster VALUES ('S001:Sprocket');
INSERT @ProductMaster VALUES ('W562:Widget');
INSERT @ProductMaster VALUES ('Gearbox');
INSERT @ProductMaster VALUES ('B498:Banana');
INSERT @ProductMaster VALUES ('Q992:Petunia');
INSERT @ProductMaster VALUES ('Chain');
SELECT RIGHT(part_code, DATALENGTH(part_code) - ISNULL(NULLIF(Colon.position, 0), DATALENGTH(part_code)))
FROM @ProductMaster
CROSS
APPLY (SELECT CHARINDEX(':', part_code COLLATE LATIN1_GENERAL_BIN)) Colon (position);
Paul
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply