SQL Server Varchar to Datetime conversion - sql-server

I have a varchar representing a datetime in this form :
dd-MM-yyyy HH:MI:SS AM
How can I convert it to datetime type ?
I have checked the list of standards of datetime here But I see no corresponding one to my format above.

Cast it directly
select CAST('10-02-2014 05:10:22 AM ' AS DATETIME)
Try this too..
select CONVERT(varchar(15),GETDATE(),105)+' '+CONVERT(varchar(30),CONVERT(VARCHAR,GETDATE(),108))+' '+SUBSTRING(CONVERT(varchar(30),CONVERT(VARCHAR,GETDATE(),100)),18,2)

Related

Convert varchar to datetime in sqlserver table

In SQL Server I have a column called "dateCreation" defined as varchar(50) and I would like to convert it to datetime type. If I change the column's type to datetime with the MS SQL Server Management Studio I get the following warning:
'atPropostes' table
- Warning: Data might be lost converting column 'dateCreation' from 'varchar(50)'.
Then If I execute this:
SELECT CONVERT(Datetime, dateCreation, 120) as dateCreation from atPropostes -- to convert it to Datetime
I get the following error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
What would be the best way to
(1) change the column type to datetime and
(2) convert all the registers from varchar(50) to Datetime type?
Thanks!
I assume that you are using SQL Server 2012
Try like this
SELECT TRY_CONVERT(DATETIME, dateCreation, 120) AS dateCreation
FROM atPropostes
Note: Please make sure that your date stored in varchar type have consistent date format. It should be something like yyyy-mm-dd or mm-dd-yyyy or.....

SQL SERVER how to convert a nvarchar field that contains 'dd/MM/yyyy HH:mm:ss' to datetime

How to convert a nvarchar field that contains 'dd/MM/yyyy HH:mm:ss' to datetime.
When i'm trying this conversion, it returns an error:
"The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value"
You want to do this first:
SET DATEFORMAT dmy;
GO
and then do the select.
SELECT convert(datetime, yourNVARCHARCol, 104)

SQl query datetime from nvarchar error

In SQL,
declare #date datetime ='Jul15'
select CONVERT(varchar(10),#date,105)
getting error
Conversion failed when converting date and/or time from character
string.
Please help
It is because Jul15 is not a valid SQL Server Date or DateTime value, hence the error when it is trying to convert it to date/datetime to assign it to a datetime variable.
Ideally you should be using ANSI-Standard when working with date or datetime values in SQL Server, ANSI-Standard Date value has the format of YYYYMMDD, therefore your statement should look something like this.....
declare #date datetime ='20150701'
select CONVERT(varchar(10),#date,105)

could not convert varchar to datetime in SQL server

In my database i got problem when converting varchar to datetime due to different format of datetime as per below.
Conversion failed when converting date and/or time from character
string.
I have 2 select clause as per below
Select Getutcdate()
Select top 1 localmachinetime from perfmon
both return following format respectively
2012-05-28 06:54:45.753>
28-05-2012 03:03:07
and when i try to convert it to datetime using CAST then it gives me error in second case as per below.
The conversion of a varchar data type to a datetime data type resulted
in an out-of-range value.
You can use convert with a Date/Time style.
convert(datetime, '28-05-2012 03:03:07', 105)

Convert varchar column to smalldatetime

I have a table with 800+ records. In this table I have a column named 'Data' of varchar(10) datatype which contains dates in dd.MM.yyyy format.I want to convert it to smalldatetime.
I've tried converting it using Enterprise Management Studio Express, but I receive this error:
The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value.
How can I convert it?
I think you'll need to do a little string manipulation to get this to work as I think SQL is expecting 'MM.dd.yyyy'. So, update your table to flip-flop the month and day first, then the conversion should go through.
update YourTable
set Data = SUBSTRING(Data,4,3) + LEFT(Data,3) + RIGHT(Data,4)
You can use:
SELECT ID, CAST(VarcharCol As SmallDateTime) as DateTimeCol From Test1
This will return a table with varcharcol values as smalldatetime
Then update the content of varcharcol with the new values.
If you don't want implicit conversion to smalldatetime, you should use CONVERT and the argument style.
dd.MM.yyyy format correspond to style 104.
For example :
SELECT CONVERT(smalldatetime, '31.12.2018', 104) AS "Result"
Result
------
2018-12-31 00:00:00

Resources