locked
How to split single column into multiple columns based on string length RRS feed

  • Question

  • Hi Team,

    I want to split a string into multiple columns like have one column length is 60 chars want to split 1st 40 in one column and remains in 2nd column.

    For example : select len('Microsoft SQL Server Management Studio - Transact-SQL IntelliSense')  -- total length is 66

    want to store 1st 40 means 'Microsoft SQL Server Management Studio - ' In column1

    remains in column2

    Please help on same


    Thanks Bala Narasimha

    • Changed type Jingyang Li Monday, February 10, 2020 2:51 PM
    Monday, February 10, 2020 7:00 AM

Answers

  • Hi BaluChalla,

    Or please check .

    declare @str varchar(300) = 'Microsoft SQL Server Management Studio - Transact-SQL IntelliSense'
    
    select left(@str, 40) as column1, right(@str, len(@str)-40) as column2
    /*
    column1                                  column2
    ---------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Microsoft SQL Server Management Studio -  Transact-SQL IntelliSense
    */

    Best Regards,

    Rachel 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by BaluChalla Tuesday, February 11, 2020 5:03 AM
    Tuesday, February 11, 2020 4:40 AM

All replies

  • Is this what you need?

    declare @str varchar(300) = 'Microsoft SQL Server Management Studio - Transact-SQL IntelliSense'
    
    select substring(@str, 1, 40) as column1, substring(@str, 41, len(@str)) as column2
    • Edited by SQLNeophyte Monday, February 10, 2020 7:11 AM
    Monday, February 10, 2020 7:10 AM
  • Hi,

    declare @str varchar(200) = 'Microsoft SQL Server Management Studio - Transact-SQL IntelliSense'

    select substring(@str,1,40) column1, substring(@str,41,len(@str)) column2,
    left(@str,40),right(@str,len(@str)-40)


    Monday, February 10, 2020 7:22 AM
  • Hi BaluChalla,

    Or please check .

    declare @str varchar(300) = 'Microsoft SQL Server Management Studio - Transact-SQL IntelliSense'
    
    select left(@str, 40) as column1, right(@str, len(@str)-40) as column2
    /*
    column1                                  column2
    ---------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Microsoft SQL Server Management Studio -  Transact-SQL IntelliSense
    */

    Best Regards,

    Rachel 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by BaluChalla Tuesday, February 11, 2020 5:03 AM
    Tuesday, February 11, 2020 4:40 AM