Hi, I've never had to deal with strings before and I'm having a little trouble. Let's get this part out of the way:CREATE TABLE #Example (s_index INT, s_query nvarchar(255))INSERT INTO #ExampleSELECT 000001, 'folder=123'INSERT INTO #ExampleSELECT 000002, 'folder=2222'INSERT INTO #ExampleSELECT 000003, 'folder=34343&other_stuff'INSERT INTO #ExampleSELECT 000004, 'folder=202-other_stuff'
I'm only concerned with the s_query field; I need to pull the folder number only and discard anything that follows so that I get:s_index folder_id------------------000001 123000002 2222000003 34343000004 202
Here's what I did initially:SELECT e.s_query , [folder_id_flawed] = Substring(e.s_query , Charindex('folder=', e.s_query) + 7 --Start after 'folder=' , CASE WHEN Charindex('&', Substring(e.s_query --When there is a '&' after the folder number , Charindex('folder=', e.s_query) + 8 , LEN(e.s_query)) ) > 0 THEN Charindex('&', Substring(e.s_query --Then stop before the '&' , Charindex('folder=', e.s_query) + 8 , LEN(e.s_query)) ) ELSE LEN(e.s_query) - 7 --Otherwise go until the end of the string END )FROM #Example eWHERE 1=1 AND ( (e.s_query LIKE 'folder=%') OR (e.s_query LIKE '%&folder=%') )And the result of that was:s_index folder_id_flawed------------------000001 123000002 2222000003 34343000004 202-other_stuff
Now, since my sample data set was so small, I thought the only character that could follow the folder number was '&'. Now I'm working with a larger (but still sample) set and noticed that '-' could follow it as well. Is there a way to check for any non-number character? I'd hate to write several nearly-identical WHEN statements.Thanks!