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 |
|
soorajtnpki
Posting Yak Master
231 Posts |
Posted - 2008-07-28 : 06:13:42
|
| hi all, i want to select phone numbers from a column phone as follows.only two cases to consider and also all spaces should be avoided.case1 input +44 (0) 113 386 6400 output 1133866400case2 input 1133866400 output 1133866400thanks in advance. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-07-28 : 06:24:07
|
| [code]select replace ( case when charindex(')',data)>0 then substring(data,charindex(')',data)+1,len(data)) else data end,' ','' ) as data from(select '+44 (0) 113 386 6400' as data union allselect '1133866400') as t[/code]MadhivananFailing to plan is Planning to fail |
 |
|
|
soorajtnpki
Posting Yak Master
231 Posts |
Posted - 2008-07-28 : 06:34:19
|
| HI Madhivanan, thanks it works great...ok. |
 |
|
|
VGuyz
Posting Yak Master
121 Posts |
Posted - 2008-07-28 : 06:41:41
|
| Check this,No need to go for case statement.select right((replace('+44 (0) 113 386 6400',' ','')),(len(replace('+44 (0) 113 386 6400',' ',''))-(charindex( ')',replace('+44 (0) 113 386 6400',' ',''))))) |
 |
|
|
VGuyz
Posting Yak Master
121 Posts |
Posted - 2008-07-28 : 06:50:53
|
| Is there any wrong in my query. |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2008-07-28 : 08:53:00
|
| Hi VGuyzYour query provides the right answers I don't know if it is quicker or slower than Madhivanan's approach. I think Madhivanan's method is easier to debug though.Consider '+44 (0) 113 386 6400'Your query performs on this string...3 x REPLACE1 X LEN1 X CHARINDEXThe CASE statement performs on this string...1 x CASE overheads1 x REPLACE1 x LEN1 x SUBSTRING2 x CHARINDEXso if it takes less time to do 2 additional REPLACES than it takes to process the CASE statement with 1 additional SUBSTRING and CHARINDEX then your query will be quicker. Both approaches perform 5 string ops.However that's the best scenario for your statement. Consider what happens for the 2nd example string.....Consider '1133866400'Your query performs on this string...3 x REPLACE1 X LEN1 X CHARINDEXThe CASE statement performs on this string...1 x CASE overheads1 x CHARINDEX check1 x REPLACESo you are still doing 5 string operations to the CASE statement's 2I have a feeling that your approach might be faster on a dataset comprising mostly strings with () in them, and the Case approach faster on datasets with mostly wholly numerical strings.-------------Charlie |
 |
|
|
VGuyz
Posting Yak Master
121 Posts |
Posted - 2008-07-28 : 09:13:12
|
| Hi charlie,its really nice.Thanks for u'r information. |
 |
|
|
|
|
|
|
|