Login or Sign Up to become a member!
LessThanDot Site Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

Formatting Dates

From Wiki

Jump to: navigation, search

According to Joe Celko this should always happen on the client side, but in case you ever need it (for example in DTS when you have to output to a file) here is the SQL code.


This part will return a 4 digit year. We are using format values greater than 100

  1. Declare @d datetime
  2.     select @d = getdate()
  3.  
  4.     select @d as OriginalDate,
  5.     convert(varchar,@d,100) as ConvertedDate,
  6.     100 as FormatValue,
  7.     'mon dd yyyy hh:miAM (or PM)' as OutputFormat
  8.     union all
  9.     select @d,convert(varchar,@d,101),101,'mm/dd/yyyy'
  10.     union all
  11.     select @d,convert(varchar,@d,102),102,'yyyy.mm.dd'
  12.     union all
  13.     select @d,convert(varchar,@d,103),103,'dd/mm/yyyy'
  14.     union all
  15.     select @d,convert(varchar,@d,104),104,'dd.mm.yyyy'
  16.     union all
  17.     select @d,convert(varchar,@d,105),105,'dd-mm-yyyy'
  18.     union all
  19.     select @d,convert(varchar,@d,106),106,'dd mon yyyy'
  20.     union all
  21.     select @d,convert(varchar,@d,107),107,'Mon dd, yyyy'
  22.     union all
  23.     select @d,convert(varchar,@d,108),108,'hh:mm:ss'
  24.     union all
  25.     select @d,convert(varchar,@d,109),109,'mon dd yyyy hh:mi:ss:mmmAM (or PM)'
  26.     union all
  27.     select @d,convert(varchar,@d,110),110,'mm-dd-yyyy'
  28.     union all
  29.     select @d,convert(varchar,@d,111),111,'yyyy/mm/dd'
  30.     union all
  31.     select @d,convert(varchar,@d,112),112,'yyyymmdd'
  32.     union all
  33.     select @d,convert(varchar,@d,113),113,'dd mon yyyy hh:mm:ss:mmm(24h)'
  34.     union all
  35.     select @d,convert(varchar,@d,114),114,'hh:mi:ss:mmm(24h)'
  36.     union all
  37.     select @d,convert(varchar,@d,120),120,'yyyy-mm-dd hh:mi:ss(24h)'
  38.     union all
  39.     select @d,convert(varchar,@d,121),121,'yyyy-mm-dd hh:mi:ss.mmm(24h)'
  40.     union all
  41.     select @d,convert(varchar,@d,126),126,'yyyy-mm-dd Thh:mm:ss:mmm(no spaces)'


This part will return mostly a 2 digit year (except for 9, 20 and 21). We are using format values less than 100

  1. Declare @d datetime
  2.     select @d = getdate()
  3.  
  4.     select @d as OriginalDate,
  5.     convert(varchar,@d,0) as ConvertedDate,
  6.     0 as FormatValue,
  7.     'mon dd yyyy hh:miAM (or PM)' as OutputFormat
  8.     union all
  9.     select @d,convert(varchar,@d,1),1,'mm/dd/yy'
  10.     union all
  11.     select @d,convert(varchar,@d,2),2,'yy.mm.dd'
  12.     union all
  13.     select @d,convert(varchar,@d,3),3,'dd/mm/yy'
  14.     union all
  15.     select @d,convert(varchar,@d,4),4,'dd.mm.yy'
  16.     union all
  17.     select @d,convert(varchar,@d,5),5,'dd-mm-yy'
  18.     union all
  19.     select @d,convert(varchar,@d,6),6,'dd mon yy'
  20.     union all
  21.     select @d,convert(varchar,@d,7),7,'Mon dd, yy'
  22.     union all
  23.     select @d,convert(varchar,@d,8),8,'hh:mm:ss'
  24.     union all
  25.     select @d,convert(varchar,@d,9),9,'mon dd yyyy hh:mi:ss:mmmAM (or PM)'
  26.     union all
  27.     select @d,convert(varchar,@d,11),11,'mm-dd-yy'
  28.     union all
  29.     select @d,convert(varchar,@d,11),11,'yy/mm/dd'
  30.     union all
  31.     select @d,convert(varchar,@d,12),12,'yymmdd'
  32.     union all
  33.     select @d,convert(varchar,@d,13),13,'dd mon yyyy hh:mm:ss:mmm(24h)'
  34.     union all
  35.     select @d,convert(varchar,@d,14),14,'hh:mi:ss:mmm(24h)'
  36.     union all
  37.     select @d,convert(varchar,@d,20),20,'yyyy-mm-dd hh:mi:ss(24h)'
  38.     union all
  39.     select @d,convert(varchar,@d,21),21,'yyyy-mm-dd hh:mi:ss.mmm(24h)'


Contributed by: --SQLDenis 17:05, 30 May 2008 (GMT)

Part of SQL Server Programming Hacks

Section Dates

125 Rating: 2.4/5 (103 votes cast)