Sunday, 4 September 2016

How to split a comma-separated value to columns| SQL Server Interview Question

Q. How to split a comma-separated value to columns. Ex('First,Second,Third')
Output should be as below


Ans:
DECLARE @S NVARCHAR(100) = 'First,Second,Third'
SELECT SUBSTRING(@S,0,CHARINDEX(',',@S,0)) Column1,
SUBSTRING(@S,(CHARINDEX(',',@S,0))+1,(CHARINDEX(',',@S,(CHARINDEX(',',@S,0)+1)))-(CHARINDEX(',',@S,0))-1) Column2,

SUBSTRING(@S,(CHARINDEX(',',@S,(CHARINDEX(',',@S,0)+1)))+1,LEN(@S)) Column3

Visit Our New Site for HR Interview Questions Answers http://www.interviewboat.com/Company/GetCompany
Sponsored Ads

3 comments:

  1. DECLARE @S NVARCHAR(100) = 'First,Second,Third'
    SELECT SUBSTRING(@S,0,CHARINDEX(',',@S,0)) Column1,
    SUBSTRING(@S,(CHARINDEX(',',@S,0))+1,(CHARINDEX(',',@S,(CHARINDEX(',',@S,0)+1)))-(CHARINDEX(',',@S,0))-1) Column2,
    SUBSTRING(@S,(CHARINDEX(',',@S,(CHARINDEX(',',@S,0)+1)))+1,LEN(@S)) Column3

    ReplyDelete
  2. please send me sql interview question in my id prakashkw2@gmail.com

    ReplyDelete