Split Date with comma separated value

If you want to select multiple dates from your date picker and store it into data tables. You can use below query to split date:

DECLARE @Dates as varchar(500)='2015-11-08,2015-11-07,2015-11-06,2015-11-05,2015-11-04,2015-11-03,2015-11-02'
DECLARE @totalLength as int=LEN(@dates)+1
DECLARE @intStartposition int=1
DECLARE @intEndposition int=CHARINDEX(',',@Dates)
WHILE @intStartposition < @totalLength
BEGIN
           IF @intEndposition =
            SET @intEndposition = @totalLength
           
     SELECT SUBSTRING(@Dates,@intStartposition,@intEndposition-@intStartposition)
    
     SET @intStartposition = @intEndposition+1
     SET @intEndposition = CHARINDEX(',', @Dates, @intStartposition)
    
     PRINT @intStartposition
     PRINT @intEndposition
END


Hope it will help you!

Comments