I'm trying to just do a simple check against a column in the table:
If (endDate is null)
use smalldatetime '12/31/2200'
else
use endDate from column
Here is my stored procedure:
ALTER PROCEDURE [dbo].[getCostByDate] #date smalldatetime, #productID int
AS
SELECT cost
FROM testDB.dbo.product_cost
WHERE #date between startDate and isNull(endDate,cast('12/31/2200' as smalldatetime)) and product_id = #productID
I tried to 'cast' the '12/31/2200' to format it for a smalldatetime, but I'm getting the error:
"The conversion of a varchar data type to a smalldatetime data type resulted in an out-of-range value."
The problem is that the date range for smalldatetime is 1900-01-01 through 2079-06-06 and your value is past the upper bound. The solution is to use a value inside the range, or another date type like datetime or datetime2.
cast('12/31/2078' as smalldatetime) would work for instance.
Here's the key:
and isNull(endDate,cast('2029-04-25T15:50:59.997' as smalldatetime))
You have to give the whole date even though it's a smalldatetime... Also, you can't use 2200, it's too far in the future I guess...
Related
I have a database with a date and a time stored separately in datetime columns (not my idea). I need to combine their values to put into another table with a datetime column. As simple as this seems, I just don't seem to be able to do it.
I can get my date value:
cast(sampledate as date) -- returns '2014-11-01'
And my date value:
cast(CollectionTime as time) -- returns '06:46:00.0000000'
I've tried a few different ways of putting them together that look OK.
For example:
concat(cast(sampledate as date) , ' ' , cast(CollectionTime as time)) -- returns '2014-11-05 08:14:00.0000000'
But when I try to insert this into a datetime column, or even just cast it as a datetime value, it doesn't work:
cast(concat(cast(sampledate as date) , ' ' , cast(CollectionTime as time)) as datetime)
-- I get the error 'Conversion failed when converting date and/or time from character string.'
This link warned me against using the FORMAT function, and I've been to some other sites that tell me what NOT to do, but I just can't seem to do this simple thing. Can anyone help? Thanks.
EDIT: Figured it out. This link solved it for older versions of SQL, but not current versions. However, it works fine if you cast to datetime2(0), not datetime.
As I commented above, here is an example where you can add the two datetimes together.
If either column is NOT datetime, simply convert that column to a datetime
Declare #YourTable table (sampledate datetime,CollectionTime datetime)
Insert Into #YourTable values
('2019-06-25 00:00:00','09:09:31')
Select *
,NewDateTime = sampleDate + CollectionTime
From #YourTable
Results
sampledate CollectionTime NewDateTime
2019-06-25 00:00:00.000 1900-01-01 09:09:31.000 2019-06-25 09:09:31.000
I have a table that has a field Report_Date. This field is a bigint type. I have another table that has ReportDate that is datetime type. I want to combine data from each table, but I want the bigint converted into a datetime.
I tried SELECT DATEADD(DD, convert(bigint, Report_Date), Report_date) however I get the error message:
Arithmetic overflow error converting expression to data type datetime.
I have also tried SELECT DATEADD(DD, convert(bigint, Report_Date), convert(datetime, Report_date)) with the same error message result.
I expect the date time to be 2019-02-28 00:00:00.000.
For your example you would need to do something like this.
select convert(datetime, convert(char(8), 20190108))
I can't for the life of me figure out what you are trying to do with your DATEADD logic there.
To cast bigint/int to datetime you firstly need to cast it to varchar. You can do it i.e. like this:
select cast(cast(Report_Date as varchar(80)) as datetime)
I want to add a new dataset into an existing table. My SQL statement is as follows:
INSERT INTO Application ( App, Load_Date, Source)
VALUES ('unknown App', '0001-01-01 00:00:00.000','SYSTEM');
I get this Errormessage: The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
I tried converting with CONVERT(VARCHAR(23), '0001-01-01 00:00:00.000', 120) but it didn't work.
The statement works if I use getdate() oder sysdatetime() though...
What am I missing?
What's the specific data type of your date/time column? If it's datetime, for example, then 0001-01-01 00:00:00.000 is out of range, specifically 1753-01-01 through 9999-12-31.
You could use a datetime2 data type instead because that supports a range of dates between 0001-01-01 00:00:00.0000000 and 9999-12-31 23:59:59.9999999. See this page for more information.
I am trying to convert an Nvarchar(510) column to human readable date time. The column EPOCHDATE is a bigint value.
When I run this:
select
dateadd(ms, 1427213353825%(3600*24*1000),
dateadd(day, 1427213353825/(3600*24*1000), '1970-01-01 00:00:00.0'))
I get back the date in human readable form. 1427213353825 is the EPOCHDATE value from the first row.
BUT when I run it using the column I get
The conversion of the nvarchar value '1427213353825' overflowed an int column.
How can I fix this? I didn't create the table so I don't think I can change that column.
Converting the epoch column to a bigint gets around that error.
DECLARE #blah TABLE (epoch NVARCHAR(512));
INSERT INTO #blah VALUES ('1427213353825');
SELECT
DATEADD(ms, CAST(epoch AS BIGINT) %(3600*24*1000),
DATEADD(DAY, CAST(epoch AS BIGINT)/(3600*24*1000), '1970-01-01 00:00:00.0'))
FROM #blah
I have a very simple query. (I'm working in SQL Server 2008.) I'm trying to select all records from a view where their ModifiedOn column is greater than a specified date. The ModifiedOn column is a datetime format. So, I have:
DECLARE #date1 AS datetime = '2013-07-31 24.59.59.999'
SELECT
some_column
FROM dbo.some_view
WHERE ModifiedOn > #date1
SQL is throwing the following error, though:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. Why is SQL thinking that one of my dates is a varchar, when I know that both of them are datetime formats? How do I fix it?
Datetime variable expects data to be in format of
YYYY-MM-DD HH:MM:SS.mmm
In your case you are trying to assign a value which is not a valid time. HH.MM.SS.mmm
Secondly a clock never strikes 24:00:00 it goes from 23:59:59.999 to 00:00:00.001.
Also in your case rather than juggling with seconds and milliseconds. just use date value and use the ANSI standard YYYYMMDD which is also sargable.
You could have written your above query something like
SELECT some_column
FROM dbo.some_view
WHERE ModifiedOn >= '20130801'