Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql-server

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.

like image 389
Brondahl Avatar asked Oct 29 '18 19:10

Brondahl


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.

like image 192
Tab Alleman Avatar answered Oct 16 '22 13:10

Tab Alleman


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).

like image 32
MJS Avatar answered Oct 16 '22 13:10

MJS