格式化SQL中用于报告的数字字段
目前我有一个表格,用于存储客户的详细信息,但是表格中的电话号码字段的input方式不同:
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出现?
任何帮助非常感谢 – 谢谢:)
尝试这个
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