SQL declare datetime - 1001-01-01 - sql-server

I am trying to declare a datetime variable with the value 1001-01-01 00:00:00.000
I have tried the following approaches with no luck
declare #d1 datetime = '1001-01-01';
declare #d2 datetime = 10010101;
declare #d3 datetime = '1001-01-01 00:00:00';
declare #d4 datetime = cast ('1001-01-01' as datetime)
I get the following errors
Msg 242, Level 16, State 3, Line 1
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Msg 8115, Level 16, State 2, Line 2
Arithmetic overflow error converting expression to data type datetime.
Msg 242, Level 16, State 3, Line 3
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Msg 242, Level 16, State 3, Line 4
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Is it possible to declare a datetime variable which can hold the value 1001-01-01 00:00:00.000?

The minimum valid date for a DateTime data type is January 1, 1753.
Try DATETIME2, using:
DECLARE #d4 DATETIME2 = '1001-01-01'
The minimum valid date for DATETIME2 is 0001-01-01

You can't do that. The earliest date that a datetime value can have is 1753-01-01.
Recent versions of Sql Server have the data type datetime2 that has a larger range:
declare #d1 datetime2 = '1001-01-01';

The minimum SQL datetime is January 1,1753, so your date is indeed out-of-range.

SQLServer datetimes must be 1/1/1753 < X < 12/31/9999
http://msdn.microsoft.com/en-us/library/ms187819.aspx
you will not be able to set a DateTime to that year, so you may want to store it as a string and leave it to the upper layer langague to deal with.

Related

Conversion failed when converting date and/or time from character string - when querying the database

The following code:
DECLARE #dateAsString AS nvarchar
SET #dateAsString = '2020-04-28T12:51:33.587Z'
DECLARE #dateObject as DATETIME
SET #dateObject = CAST(#dateAsString as DATETIME)
SELECT DATEDIFF(DAY, #dateObject, CURRENT_TIMESTAMP)
I get this error:
Msg 241, Level 16, State 1, Line 6
Conversion failed when converting date and/or time from character string
How can I ensure that the date stored in the database does not give me this error?
Convert and Cast didn't do the trick... please help!
The reason for the error is that you need to define the size of the #dateAsString nvarchar variable. When the size is not specified the default length is 1 and the actual value of the #dateAsString variable is 2.
Also, as an option, you may use CONVERT() with an appropriate date and time style:
DECLARE #dateAsString AS nvarchar(24)
SET #dateAsString = N'2020-04-28T12:51:33.587Z'
DECLARE #dateObject as DATETIME
--SET #dateObject = CAST(#dateAsString as DATETIME)
SET #dateObject = CONVERT(datetime, #dateAsString, 127)
SELECT DATEDIFF(DAY, #dateObject, CURRENT_TIMESTAMP)
Your code is ok, only your nvarchar was to small to hold you data
DECLARE #dateAsString AS nvarchar(28)
SET #dateAsString = '2020-04-28T12:51:33.587Z'
DECLARE #dateObject as DATETIME
SET #dateObject = CAST(#dateAsString as DATETIME )
SELECT DATEDIFF(DAY, #dateObject, CURRENT_TIMESTAMP)
GO
| (No column name) |
| ---------------: |
| 174 |
db<>fiddle here

Error converting nvarchar to float in SQL Server

I want to view the session hours of previous 3 months in my SQL (hrs column is declared as nvarchar), I need to convert the data into float if I want to view the data of last 3 months but I am getting some errors
SELECT sum(convert(float, hrs))
FROM companysonvinunitvenue
WHERE date >= DATEADD(day, -90, GETDATE())
Error:
Msg 8114, Level 16, State 5, Line 1
Error converting data type nvarchar to float.
SELECT sum(cast(hrs as float))
FROM companysonvinunitvenue
WHERE date >= DATEADD(day, -90, GETDATE())
Error:
Msg 8114, Level 16, State 5, Line 1
Error converting data type nvarchar to float.
SELECT CAST(CAST (hrs AS NUMERIC(19,4)) AS INT)
FROM companysonvinunitvenue
WHERE date >= DATEADD(day, -90, GETDATE())
Error:
Msg 8114, Level 16, State 5, Line 1
Error converting data type nvarchar to numeric.
SELECT CAST(CAST (hrs AS int) AS INT)
FROM companysonvinunitvenue
WHERE date >= DATEADD(day, -90, GETDATE())
Error:
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the nvarchar value '02:00:00 ' to data type int.
I tried these four ways and failed in all of them, how I need to do this?
First, use date and time data types for datetime values. And you will avoid these and many other troubles and also save disk space, RAM, reduce number of reads and so on.
As for your problem. For Sql Server 2008+
DECLARE #companysonvinunitvenue TABLE (hrs NVARCHAR(10))
INSERT #companysonvinunitvenue VALUES ('02:00:00'),('21:31:03')
SELECT DATEPART(HOUR,CAST(hrs AS time))
FROM #companysonvinunitvenue
For other versions
DECLARE #companysonvinunitvenue TABLE (hrs NVARCHAR(10))
INSERT #companysonvinunitvenue VALUES ('02:00:00'),('21:31:03')
SELECT cast(LEFT(hrs,2) AS INT)
FROM #companysonvinunitvenue

SQL Server Convert int to Datetime

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

SQL Server: Convert a varchar to a datetime

I would like a date in a varchar(255) data type as below to be converted to a datetime or date format
05 jun 2007
When I use a cast with either the datetime or date:
UPDATE [dbo].[SSIS_TempDump_Episode_Numbers]
SET DischargeDate = CAST([Discharge date] as Date)
I get this error:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
or alter column gives me the same result. is it because of the date format?
Select CONVERT(datetime,'05 jun 2007') -->'2007-06-05 00:00:00.000'
DECLARE #foo VARCHAR(255)
SET #foo = '05 jun 2007'
PRINT CONVERT(DATETIME,#foo)

Convert varchar to datetime

i'm trying to convert following varchar date to datetime format but it doesn't work
select Convert(datetime, '2010-04-14',103)+10 AS DocDueDate
from tblActionHeader AH
i'm getting the error
Msg 242, Level 16, State 3, Line 1
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
You are using a date style for the format dd/mm/yyyy, use the style 120 instead:
select Convert(datetime, '2010-04-14',120) + 10 AS DocDueDate
from tblActionHeader AH
Have you tried a cast, as "select cast('2010-04-14' as datetime)"?
for example
create table test2 (updatetime varchar(20));
insert into test2 values ('2010-9-12 10:33:5');
select updatetime from test2 where convert(datetime,updatetime,110)>'2010-9-12
If you're using style 103, you should be using British French Date formatting.
select DATEADD(Day, 10,Convert(datetime, '14/04/2010',103)) AS DocDueDate
See the docs for details.

Resources