2015-09-03 74 views
0

目前我已經存儲了客戶信息的表,但是電話號碼字段在不同的輸入表中:格式化數字字段在SQL進行報告

ID | Name |Address |DOB   | Gender |Phone Number | 
--------------------------------------------------------- 
01 | Max |Abc Road| 2000-12-19 | Male |07777 893 8902 | 
02 | Sam |TBH Road| null  | Male |077778938902 | 
03 | Doe |Dr Road| 1999-11-13 | Male |077778 938902 | 

但是是有可能重新格式化手機數場這樣的結果可能會出現像這樣:

ID | Name |Address |DOB   | Gender |Phone Number | 
--------------------------------------------------------- 
01 | Max |Abc Road| 2000-12-19 | Male |07777 893 8902 | 
02 | Sam |TBH Road| null  | Male |07777 893 8902 | 
03 | Doe |Dr Road| 1999-11-13 | Male |07777 893 8902 | 

也用於報告和出口的原因是它能夠確保0的出現的電話號碼前面?

任何幫助深表感謝 - 感謝:)

+0

首先,告訴我們什麼類型的da你正在連接的tabase。 –

回答

1

試試這個

DECLARE @TmpTbl TABLE (PhoneNumber varchar(20)) 
INSERT INTO @TmpTbl 
SELECT 
    '07777 893 8902' 
    UNION ALL SELECT '077778938902' 
    UNION ALL SELECT '077778 938902' 

select 
CONCAT(LEFT(replace(PhoneNumber,' ',''),5) 
     ,' ' 
     ,SUBSTRING(replace(PhoneNumber,' ',''),6,3) 
     ,' ' 
     ,RIGHT(replace(PhoneNumber,' ',''),4)) 
from @TmpTbl 

或本將在地方CONCAT聲明

Stuff(Stuff(REPLACE(PhoneNumber,' ',''),9,0,' '),6,0,' ') 

輸出的工作,以及:

07777 893 8902 
07777 893 8902 
07777 893 8902 
+0

完美!你擊敗了我。 – Utsav