zoukankan      html  css  js  c++  java
  • SQL Server format phone number

    -- Try:

    /* format (123) 456-7 to 123-4567 */
    select   SUBSTRING('(123) 456-7', 2, 3) + '-' +  SUBSTRING('(123) 456-7', 7, 3) +  SUBSTRING('(123) 456-7', 11, 1)

    /* format 123-456-7890 to (123) 456-7890 */
    select '(' + SUBSTRING('123-456-7890', 1, 3) + ') ' +  SUBSTRING('123-456-7890', 5, 3) + '-' +  SUBSTRING('123-456-7890', 9, 4)

    /* format 1234567 to 123-4567 */
    select   SUBSTRING('1234567', 1, 3) + '-' +  SUBSTRING('1234567', 4, 4)

    /* format  1234567890  to  (123) 456-7890 */
    select '(' + SUBSTRING('1234567890', 1, 3) + ') ' +  SUBSTRING('1234567890', 4, 3) + '-' +  SUBSTRING('1234567890', 7, 4)

    -- Format:

    /* format (123) 456-7 to 123-4567 */
    update tableAAA set phone= SUBSTRING(phone, 2, 3) + '-' +  SUBSTRING(phone, 7, 3) +  SUBSTRING(phone, 11, 1)     
    where len(phone)=11 and CHARINDEX('(',phone)=1 and CHARINDEX(')',phone)=5  and CHARINDEX('-',phone)=10

    /* format 123-456-7890 to (123) 456-7890 */
    update tableAAA set phone= '(' + SUBSTRING(phone, 1, 3) + ') ' +  SUBSTRING(phone, 5, 3) + '-' +  SUBSTRING(phone, 9, 4) 
    where  len(phone)=12 and CHARINDEX('-',phone)=4 and substring(phone,8,1)='-' and charindex('(',phone)= 0 and charindex(')',phone)= 0

    /* format 1234567 to 123-4567 */
    update tableAAA set phone=SUBSTRING(phone, 1, 3) + '-' +  SUBSTRING(phone, 4, 4)
    where  len(phone)=7 and charindex('-',phone)= 0 and charindex('(',phone)= 0 and charindex(')',phone)= 0

    /* format  1234567890  to  (123) 456-7890 */
    update tableAAA set phone= '(' + SUBSTRING(phone, 1, 3) + ') ' +  SUBSTRING(phone, 4, 3) + '-' +  SUBSTRING(phone, 7, 4)
    where  len(phone)=10 and charindex('-',phone)= 0 and charindex('(',phone)= 0 and charindex(')',phone)= 0

  • 相关阅读:
    团队开发-第一阶段冲刺-10
    团队开发-第一阶段冲刺-09
    Spring(三)
    第五周总结
    Spring(一)
    程序员修炼之道:从小工到专家阅读笔记1
    MyBatis(三)
    MyBatis(二)
    MyBatis
    第四周总结
  • 原文地址:https://www.cnblogs.com/emanlee/p/1530454.html
Copyright © 2011-2022 走看看