| Author |
Topic |
|
sqlteamForummer
Starting Member
13 Posts |
Posted - 2005-11-30 : 13:29:16
|
| I have to grab a substring from each item of the column: For exthe column has 3 items: I have to grab werwe.exe from the 1st, wrkew.pdf from the 2nd and 872.gif from the 3rd. How should I do it?Column1--------name\dexter.exe\city\234324name\simpson.pdf\city\23423name\qwe.gif\city\47384\45087name\2325.txt\city\cityThanx |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
sqlteamForummer
Starting Member
13 Posts |
Posted - 2005-11-30 : 13:41:30
|
| Oh Sorry,I have to grab a substring from each item of the column: For exthe column has 4 items: I have to grab dexter.exe from the 1st, simpson.pdf from the 2nd, qwe.gif from the 3rd and 2325.txt from the 4th. How should I do it?Column1--------name\dexter.exe\city\234324name\simpson.pdf\city\23423name\qwe.gif\city\47384\45087name\2325.txt\city\cityThanx |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-11-30 : 14:04:30
|
| [code]USE NorthwindGOSET NOCOUNT ONCREATE TABLE myTable99(Col1 varchar(255))GOINSERT INTO myTable99(Col1)SELECT 'name\dexter.exe\city\234324' UNION ALLSELECT 'name\simpson.pdf\city\23423' UNION ALLSELECT 'name\qwe.gif\city\47384\45087' UNION ALLSELECT 'name\2325.txt\city\city'GOSELECT SUBSTRING(Col1 , CHARINDEX('\',Col1)+1 , (CHARINDEX('\',Col1,CHARINDEX('\',Col1)+1)-1) - (CHARINDEX('\',Col1)+1)) FROM myTable99GOSET NOCOUNT OFFDROP TABLE myTable99GO[/code]Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2005-11-30 : 14:11:23
|
This will split the source data into its 4 columns:SELECT [COLUMN_1] = substring(Col1 + '\', 0 + 1, charindex('\', Col1 + '\', 0 + 1) - 0 - 1 ), [COLUMN_2] = substring(Col1 + '\', charindex('\', Col1 + '\') + 1, charindex('\', Col1 + '\', charindex('\', Col1 + '\') + 1) - charindex('\', Col1 + '\') - 1 ), [COLUMN_3] = substring(Col1 + '\', charindex('\', Col1 + '\', charindex('\', Col1 + '\') + 1) + 1, charindex('\', Col1 + '\', charindex('\', Col1 + '\', charindex('\', Col1 + '\') + 1) + 1) - charindex('\', Col1 + '\', charindex('\', Col1 + '\') + 1) - 1 ), [COLUMN_4] = substring(Col1 + '\', charindex('\', Col1 + '\', charindex('\', Col1 + '\', charindex('\', Col1 + '\') + 1) + 1) + 1, charindex('\', Col1 + '\', charindex('\', Col1 + '\', charindex('\', Col1 + '\', charindex('\', Col1 + '\') + 1) + 1) + 1) - charindex('\', Col1 + '\', charindex('\', Col1 + '\', charindex('\', Col1 + '\') + 1) + 1) - 1 )FROM myTable99Kristen |
 |
|
|
sqlteamForummer
Starting Member
13 Posts |
Posted - 2005-11-30 : 14:18:54
|
| Thanx much guys |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-11-30 : 15:04:34
|
quote: Originally posted by Kristen This will split the source data into its 4 columns:SELECT [COLUMN_1] = substring(Col1 + '\', 0 + 1, charindex('\', Col1 + '\', 0 + 1) - 0 - 1 ), [COLUMN_2] = substring(Col1 + '\', charindex('\', Col1 + '\') + 1, charindex('\', Col1 + '\', charindex('\', Col1 + '\') + 1) - charindex('\', Col1 + '\') - 1 ), [COLUMN_3] = substring(Col1 + '\', charindex('\', Col1 + '\', charindex('\', Col1 + '\') + 1) + 1, charindex('\', Col1 + '\', charindex('\', Col1 + '\', charindex('\', Col1 + '\') + 1) + 1) - charindex('\', Col1 + '\', charindex('\', Col1 + '\') + 1) - 1 ), [COLUMN_4] = substring(Col1 + '\', charindex('\', Col1 + '\', charindex('\', Col1 + '\', charindex('\', Col1 + '\') + 1) + 1) + 1, charindex('\', Col1 + '\', charindex('\', Col1 + '\', charindex('\', Col1 + '\', charindex('\', Col1 + '\') + 1) + 1) + 1) - charindex('\', Col1 + '\', charindex('\', Col1 + '\', charindex('\', Col1 + '\') + 1) + 1) - 1 )FROM myTable99Kristen
WTF? [COLUMN_1] = substring(Col1 + '\' -- the string , 0 + 1 -- the starting position , charindex('\', Col1 + '\', 0 + 1) - 0 - 1 ) -- the offsetOK, I get the method behind the madness...Place holders....Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
Kristen
Test
22859 Posts |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
|
|
|