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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

SQL help converting format xxxxxx to xx-xx-xx

  • 18-10-2006 10: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, 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,688 Mod ✭✭✭✭Morpheus


    cheers, thats what i was looking for.

    thanks lads


Advertisement