Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i select a datetime as ddMMyy, so a two-digit year?

I'm replacing my C# code for performance reasons with a sql query:

CreatedAt.ToString("ddMMyy", System.Globalization.CultureInfo.InvariantCulture)

returns the creation-date as 140115(for today). Simple enough in C#, but how do i get this in T-SQL?

I have tried:

REPLACE(CONVERT(CHAR(10), ChargeArrival.CreatedAt, 103), '/', '')

which is almost correctly, but the year has four digits instead of two, so 14012015 instead of 140115.

like image 672
Tim Schmelter Avatar asked Jan 14 '15 15:01

Tim Schmelter


People also ask

What is default date format in SQL?

The default string literal format, which is used for down-level clients, complies with the SQL standard form that is defined as YYYY-MM-DD. This format is the same as the ISO 8601 definition for DATE.


1 Answers

As the docs for CONVERT state, use 103 if you want the century and 3 if you dont:

REPLACE(CONVERT(CHAR(10), ChargeArrival.CreatedAt, 3), '/', '')
like image 124
Jamiec Avatar answered Oct 01 '22 06:10

Jamiec