Search code examples
sqlsql-serversql-server-2008ssis-2012

sql query to get static result


i have a data of numbers: 123456789

i needed it to be converted into : 123-45-6789

and if my data is like : 56789 and 456789 it should be converted to : 5-6789 and -45-6789

where i need an sql query to get this coneverted auomatically(column) based on data.


Solution

  • Yet another option

    Declare @YourTable table (SomeCol int)
    Insert Into @YourTable values
     (123456789)
    ,(56789)
    ,(456789)
    ,(6789)
    
    Select *
          ,ltrim(replace(stuff(stuff(str(SomeCol,9),6,0,'-'),4,0,'-'),' -',''))
     From  @YourTable
    

    Returns

    SomeCol     (No column name)
    123456789   123-45-6789
    56789       5-6789
    456789      45-6789
    6789        6789