SQL Server Convert int to Datetime - sql-server

I'm trying to convert some data that has datatype int. However when I tried to convert 200 into year, it throws an error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Code:
declare #MonthStart datetime,
#Day int = 1,
#Month int = 5,
#Year int = 200 /*gives me an error if I input this but when I tried 2016 the code works fine*/
set #MonthStart = Cast(#Month as varchar(10)) +'-'+ Cast(#Day as varchar(10))+'-'+Cast(#Year as varchar(10))
select #MonthStart
I don't know what's the problem on this. Maybe the datetime won't accept this kind of format 200-05-01 as a date format.

As the error pointed out, you have an out-of-range value. The minimum value for DATETIME is:
1753-01-01 00:00:00.000
What you're tring to convert is below the minimum, thus the error.
As commented by marc_s:
And if you're on SQL Server 2008 or newer, you can get around this
problem by using DATETIME2(3) instead of DATETIME. This new datatype
doesn't have those "artificial" range limitations as the old one did.

You can use datetime2 instead of datetime. It supports dates of 1-1-0001 or later. https://msdn.microsoft.com/en-us/library/bb677335.aspx

Related

Dividing a date value in T-SQL

I am working on migrating an Access program into SQL Server.
The following is my SQL code taken directly from access.
(([Promise Date])-([Date Recieved]))/100
As you can see, I am attempting to do a division on a datetime value.
This is the error message i receive:
Msg 257, Level 16, State 3, Line 1
Implicit conversion from data type datetime to int is not allowed. Use the
CONVERT function to run this query.
Both fields are type Datetime. Any ideas what I am missing?
I think if I am right you are looking for something like this
SELECT DATEDIFF(day, [Promise Date], [Date Recieved])/100;
If right you can get more details here
since dates in access are effectively stored as DOUBLE, I would recommend converting to the SQL float type to handle any part days
(cast([Promise Date] as float) - cast([Date Recieved] as float))/100
check out the following example, the different ideas here give quite different answers
declare #x as datetime = '19960420 15:05:48';
declare #y as datetime = '19960423 18:09:23';
select (CAST(#y as float) - CAST(#x as float)) / 100
select (CAST(#y as int) - CAST(#x as int)) / 100
select datediff(day,#x,#y) / 100
select cast(datediff(day,#x,#y) as float) / 100
Access should treat the dates as floats, with the fractional part representing time, and the integer part representing days
Declare #pd Date = '2019-01-15'
Declare #dr Date = '2019-01-12'
Select Cast(DATEDIFF(d,#dr,#pd) As Float) /100
Result
0.03
UPDATE: As per Cato's comment to allow for DateTime
Declare #pd DateTime = '2001-01-01 19:00:00'
Declare #dr DateTime = '2001-01-05 13:00:00'
Select (Cast(DATEDIFF(hh,#dr,#pd) As Float)/24) / 100
Result:
-0.0375

compare current datetime between 2 date

Here is my code:
declare #fdate varchar(50)
declare #tdate varchar(50)
declare #diff int
declare #CalFromdate datetime
declare #CalTodate datetime
set #diff = 360
set #fdate = '2016-12-19 09:30:00'
set #CalFromdate = DATEADD(mi,#diff,CONVERT(DATETIME, #fdate))
print #CalFromdate
set #CalTodate = DATEADD(mi,#diff,CONVERT(DATETIME, #tdate))
print #CalTodate
gives me Dec 19 2016 3:30PM as fromdate
I don't want this format.
Either '2016-12-19 09:30:00' or '2016-12-19 09:30:00:0000'. I want to compare my current date getdate() with 2 dates. If I use above output format with my gatedate format, will it work or both should be in same format?
You are only seeing the date in the format Dec 19 2016 3:30PM because that is the completely arbitrary formatting that is applied in SSMS when printing a datetime value.
If you change your code to select instead of print you will return your datetime value in the format 2016-12-19 15:30:00.000 but again, this is just an arbitrary formatting used to display a type of data.
Within your query, your datetime value doesn't actually have a format. It is simply a date and time. Use it in your comparison and worry about the formatting in whatever application you are presenting the data in.
As an aside, you also don't need to bother using convert on your #fdate or #tdate parameters, as you have already specified them as datetime values in your declare statements.

Error when converting float to datetime

I am trying to convert a float in the format yyyymmdd to datetime. According to this the correct style code for that format is 112.
Code:
select
convert(datetime,cast(myDate as numeric),112)
from MyTable
Error:
Msg 8115, Level 16, State 2, Line 1
Arithmetic overflow error converting expression to data type datetime.
I get the same error without the cast as numeric part. I've been looking around for a couple hours, but I haven't been able to find anything that fixes this. If you know a better way to convert the float to a datetime I would be open to that idea.
Thank you for your help.
EDIT
Here is the working code:
SELECT
case when isdate(CAST(CAST(myDate AS INT) AS VARCHAR(8))) = 1
then CAST(CAST(CAST(myDate AS INT) AS VARCHAR(8)) AS DATETIME)
end
from MyTable
I wrapped it in the isdate because there were a few invalid dates in there. Thanks to Matt for the help.
EDIT2
Better version:
SELECT
TRY_CAST(CAST(CAST(myDate AS INT) AS VARCHAR(8)) AS DATETIME)
FROM MyTable
First you must convert the FLOAT to a VARCHAR. And since FLOAT has a number of decimal points, it must first be converted to an INT.
DECLARE #myDate FLOAT
SET #myDate = 20140721
SELECT CAST(CAST(#myDate AS INT) AS VARCHAR(8))
--20140721
Then you can convert the VARCHAR to DATE or DATETIME format.
DECLARE #myDate FLOAT
SET #myDate = 20140721
SELECT CAST(CAST(CAST(#myDate AS INT) AS VARCHAR(8)) AS DATE)
--2014-07-21

Arithmetic overflow error converting expression to data type datetime. (while displaying date time..)

While executing following error is showing
declare #yr_mnth_dt as numeric;
set #yr_mnth_dt = 20130822;
select convert(datetime,#yr_mnth_dt,112) as YR_MNTH_DT
error shows
Arithmetic overflow error converting expression to data type datetime.
You issue is that you're trying to convert the numeric to a datetime, and this just isn't working.
You need to turn your numeric into a string first:
declare #yr_mnth_dt as numeric;
set #yr_mnth_dt = 20130822;
select yr_mnth_dt = cast(cast(#yr_mnth_dt as char(8)) as datetime);
SQL Fiddle with demo.
When you try and convert a numeric type to a datetime, SQL Server tries to add the numeric value as the number of days to the date 01-Jan-1900. In your case this is trying to add millions of days, and hence the overflow error.
CONVERT works fine, too, if you prefer:
select yr_mnth_dt = convert(datetime, convert(char(8), #yr_mnth_dt));
SQL Fiddle with demo.
I've only seen the conversion used for strings. I can't easily tell whether it's even designed to work with numbers. You could convert the number to a string, then the string to a date. However, I would personally just use DATEFROMPARTS:
SELECT DATEFROMPARTS(#yr_mnth_dt / 10000,
(#yr_mnth_dt / 100) % 100,
#yr_mnth_dt % 100) AS YR_MNTH_DT
Why numeric?
Try this
declare #yr_mnth_dt as varchar(10);
set #yr_mnth_dt = '20130822';
select convert(datetime,#yr_mnth_dt,112) as YR_MNTH_DT

How to convert this varchar values to a datetime

I want to convert a varchar value that iI set up to a date time value. I want to load rows of a database into another database, but conversion is not going well.
This is my query:
select Krant
, cast(jaar as varchar(4))+'-'
+RIGHT('0'+cast(maand as varchar(2)),2)+'-'
+RIGHT('0'+cast(dag as varchar(2)),2) as datum
, Inhoud as artikel
, LEN(Inhoud)-LEN(Replace(Inhoud,' ','')) as numwords
, 'Goirle' as vestiging
from [Sitecore_Bibliotheekmb_Krantenknipsel].[dbo].[KRANGOI]
The cast to Datum has to be a datetime value, but i am not getting it to work properly. When i tried to cast to datetime it gave me an out of range exception.
These are the results of this query:
alt text http://94.100.115.48/837450001-837500000/837478801-837478900/837478868_5_dE_7.jpeg
I want the "Datum" field to be a Datetime field with the same values but in Datetime format. Could anyone help me please :).
Thanks,
Younes
Use this:
select Krant, cast(cast(jaar as varchar(4))+'-'
+RIGHT('0'+cast(maand as varchar(2)),2)+'-'
+RIGHT('0'+cast(dag as varchar(2)),2) as datetime) as datum,
Inhoud as artikel,
LEN(Inhoud)-LEN(Replace(Inhoud,' ','')) as numwords,
'Goirle' as vestiging
from [Sitecore_Bibliotheekmb_Krantenknipsel].[dbo].[KRANGOI]
You are not casting the Varchars to a Datetime
CAST and CONVERT on MSDN.
Can you try this:
SELECT CAST(('2010' + '-' + '01' + '-' + '06') AS DATETIME)
If any of your dates are earlier than 1753, a simple datetime type will not be sufficient. For this, you'd need to use the datetime2 datatype introduced in SQL Server 2008.
Also check that all values of maand and dag are valid days and months.

Resources