[Solved] HOW TO SUBSTRING in SQL QUERY


Just check this sample , you have to use charindex(to find the index of comma) and substring function to get substring value

Declare @var varchar(50) = '21699,21712'
select charindex( ',', @var), substring ( @var, charindex(',', @var)+1, len(@var))

you can also use this too:

declare @var varchar(50) = '21699,21712'
select charindex( ',', @var), substring ( @var, charindex(',', @var)+1, len(@var))

;WITH Split_Names (Name, xmlname)
AS
(
    SELECT 
    @var name,
    CONVERT(XML,'<Names><name>' + REPLACE(@var,',', '</name><name>') + '</name></Names>') AS xmlname      
)

 SELECT       
 xmlname.value('/Names[1]/name[1]','varchar(100)') AS part1,    
 xmlname.value('/Names[1]/name[2]','varchar(100)') AS part2
 FROM Split_Names

solved HOW TO SUBSTRING in SQL QUERY