Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Convert Varchar to Datetime

I have this date format: 2011-09-28 18:01:00 (in varchar), and I want to convert it to datetime changing to this format 28-09-2011 18:01:00. How can I do it?

like image 264
HaOx Avatar asked Apr 20 '12 13:04

HaOx


People also ask

How do I convert text to date in sql?

SQL Server: Convert string to date explicitly In SQL Server, converting a string to date explicitly can be achieved using CONVERT(). CAST() and PARSE() functions.

How do you resolve the conversion of a VARCHAR data type to a datetime data type resulted in an out of range value?

The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. You need separators for the date like a “/”, a “.” or a “-“. We use substring to concatenate the “-” to use an acceptable date format and then we use the CONVERT function to convert the characters to sql date.

How do I convert a string to a date?

Using strptime() , date and time in string format can be converted to datetime type. The first parameter is the string and the second is the date time format specifier. One advantage of converting to date format is one can select the month or date or time individually.


1 Answers

SELECT CONVERT(Datetime, '2011-09-28 18:01:00', 120) -- to convert it to Datetime  SELECT CONVERT( VARCHAR(30), @date ,105) -- italian format [28-09-2011 18:01:00] + ' ' + SELECT CONVERT( VARCHAR(30), @date ,108 ) -- full date [with time/minutes/sec] 
like image 151
Zyku Avatar answered Sep 22 '22 21:09

Zyku