Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

SQL help converting format xxxxxx to xx-xx-xx

  • 18-10-2006 11:36AM
    #1
    Moderators, Society & Culture Moderators Posts: 2,687 Mod ✭✭✭✭


    Hi,

    I need to convert a number from 112233 to 11-22-33 in SQL as part of a select statement.

    eg....

    SET @sortCode = SELECT SortCode FROM tblBankAccount

    -- code to convert @sortCode from 112233 to 11-22-33


    INSERT INTO tblNewTable
    VALUES (@sortCode, etc)

    how would i do this?

    thanks


Comments

  • Closed Accounts Posts: 49 masterclass


    Hi Morpheus,

    Does this help you ?

    select 933406 from dual

    select substr(933405,0,2) || '-' || substr(933405,3,2) || '-' || substr(933405,5,7) from dual


  • Registered Users, Registered Users 2 Posts: 1,193 ✭✭✭liamo


    Hi,

    If this is not going to be a once-off requirement, I prefer to put stuff like this into a UDF so that it makes the SQL Statement a bit more readable.
    Create Function udf_SortCode ( @SortCodeIn Char(6) )
    Returns Char(8)
    As
    Begin
      Declare @SortCodeOut Char(8)
      Set @SortCodeOut = Substring(@SortCodeIn,1,2) + '-' + 
         Substring(@SortCodeIn,3,2)  + '-' + Substring(@SortCodeIn,5,2) 
      Return @SortCodeOut
    End
    


    Then you can do the following :
    Select dbo.udf_SortCode(SortCode) FROM tblBankAccount
    



    Regards,

    Liam

    PS In the absence of information to the contrary, I've assumed that you're using SQL Server.


  • Moderators, Society & Culture Moderators Posts: 2,687 Mod ✭✭✭✭Morpheus


    cheers, thats what i was looking for.

    thanks lads


Advertisement