Created
July 27, 2012 23:11
-
-
Save wellercs/3190970 to your computer and use it in GitHub Desktop.
t-sql get first item from list using substring and charindex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DECLARE @InvalidList varchar(50) | |
DECLARE @ValidList varchar(50) | |
SET @InvalidList = 'Item1-Item2-Item3' | |
SET @ValidList = 'Item1 > Item2 > Item3' | |
SELECT | |
CASE | |
WHEN CHARINDEX('>',@InvalidList) = 0 THEN @InvalidList | |
ELSE LTRIM(RTRIM(REPLACE(SUBSTRING(@InvalidList,1,CHARINDEX('>',@InvalidList)),'>',''))) | |
END | |
SELECT | |
CASE | |
WHEN CHARINDEX('>',@ValidList) = 0 THEN @ValidList | |
ELSE LTRIM(RTRIM(REPLACE(SUBSTRING(@ValidList,1,CHARINDEX('>',@ValidList)),'>',''))) | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment