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

How to highlight selected text in notepad++

  –> To highlight a block of code in Notepad++, please do the following steps step-1  :- Select the required text. step-2  :- Right click...