-1

I came across a SQL query:

select IssuedDate, Convert(char(4), IssuedDate, 0)
from TempTable

and this is its output:

           IssuedDate            Null
    ----------------------------------
    1964-02-17 00:00:00.0000000  Feb 
    2018-08-28 00:00:00.0000000  Aug 
    2018-08-28 00:00:00.0000000  Aug 
    2018-08-28 00:00:00.0000000  Aug 

Can anyone please let me know how the convert function is working and converting month number to month name?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Deepak Verma
  • 617
  • 5
  • 18
  • Possible duplicate of [Convert Month Number to Month Name Function in SQL](https://stackoverflow.com/questions/185520/convert-month-number-to-month-name-function-in-sql) – Amira Bedhiafi Oct 24 '19 at 10:38
  • Why not use `DATENAME`? – Thom A Oct 24 '19 at 10:38
  • As for how it's working, it's using `CONVERT` and a [Date and Time style](https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-ver15#date-and-time-styles). But it seems odd that it wants the first 3 characters of the month's name, followed by a space. – Thom A Oct 24 '19 at 10:39
  • @larnu yes that making me confused about its implementation – Deepak Verma Oct 24 '19 at 11:05

1 Answers1

1

This is using the default format for convert (the third argument is 0), which is "mon dd yyyy hh:miAM". That is, the first three characters in the formatted string are the month abbreviation.

The conversion is to a string with a length of four, it keeps only the first four characters. That would be the month abbreviation and the following character.

In my opinion, a more sensible approach would be:

left(datename(month, issueddate), 3)

This at least works for English, where the month abbreviations are the first the characters of the month name.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786