Converting a varchar column to datetime - sql

I have a table that has a column for dateofbirth varchar(10). All the data inside is stored like this '02/01/1990'.
I need to convert it to datetime. I've tried
CAST(DateofBirth AS DATEITME) AS BirthDate
But I keep getting this error when I try importing:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value"
I need it like this:
1990-02-01 00:00:00:000

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]

You need to convert a Varchar in format MM/DD/YYYY to a Datetime.
Sql server recognizes a set of predefined date formats that it is able to automatically parse. See this cheat list.
Your input format corresponds to sql server date format 101, hence you can do :
SELECT CONVERT(Datetime, '02/01/1990', 101)

If you try to do it in sql query there is syntax to do it .
Here a link ..
SQL Server Convert Varchar to Datetime

Your cast statement works for me:
Declare #dateofbirth varchar(10) = '02/01/1990'
select cast(#dateofbirth as datetime)AS BirthDate
Result:
BirthDate
1990-02-01 00:00:00.000

Related

Convert Varchar field to datetime in SQL Server

I am trying to convert varchar date to date time field and having a really hard time. It is giving me:
Conversion failed when converting date and/or time from character string.
or
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
I have 2 datetime formats below :
Date field looks like 21-12-2009, 05-10-2005 etc
Date field looks like 19-03-2018 14:59
select cast(convert(varchar,Stg_hw.date_of_birth,110)as date)
from Stg_Height_and_Weight Stg_hw
where Stg_hw.person_id = 1620458
select CONVERT(datetime,LEFT(date_of_birth,2)+SUBSTRING(Stg_hw.date_of_birth,4,2)+SUBSTRING(Stg_hw.date_of_birth,7,4))
from Stg_Height_and_Weight Stg_hw
where Stg_hw.person_id = 1620458
select cast(Stg_hw.date_of_birth as date)
from Stg_Height_and_Weight Stg_hw
where Stg_hw.person_id = 1620458
CONVERT(DATETIME, DateFieldColumnName, 103) can help to convert those 2 format to datetime.
Sample exection:
CREATE TABLE TestTable (DateVal VARCHAR (20));
INSERT INTO TestTable (DateVal) VALUES
('21-12-2009'), ('05-10-2005'), ('19-03-2018 14:59');
SELECT CONVERT(DATETIME, DateVal, 103) AS Result
FROM TestTable
Please find the working demo on db<>fiddle

SQL Server: how to change data entry from VARCHAR to DATETIME?

I have below sample data:
03202012 as date but the column datatype is Varchar.
I want to convert it to 2012-03-20 00:00:00.000 as Datetime.
I tried using
CAST(CONVERT(CHAR(10), Column, 101) AS DATETIME)
But I get an error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Complete code snippet to test:
DECLARE #Column VARCHAR(MAX) = '03202012'
SELECT CAST(CONVERT(CHAR(10), #Column, 101) AS DATETIME)
Use yyyyMMdd format, that always works:
DECLARE #myDateString varchar(10) = '03202012';
SELECT cast( substring(#myDateString, 5, 4)+
substring(#myDateString, 1, 2)+
substring(#myDateString, 3, 2) AS datetime);
I found below script help me solved my concern.
SELECT convert(datetime, STUFF(STUFF('31012016',3,0,'-'),6,0,'-'), 105)
Result: 2016-01-31 00:00:00.000
Thanks all for the effort. :D
In MySQL, you can use the STR_TO_DATE function to convert a string to a date. For your example, it would look like this
STR_TO_DATE("03-02-2012", "%m-%d-%Y");
Note that the format part of the string must match the format part of the date.
Edit: Just found out this is for SQL Server, but I assume this will work there as well.

How to update/Change the date format to MM/dd/yyyy from dd/MM/yyyy format in sql server?

In my db DOB is saved in dd/mm/yyyy format. i want to change the DOB date format to MM/dd/yyyy. How can i do that?
First i want to know which datatype you are using for saving your date value. There is nothing provided with your code no sample code, table details nothing. anyway
i think your 'date of birth' field datatype is datetime,then you can use the following example
create table checktable(
ID int,
name nvarchar (30),
dob datetime);
Example data insert into the table
insert into checktable(ID,name,dob) values(10,'myname','03/01/2014');
//..........
select * from checktable
//Use CONVERT() it will give you the desired output
SELECT TOP 1 ID, dob,CONVERT(varchar,dob,101) 'mm/dd/yyyy'
FROM checktable
UPDATE
if your datatype is varchar and now it is in the format mm/dd/yyyy and you want to change it into dd/mm/yyyy format then use the following example it will help you
create table checktable1(
ID int,
name nvarchar (30),
dob varchar(20));
// insert sample data
insert into checktable1(ID,name,dob) values(10,'myname','21/05/2010');
select * from checktable1
// change the format using substring()
select * FROM checktable1
select dob,substring(dob,4,3)+substring(dob, 1, 3)+substring(dob, 7, 4) from checktable1
It will give you result in 05/21/2010 (mm/dd/yyyy)format
Microsoft SQL: https://msdn.microsoft.com/en-us/library/ms187928.aspx
Syntax for CONVERT:
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )
use example:
SELECT CONVERT(VARCHAR(10), GETDATE(), 101) AS [MM/DD/YYYY]
For other databases search Date and Time Functions in documentation.
The CONVERT() function is a general function that converts an expression of one data type to another.
The CONVERT() function can be used to display date/time data in different formats.
http://www.w3schools.com/sql/func_convert.asp
if your data type is DateTime then no need to convert it, in later you can convert its format as you want but if your are using varchar or nvarchar
then you can use CONVERT() as below
SELECT convert(datetime, '23/10/2016', 103)
Result : 2016-10-23 00:00:00.000
if just want to conver in date then
SELECT convert(date, '23/10/2016', 103)
Result : 2016-10-23
for more information and other formats
http://www.sqlusa.com/bestpractices/datetimeconversion/
and if you just want to convert string to sting then
SELECT CONVERT(varchar,'23/10/2016',101)
Result : 23/10/2016
This is the query to show all data in dd/mm/yyyy format.Type your column name that contains date in place of datecolumnname and your table name in place of tablename in below query:
select convert(varchar,datecolumname,103) as datecolumname from tablename

Convert from nvarchar to DateTime with am pm?

In my table I have myDate column of type nvarchar(50).
The result I need is to select this date/time: 07/11/2013 11:22:07
And I need to get 07/11/2013 11:22:07 am from it (add am/pm to the original date&time).
I tried everything but get only the original data without am/pm.
This is an example from my query :
select convert(dateTime,myDate,100) as Date from Info
or
select convert(dateTime,myDate,0) as Date from Info
What am I missing ?
try this !!
declare #date datetime
set #date='07/11/2013 11:22:07'
SELECT cast(convert(varchar(20),substring(convert(nvarchar(20),#date, 9), 0, 21)
+ ' ' + substring(convert(nvarchar(30), #date, 9), 25, 2),105) as datetime)
Your field is a NVARCHAR field so just return it without any conversion. In your query you convert string representation into the DATETIME type and returns it. Your browser software which shows query results convert DateTime value into string representation to show it to you and conversion format depends on this software usually you can change it changing Windows Regional Settings.
You can get AM/PM data using following query
declare #date datetime
select #date= CAST('07/11/2013 11:22:07' AS datetime)
select RIGHT ( CONVERT(VARCHAR,#date,9),2)

string ( dd/mm/yyyy) to date sql server

EDIT:
The solution is in the reference post's solution .I was careless to overlook DATETIME--> Varchar(10)
`Syntax for CONVERT:
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )`
I am aware of this post
SQL Server (2005, 2000, 7.0) does not have any flexible, or even non-flexible, way of taking an arbitrarily structured datetime in string format and converting it to the datetime data type.
So I am looking for a solution that solves this particular String format only.
Let's say I have a table in sql server with field :inputDate in datetime format
The following code works without convert/cast
SELECT inputDate
FROM some_table
WHERE inputDate > '01/24/2013'
But it won't work for
SELECT inputDate
FROM some_table
WHERE inputDate > '24/01/2013'
Throwing an The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
Then I tried
SELECT inputDate
FROM some_table
WHERE inputDate > CONVERT(VARCHAR(10), '24/01/2013', 103)
Throwing the same error
Is there a way to convert string in dd/mm/yyyy to be recognize as datetime format in SQL SERVER? Or the only way, and the proper way is doing santization elsewhere?
have you tried using DATETIME instead of VARCHAR(10)
WHERE inputDate > CONVERT(DATETIME, '24/01/2013', 103)
Try to use the information in this post : ISO 8601
I succeed to do the following :
select convert(datetime,convert(char(23),'20140125' ,127),103) as MyDate
to get this :
2014-01-25 00:00:00.000
Use ISO 8601 date format YYYY-MM-DDT00:00:00. It will be implicitly converted to datetime in any locale
Try to increase the length of your VARCHAR(10) to VARCHAR(14) like:
select inputDate from Table
where inputDate > convert(varchar(14), '24/01/2013', 103)