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)
Related
I had a query that was working but after importing again one table I see the following problem when converting a Varchar into DateTime.
I have the following problem when running the following query:
select FORMAT(convert (datetime,date) , 'ddMMyyyy') from kat.[dbo].[myTable]
If I try the following I see the same problem:
SELECT convert(datetime, date, 126) from kat.[dbo].[myTable]
The dates that I have in the main table follow the same format:
2017-09-01
EDit with Data Screenshot for the Format:
MAny thanks in advance,
Kat
The dates that I have in the main table follow the same format:
2017-09-01
There are some stings in your table that have different format.
Try to catch them with this code:
select *
from kat.[dbo].[myTable]
where dt not like '[1-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]';
In future, don't store datetime values as strings, use datetime/date.
UPDATE
So it's not true that your strings are like '2017-09-01'
Here is my example with your date:
declare #t table (dt varchar(20));
insert into #t values ('2017-09-01');
select *
from #t
where dt not like '[1-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]';
My code returns no rows because '2017-09-01' reflects the format you inicated, and if it returns all rows in your case, the format is different.
I actually think strings cannot be converted directly to dates (at least not when you use CAST)... Why not convert the string to an int first?
Assuming your column is named [Date], I would stick with:
SELECT
cast(DATEADD(DAY, cast([Date] as int), -2) as date) as [Date]
FROM kat.[dbo].[myTable]
If you have a different column name, here is the template:
SELECT
cast(DATEADD(DAY, cast([MyColumnName] as int), -2) as date) as [MyColumnName]
FROM kat.[dbo].[myTable]
Alternatively, I've also updated your code using CONVERT (which honestly I rarely every use so it could be wrong)
SELECT convert(datetime, convert(int, [Date]), 126) from kat.[dbo].[myTable]
I have a situation
I need to convert a varchar value from dd-mmm-yyyy to dd/mm/yyyy
order by above converted date
Using
CONVERT(VARCHAR(10), CAST([Date] AS DATETIME), 103) as ModifiedDate
I got the required format but, when I do order by ModifiedDate
order by result set showing 2013, 2014, 2015 years mixed match. I think its doing order by date. Where I am doing wrong? Can anybody help? Thanks
Since ModifiedDate is a varchar, its ordering is lexical (01/01/1900 < 01/01/1901 < 02/01/1900).
The solution is not to use a varchar for sorting. Convert your varchar-date to a real date (i.e., only CAST([Date] AS DATETIME) without the outer CONVERT) and sort by this expression.
select CONVERT(VARCHAR(10),cast ([Date] AS DATETIME),103) as ModifiedDate
from <table name> order by CONVERT(VARCHAR(10),cast ([Date] AS DATETIME),103)
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'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...
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'