Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
bobmcclellan
Starting Member
46 Posts |
Posted - 2011-09-22 : 08:44:20
|
What's the best way to parse all non numeric chars from a string.I am looking to add a column to a query that shows the value in ms from a string of data.. I want the [duration] column to be converted to Int.SELECT *, duration = substring(pingstring,charindex('time=',pingstring)+5,4)It's hard to show by pasting the data here but the last part of the string is actuallythe [duration] column that the substring is returning. Thanks in advance for any help...bobsample data returnedPingString - durationReply from 159.61.135.1: bytes=32 time=80ms TTL=255 80msReply from 159.61.135.1: bytes=32 time=807ms TTL=255 807mReply from 159.61.135.1: bytes=32 time=79ms TTL=255 79msReply from 159.61.135.1: bytes=32 time=79ms TTL=255 79ms |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-09-22 : 09:37:55
|
[code]-- table var for sample datadeclare @Sample table (id int identity(1,1), pingstring varchar(255))-- insert sample datainsert @Sample(pingstring)select 'Reply from 159.61.135.1: bytes=32 time=80ms TTL=255 80ms' union allselect 'Reply from 159.61.135.1: bytes=32 time=807ms TTL=255 807m' union allselect 'Reply from 159.61.135.1: bytes=32 time=79ms TTL=255 79ms' union allselect 'Reply from 159.61.135.1: bytes=32 time=79ms TTL=255 79ms'-- show sample dataselect * from @Sample-- solutionselectid,pingstring,case when charindex('time=',pingstring) > 0 and charindex('ms',pingstring) > 0 then substring(pingstring,(charindex('time=',pingstring)+5),charindex('ms',pingstring)-(charindex('time=',pingstring)+5)) else NULLend as durationfrom @Sample-- givesid pingstring duration1 Reply from 159.61.135.1: bytes=32 time=80ms TTL=255 80ms 802 Reply from 159.61.135.1: bytes=32 time=807ms TTL=255 807m 8073 Reply from 159.61.135.1: bytes=32 time=79ms TTL=255 79ms 794 Reply from 159.61.135.1: bytes=32 time=79ms TTL=255 79ms 79[/code] No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
bobmcclellan
Starting Member
46 Posts |
Posted - 2011-09-22 : 09:50:01
|
Perfect! Thanks Fred...Very much Appreciated! |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-09-22 : 09:51:44
|
welcome  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|
|
|
|