hi,
--the string @s-2 has comma seprated text , some times it is seprated by semicolon also.(so i need to put or in patindex)
-- i have to find out first text "R1" to do that i need to find out the index of first occurence of comma or semicolan so that i can use substring to get the "R1" ( primary task is to get the "R1")
Qt1) so please tell me how to put "or" in patindex. or any other good method would be helpfull
following is what i was trying. i have also tried to get comma's index. then used the input to get if there is any smicolon. but it needs patindex to be used twoice.
DECLARE @s-2 VARCHAR(MAX)= NULL
SELECT PATINDEX('%[^A-Za-z0-9-())![]]%','sd(())[-s!;dfd')
Q2) how to escape [] in side [] in patindex.
December 21, 2021 at 7:12 am
i have rephrased the question
DECLARE @s-2 VARCHAR(MAX)= 'r1,r2'
SELECT PATINDEX('%[^A-Za-z0-9-())![]]%',@s)
DECLARE @S1 VARCHAR(MAX)= 'X1;r2'
SELECT PATINDEX('%[^A-Za-z0-9-())![]]%',@s1)
i need to get first text string before first comma or semicolan ex "r1" and "x1" respectivily . which is seprated by comma or simicolon .
i have rephrased the question
DECLARE @s-2 VARCHAR(MAX)= 'r1,r2' SELECT PATINDEX('%[^A-Za-z0-9-())![]]%',@s)
DECLARE @S1 VARCHAR(MAX)= 'X1;r2' SELECT PATINDEX('%[^A-Za-z0-9-())![]]%',@s1)
i need to get first text string before first comma or semicolan ex "r1" and "x1" respectivily . which is seprated by comma or simicolon .
DECLARE @S VARCHAR(MAX)= 'r1,r2';
DECLARE @S1 VARCHAR(MAX)= 'X1;r2';
DECLARE @S2 VARCHAR(MAX)= 'A5'; -- WHAT sould happen here - Update the CASE statement as needed
WITH cteData AS (
SELECT MyString = @S
UNION ALL
SELECT MyString = @S1
UNION ALL
SELECT MyString = @S2
)
SELECT cte.MyString
, ExtractedString = CASE WHEN ISNULL(cs.iPos, 0) > 0
THEN LEFT(cte.MyString, cs.iPos -1)
ELSE NULL
END
FROM cteData AS cte
OUTER APPLY (SELECT PATINDEX('%[,;]%', cte.MyString)) AS cs(iPos);
December 22, 2021 at 2:37 am
A minor optimization on DesNorton's good code would be to remove the ISNULL from the CASE WHEN because a NULL can't be greater than 0 (nor equal to or less than or any other type of comparison).
The ELSE NULL can be removed, as well, but it makes it real obvious what happens and doesn't cost any extra clock cycles.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply