Tuesday 24 December 2019

Convert varchar DD-MM-YYYY or DD/MM/YYYY to YYYY-MM-DD in SQL Query

//////////////////


declare @DateStr varchar(100)=
'13-09-2019' --or '13/09/2019'
or '2019-09-13'
or 


SELECT case when len(isnull(@DateStr ,''))>0 then CONVERT(VARCHAR(10), case when substring(@DateStr ,5,1)='-' or substring(@DateStr ,5,1)='/'then @DateStr else CONVERT(date, @DateStr , 105) end, 23) else null end




//////////////////// Result

2019-09-13 or 
2019-09-13 or  
2019-09-13 or 
NULL

No comments:

Post a Comment

Excel Sort values in ascending order using function TEXTJOIN

 Excel ::  Text ::  1,3,5,2,9,5,11 Result :: 1,2,3,5,5,9,11 Formula ::     TEXTJOIN ( ",",1,SORT(MID(SUBSTITUTE( A1 ,","...