问题
I am reposting my question as I am new to SQL 2012.
I want to fetch the numeric data between quotes (") in the following rows,
row1:'asdalknd,"1,2,3,4",slknsdl,"5,6,7,8",snlsn'
row2:'asknd,"111,267,387,4756",snsdl,"534,646,767,348",snlssdsdsdsjkvkn'
row3'....
row4'....
row5'....
row6'...
row7'...
row8'....
The above mentioned are the rows of a single column.
I just want to extract the numerics(may be in another column for each rows)
Can anybody pls help, as this is way above my basic knowledge of t-sql.
Thanks
回答1:
this is Ugly, but will eventually work:
COLUMN = 'jksjdksls#$#$@@kskjfjf,"123,456,789" lsnslkdswfnslsjfls'
left(
right(COLUMN,len(COLUMN)-instr(COLUMN,"""")),
instr(
right(COLUMN,len(COLUMN)-instr(COLUMN,"""")),
"""") -1
)
--> 123,456,789
This is what is done:
- We take this string
'jksjdksls#$#$@@kskjfjf,"123,456,789" lsnslkdswfnslsjfls'
- find the first occurence of " with
instr(COLUMN,"""")
--> returns 24 - take the right end of the string with. Therefore we need to take the length of the string with
len(COLUMN)
--> 55 and substract the position of the first " (24) - then we need to find the second " with
instr()
in the right string, which we need to create again withright(COLUMN,len(COLUMN)-instr(COLUMN,""""))
and substract 1 for the ".
来源:https://stackoverflow.com/questions/24704629/how-to-select-specific-data-between-quotes