I'm trying to convert my text column to a datetime. I've tried several things but i keep getting errors. The steps I tried so far:
Convert(datetime, mydate, 103)
The error it gives after performing this query in my system:
'Arithmetic overflow error converting expression to data type datetime'
So I searched on the internet and found several solutions which I tried.
Convert(datetime, convert(char(8), mydate)
and
Cast(cast(mydate as char(8)) as datetime)
The error it gives me after performing these queries: 'conversion failed when converting date and/or time from character string' So I searched for more possible solutions and tried this:
Convert(datetime, convert(char(8), mydate),103)
But this still gives me the same error. I really run out of solutions right now. Does any of you guys maybe know how to fix this problem?
Kind regards and thanks in advance
Here is some sample data:
+----+------------+
| ID | MyDate |
+----+------------+
| 1 | 20170120 |
+----+------------+
| 2 | 19940101 |
+----+------------+
| 3 | 20001220 |
+----+------------+
| 4 | 20171101 |
+----+------------+
The issue is likely with formatting. Do you have an example of the string you're trying to convert to a datetime?
I would advise referring to the "style" parameter in the documentation to ensure it matches the string format that you're passing into the CONVERT function. If you can provide a sample string it will be easier to suggest a working example.
https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-2017
#miltenburger, I am guessing that #mydate has been stored as integer. That is why it throws the error:
Msg 8115, Level 16, State 2, Line 4
Arithmetic overflow error converting expression to data type datetime
What you need to do is to convert it to varchar first before converting to datetime.
See the mockup below:
DECLARE #mydate INT =20181001
SELECT CONVERT(DATETIME, CAST(#mydate as VARCHAR(20)), 103)
You need to check the database Implied DATEFORMAT set by language
SET LANGUAGE us_english
SELECT CAST('2018-02-25' AS datetime)
SET LANGUAGE british
SELECT CAST('2018-02-25' AS datetime)
You have the wrong format
select convert(datetime, '20000113', 112)
Related
I'm receiving date values(as string) from one of the source system. It is always a 8 char long as shown below. I'm really not sure why they have kept in this format.
15/06-10
07/03-03
28/10-04
10/07-90
05/07-55
But for my application, I need to convert this into a proper date format(i.e. DD-MON-YYYY).
First 2 characters represent Date
Next 2 characters(after /) represent Month
last 2 represent year
In oracle, I can use to_date to achieve this. Something like
select to_date('15/06-10','yy/mm-dd') from dual;
But in SQL Server, I couldn't find such function. Is there a way to achieve this? The closest I have got is
DECLARE #d VARCHAR(100) = '15/06-10';
SELECT DATEFROMPARTS('20'+SUBSTRING(#d,7,2),SUBSTRING(#d,4,2),SUBSTRING(#d,1,2))
I'm not sure if this is the right way. Also It is giving the results only when I hardcode 20 for the year. But i'm getting data from 1950. So I cannot hardcode 19 or 20 Expected output for above sample is
+----------+-------------+
| 15/06-10 | 15-JUN-2010 |
+----------+-------------+
| 07/03-03 | 07-MAR-2003 |
+----------+-------------+
| 28/10-04 | 28-OCT-2004 |
+----------+-------------+
| 10/07-90 | 10-JUL-1990 |
+----------+-------------+
| 05/07-55 | 05-JUL-1955 |
+----------+-------------+
SQL Server does not have a to_date() function. But here is a simpler way to do the conversion you want:
select convert(date, replace('20' + str, '/', '-'))
from (values ('15/06-10'), ('07/03-03'), ('28/10-04')) v(str)
The expression in the select actually changes the format to YYYY-MM-DD, which is trivially converted to a date. However, SQL Server convert()/cast() is relatively smart about recognizing dates in strings.
One option uses datefromparts() and string functions:
select datefromparts(
'20' + left(#d, 2),
substring(#d, 4, 2),
right(#d, 2)
) as dt
The following query attempts to convert a varchar datatype to int -- but it's not working. Do you know why?
SELECT
CONVERT(INT, [BR]) AS dd,
CAST([BR] AS INT) AS cc
FROM
[Seg].[dbo].[pro_ben]
I get the error:
Msg 245, Level 16, State 1, Line 2
Conversion failed when converting the varchar value '3893.768' to data type int
Obviously your string has a decimal part, hence you cannot cast it directly to an integer.
A solution would be to first CAST to a floating point number, and then to an integer, like:
SELECT CAST( CAST([BR] AS FLOAT) AS INT) AS CC FROM [Seg].[dbo].[pro_ben]
Demo on DB Fiddle:
SELECT CAST('3893.768' AS FLOAT)
| (No column name) |
| ---------------: |
| 3893.768 |
SELECT CAST( CAST('3893.768' AS FLOAT) AS INT)
| (No column name) |
| ---------------: |
| 3893 |
The value is not an integer. This leaves you with various options:
You could use try_convert() which would return NULL.
You can use an intermediate data type and then convert to the final data type, but you might lose precision (under some circumstances).
You could convert to a numeric() instead.
Conversion to a numeric ignores decimal points, but they do not cause an error:
SELECT convert(numeric(10,0), [BR]) as dd
FROM [Seg].[dbo].[pro_ben];
You can then convert to an int if you specifically need that type.
I would try
Select Round([BR],0) as dd
FROM [Seg].[dbo].[pro_ben];
mmmm it appears once I change data type to float, it works.
I would like to convert varchar data to datetime format. So, can anyone let me know the way. this is the sample data available in column.
I tried following things
SELECT
CASE WHEN [EntryTime] = ''
THEN NULL
ELSE CONVERT(datetime2, [EntryTime])
END [EntryTime]
FROM
table
I get this result:
But, I don't need additional milliseconds.
So, can anyone let me know how should I convert it to a datetime format?
For all other tries, I am getting error as
Conversion failed when converting date and/or time from character string
So, can anyone please help me? Thanks.
specify the sub-second width as datetime2(7)
select
try_cast(EntryTime as datetime2(7)) by_try_cast
, try_convert(datetime2(7), EntryTime ) by_try_covert
I also suggest you use try_cast() or try_convert() if they are available, if not just remove "try_", The advantage of these is that if a string is an invalid date they return NULL and don't crash the query.
If you really don't want datetime2:
select cast(try_cast(EntryTime as datetime2(7)) as datetime)
see it: http://rextester.com/YWBB31287
LEFT and Concat (SQL2012+)/ or use standard concatenation
then Cast or Convert (for formatting)
declare #mytable as table (
entrytime varchar(100)
)
insert into #mytable
values ('2017-08-31 08:23:23'),
('2017-11-10 08:28:37.75000000'),
('2017-10-30 08:23:37.32000000')
select cast(left(concat(entrytime,'.000'),23) as datetime) from #mytable
Result
2017-08-31 08:23:23.000
2017-11-10 08:28:37.750
2017-10-30 08:23:37.320
Trying to convert my date to a different format but running into issues.
Currently, my date column looks like:
YearBuilt
1934-01-01 00:00:00:0000
1981-01-01 00:00:00:0000
I'd like to have it be:
YearBuilt
01/01/1934 00:00:00:0000
01/01/1981 00:00:00:0000
I tried
update table set YearBuilt = '01/01/' + YearBuilt
But get the error:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
But my YearBuilt column is already a datetime data type, so I'm not sure which string it's trying to convert.
Any input would be sincerely appreciated!
Date/times are stored in SQL in an internal format. That is a good thing! You should specify the format on output. That is, you can do:
select '01/01/' + datename(year, YearBuilt)
You can build this into the table:
alter table t add YearBuilt_str as ('01/01/' + datename(year, YearBuilt))
Then just use the string version to get the alternative format.
Try this:
DECLARE #D DateTime = '2017-11-01 00:00:00:000';
SELECT #D AS Input,
CONVERT(VARCHAR(50) , #D, 101) +
SUBSTRING(CONVERT(VARCHAR(50), #D, 113), 12, LEN(CONVERT(VARCHAR(30), #D, 113))) AS OutPut;
Result:
+---------------------+------------------------+
| Input | OutPut |
+---------------------+------------------------+
| 01.11.2017 00:00:00 | 1/11/2017 00:00:00:000 |
+---------------------+------------------------+
When your column has Date Time datatype, then you can't update the date with your custom date format.
But, you can select your date with your Custom format by SQL Query:
SELECT '01/01/'+CONVERT(VARCHAR(4), YEAR(YearBuilt))+' '+CONVERT(VARCHAR(MAX), CAST(YearBuilt AS TIME)) [YearBuilt]
FROM <table_name>;
Result :
YearBuilt
01/01/1934 00:00:00.0000000
01/01/1981 00:00:00.0000000
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.