Convert data dd / mm / yyyy hh: mm: ss to YYYY / mm / dd

I have a date in the following format 14/12/2015 00:00:00 and I need to convert to 2015-12-14 how can I do this in SQL?

Author: Marco Souza, 2015-12-15

6 answers

Do this little padawan:

CONVERT (VARCHAR, CONVERT(DATETIME, dataSistema, 103), 102) AS novaData

Example: http://sqlfiddle.com/#! 6 / 2173d/2

In the documentation has some examples and explanations about the CONVERT

 7
Author: DiegoAugusto, 2015-12-15 21:12:53
DECLARE @data datetime=GetDate()

SELECT concat(YEAR(@data),'-',MONTH(@data),'-',DAY(@data)) As data_convertida

The Concat function is valid only from SQL Server 2012 if you do not have this SQL use the following

SELECT cast(YEAR(@data) as varchar(4))+'-'+cast(MONTH(@data)  as varchar(2))+'-'+cast(DAY(@data) as varchar(4)) As data_convertida

Note: See that in this case I used a variable of type datetime Using the DAY(), MONTH (), and YEAR () functions, SQL automatically knows where to find the values for each of the values it asks for in the function.

What type of variable/data are you trying to convert to that format

 4
Author: Alberto Cláudio Mandlate, 2015-12-15 13:30:33

Edited: after some tests I noticed that the function CONVERT will only work if the variable that will be formatted/converted is of type Date.

Use the function CONVERT(datatype(size), variable, style).

The first parameter is the type of data that will be formatted, the second parameter is the variable that will convert and the third is the code of the pattern you want, in this case 111 corresponds to yyyy/mm/dd.

CONVERT(VARCHAR(10), GETDATE(), 111);
 0
Author: Pedro Camara Junior, 2015-12-17 18:17:32
SELECT convert(datetime, '14/12/2015 00:00:00', 103)
 -1
Author: PauloHDSousa, 2015-12-15 13:09:57

In MYSQL you can use DATE_FORMAT ()

 -1
Author: Luan Gabriel, 2015-12-15 13:12:00

Try This:

SELECT CONVERT(VARCHAR (10), GETDATE (), 120)

Or that

Select date (ColumnName) from tablename

 -1
Author: durtto, 2015-12-15 13:25:59