What is going on in this SQL: `SELECT CONVERT(CHAR(10), GETDATE(),103)`

Viewed 1120

I found this in some code that I was tasked with documenting:

SELECT CONVERT(CHAR(10), GETDATE(), 103)

I ... don't get it. The first arg should be a data_type, not a string. How is "LineFeed" a valid data_type?

It runs, and returns today's date (in SQL Server) so I must be missing something blindingly obvious.

2 Answers

CHAR is a valid data type, in addition to being a function.

The parser is intelligent enough to know that in this context, CHAR is being used as a datatype.

The CONVERT() function takes 3 arguments:

  1. data type
  2. expression
  3. style

Your line SELECT CONVERT(CHAR(10), GETDATE(), 103) meets this signature:

  1. data type is CHAR(10)
  2. expression is GETDATE()
  3. style is 103

The GETDATE() expression returns a DATETIME object which is converted to a CHAR(10) in the style specified by 103 (which is dd/mm/yyyy as per the site I linked to above).

Related