;

How to convert DateTime using SQL Server


Tutorialsrack 20/02/2019 SQL

SQL Server gives various alternatives you can use to format a date/time string. One of the primary need is to get an actual date/time. The most common way to get the current date/time using GETDATE(). GETDATE() provides the current date and time according to the server providing the date and time. If you needed a universal date/time, then GETUTCDATE() should be used. To change the format of the date, you need to convert the requested date to a string and specify the format number corresponding to the format needed.

How to get different SQL Server date formats

  1. Use the date format option along with CONVERT function.
  2. Syntax :-  Convert(varchargetdate(), format-number), For Example,  To get YYYY-MM-DD use SELECT CONVERT(varchar, getdate(), 23)
  3.  Check out the chart to get a list of all format options

Date Only Formats:-

S.No. Format No. Query Output
1. 1 SELECT convert(varchar, getdate(), 1) MM/dd/yyyy
2. 2 SELECT convert(varchar, getdate() , 2) yy.MM.dd
3. 3 SELECT convert(varchar, getdate() , 3) dd/MM/yy
4. 4 SELECT convert(varchar, getdate() , 4) dd.MM.yy
5. 5 SELECT convert(varchar, getdate(), 5) dd-MM-yy
6. 6 SELECT convert(varchar, getdate() , 6) dd MON yy
7. 7 SELECT convert(varchar, getdate(), 7) MON dd, yy
8. 10 SELECT convert(varchar, getdate() , 10) MM-dd-yy
9. 11 SELECT convert(varchar, getdate() , 11) yy/MM/dd
10. 12 SELECT convert(varchar, getdate() , 12) yymmdd
11. 23 SELECT convert(varchar, getdate() , 23) yyyy-MM-dd
12. 101 SELECT convert(varchar, getdate() , 101) MM/dd/YYYY
13. 102 SELECT convert(varchar, getdate(), 102) yyyy.MM.dd
14. 103 SELECT convert(varchar, getdate() , 103) dd/MM/yyyy
15. 104 SELECT convert(varchar, getdate() , 104) dd.MM.yyyy
16. 105 SELECT convert(varchar, getdate() , 105) dd-MM-yyyy
17. 106 SELECT convert(varchar, getdate() , 106) dd MON yyyy
18. 107 SELECT convert(varchar, getdate() , 107) MON dd, yyyy
19. 110 SELECT convert(varchar, getdate() , 110) MM-dd-yyyy
20. 111 SELECT convert(varchar, getdate(), 111) yyyy/MM/dd
21. 112 SELECT convert(varchar, getdate() , 112) yyyymmdd

TIME ONLY FORMATS

S.No. Format No. Query Output
1. 8 SELECT convert(varchar, getdate(), 8) hh:mm:ss
2. 14 SELECT convert(varchar, getdate(), 14) hh:mm:ss:mmm(24h)
3. 24 SELECT convert(varchar, getdate() , 24) hh:mm:ss
4. 108 SELECT convert(varchar, getdate() , 108) hh:mm:ss
5. 114 SELECT convert(varchar, getdate() , 114) hh:mm:ss:mmm(24h)

DATE & TIME FORMATS

S.No. Format No. Query Output
1. 0 SELECT convert(varchar, getdate(), 0) MON dd yy hh:mmAM (or PM)
2. 9 SELECT convert(varchar, getdate() , 9) MON dd yy hh:mm:ss:mmmAM (or PM)
3. 13 SELECT convert(varchar, getdate() , 13) dd MON yy hh:mm:ss:mmm
4. 20 SELECT convert(varchar, getdate() , 20) yyyy-MM-dd hh:mm:ss(24h)
5. 21 SELECT convert(varchar, getdate() , 21) yyyy-MM-dd hh:mm:ss.mmm
6. 22 SELECT convert(varchar, getdate() , 22) MM/dd/yy hh:mm:ss AM(or PM)
7. 25 SELECT convert(varchar, getdate() , 25) yyyy-MM-dd hh:mm:ss:mmm
8. 100 SELECT convert(varchar, getdate(), 100) MON dd yyyy hh:mmAM (or PM)
9. 109 SELECT convert(varchar, getdate() , 109) MON dd yyyy hh:mm:ss:mmmAM (or PM)
10. 113 SELECT convert(varchar, getdate(), 113) dd MON 2018 hh:mm:ss:mmm
11. 120 SELECT convert(varchar, getdate(), 120) yyyy-MM-dd hh:mm:ss
12. 121 SELECT convert(varchar, getdate() , 121) yyyy-MM-dd hh:mm:ss:mmm
13. 126 SELECT convert(varchar , getdate() , 126) yyyy-mm-ddThh:mm:ss:mmm
14. 127 SELECT convert(varchar, getdate(), 127) yyyy-MM-ddThh:mm:ss:mmm

FORMATS WITH ISSUES

S.No. Format No. Query Output Details
1. 130 SELECT convert(varchar, getdate(), 130) 13 ???? 1440 8:01:00:203PM (Current datetime (23/09/2018 08:03pm) output), your output might be different according to your current datetime Islamic/Hijri date
2. 131 SELECT convert(varchar, getdate() , 131) 13/01/1440 8:02:22:120PM (Current datetime (23/09/2018 08:03pm)  output) ,your output might be different according to your current datetime Islamic/Hijri date

 

You can also format the date or time without dividing characters, as well as concatenate the date and time string:

S.No. Sample Query Output
1. select replace(convert(varchar, getdate(),101),'/','') MMddyyyy
2. select replace(convert(varchar, getdate(),101),'/','') + replace(convert(varchar, getdate(),108),':','') MMddyyyyhhmmss

If you want to get a list of all valid date and time formats, you could use the code below and change the @Datetime to GETDATE() or any other date you want to use.  This will output just the valid formats.

Get a list of all valid Date and Time Formats, Use  this Statement
DECLARE @counter INT = 0
DECLARE @Datetime DATETIME = '2018-09-23 00:38:54.840'

CREATE TABLE #DateTimeFormats (dateFormatOption int, dateOutput varchar(40))

WHILE (@counter <= 150 )
BEGIN
   BEGIN TRY
      INSERT INTO #DateTimeFormats
      SELECT CONVERT(varchar,@counter),CONVERT(varchar,@Datetime , @counter) 
      SET @counter = @counter + 1
   END TRY
   BEGIN CATCH;
      SET @counter = @counter + 1
      IF @counter >= 150
      BEGIN
         BREAK
      END
   END CATCH
END

SELECT * FROM #DateTimeFormats


Related Posts



Comments

Recent Posts
Tags