Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

SQL help converting format xxxxxx to xx-xx-xx

Options
  • 18-10-2006 11:36am
    #1
    Moderators, Society & Culture Moderators Posts: 2,688 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 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,688 Mod ✭✭✭✭Morpheus


    cheers, thats what i was looking for.

    thanks lads


Advertisement