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

Military Time Format

From Wiki

Jump to: navigation, search

This WiKi entry is based on a recent question asked at MSDN forum Custom String Format

In order to create a time string using military format in T-SQL we can use two T-SQL functions DatePart and DateName and also a well known technique of padding value to left using RIGHT function.

  1. declare @d datetime = getdate()
  2. --ddHHmmMMMyy.toUpper (military format).
  3. --That is two digit day, two digit hour, two digit minute, three letter month, two digit year, and month is uppercase.
  4. select Right('00' + cast(DATEPART(day,@d) as varchar(2)),2) +
  5. Right('00' + cast(DATEPART(hour,@d) as varchar(2)),2) +
  6. Right('00' + cast(DATEPART(minute,@d) as varchar(2)),2) +
  7. upper(left(DATENAME(month,@d),3)) + RIGHT(cast(DATEPART(year,@d) as varchar(4)),2)

Contributed by: --Naomi 21:41, 14 April 2010 (GMT)

693 Rating: 3.1/5 (19 votes cast)