sql server - How to split string using delimiter char using T-SQL? -
i have long string in 1 of columns of table. want specific information:- table structure:-
col1 = '123' col2 = 'aaaaa' col3 = 'clent id = 4356hy|client name = b b bob|client phone = 667-444-2626|client fax = 666-666-0151|info = inf8888877 -mac333330554/444400800'
my select statement is:-
select col1, col2, col3 table01
but in col3 need 'client name's value 'b b bob'.
in col3 -
column delimiter '|' pipe char (eg. 'client id = 4356hy')
key value delimiter ' = ' equal sign 1 white space (leading , trailing).
please help.
for specific data, can use
select col1, col2, ltrim(rtrim(substring( stuff(col3, charindex('|', col3, patindex('%|client name =%', col3) + 14), 1000, ''), patindex('%|client name =%', col3) + 14, 1000))) col3 table01
edit - charindex vs patindex
test
select col3='clent id = 4356hy|client name = b b bob|client phone = 667-444-2626|client fax = 666-666-0151|info = inf8888877 -mac333330554/444400800' t1m master..spt_values cross join master..spt_values b a.number < 100 -- (711704 row(s) affected) set statistics time on dbcc dropcleanbuffers dbcc freeproccache select a=charindex('|client name =', col3) #tmp1 t1m drop table #tmp1 dbcc dropcleanbuffers dbcc freeproccache select a=patindex('%|client name =%', col3) #tmp2 t1m drop table #tmp2 set statistics time off
timings
charindex: sql server execution times (1): cpu time = 5656 ms, elapsed time = 6418 ms. sql server execution times (2): cpu time = 5813 ms, elapsed time = 6114 ms. sql server execution times (3): cpu time = 5672 ms, elapsed time = 6108 ms. patindex: sql server execution times (1): cpu time = 5906 ms, elapsed time = 6296 ms. sql server execution times (2): cpu time = 5860 ms, elapsed time = 6404 ms. sql server execution times (3): cpu time = 6109 ms, elapsed time = 6301 ms.
conclusion
the timings charindex , patindex 700k calls within 3.5% of each other, don't think matter whichever used. use them interchangeably when both can work.
Comments
Post a Comment