Saturday, September 28, 2019

SQL Server - Different types Formatting of Date/Time with Example


MM/DD/YYYY to YYYY/MM/DD

Declare @DateFormat varchar(20)
set @DateFormat='02/21/2019' --MM/DD/YYYY
Select @DateFormat as SourceFormat, CONVERT(Date,@DateFormat,101) as TargetFormat

SourceFormat   TargetFormat
02/21/2019         2019-02-21

DD/MM/YYYY to YYYY/MM/DD

Declare @DateFormat varchar(20)
set @DateFormat='24/02/2019' --DD/MM/YYYY
Select @DateFormat as SourceFormat, CONVERT(Date,@DateFormat,103) as TargetFormat

SourceFormat   TargetFormat
24/02/2019         2019-02-24

YYYY/MM/DD to DD/MM/YYYY

Declare @DateFormat varchar(20)
set @DateFormat='2019/05/24' --YYYY/MM/DD
Select @DateFormat as SourceFormat, CONVERT(VARCHAR(10), CAST(@DateFormat AS DATETIME), 103) as TargetFormat

SourceFormat   TargetFormat
2019/05/24         24/05/2019


DD/MM/YYYY to MM/DD/YYYY

Declare @DateFormat varchar(20)
set @DateFormat='20/05/2019' --DD/MM/YYYY
Select @DateFormat as SourceFormat, convert(varchar, convert(date, @DateFormat, 105), 101) as TargetFormat

SourceFormat   TargetFormat
20/05/2019         05/20/2019

MM-DD-YYYY to DD-MM-YYYY

Declare @DateFormat varchar(20)
set @DateFormat='05-24-2019' --MM-DD-YYYY
Select @DateFormat as SourceFormat, convert(varchar, convert(date, @DateFormat, 101), 105) as TargetFormat

SourceFormat   TargetFormat
05-24-2019          24-05-2019

No comments:

Post a Comment