Convert text to system date format [duplicate]

EDIT

From SQL Server 2012 and later you might use the FORMAT function if you want to force a date into a formatted text: https://msdn.microsoft.com/en-us/library/hh213505.aspx

With earlier versions you might use something like this:

DECLARE @d DATETIME=GETDATE();

DECLARE @TargetFormat VARCHAR(100)='DD/MM/YYYY';

SELECT CONVERT(VARCHAR(100),@d, CASE @TargetFormat
                                  WHEN 'MM/DD/YYYY' THEN 101
                                  WHEN 'DD/MM/YYYY' THEN 103
                                  --add all formats convert can undertsand
                                 ELSE 101 END )         

Previous

Look here: https://msdn.microsoft.com/en-us/library/ms187928.aspx

The third parameter in CONVERT specifies the format.

The MM/DD/YYYY was 101, dd/mm/yyyy was 103

But: You should avoid date literals as they are depending on your system’s culture.

From your question I get that you are the owner of the source, so you can change this to the way you need it. I strongly advise you to use independent formats.

Look here: https://stackoverflow.com/a/34275965/5089204

Leave a Comment