Converting nvarchar (epoch time) to datetime in t-Sql - sql-server

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

Related

SQL Server: combine date column and time column, insert into datetime column

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

Convert Bigint to Datetime SQL Server

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)

TSQL : Conversion failed when converting date and/or time from character string

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]

Convert to mm/dd/yyyy and select max value

Using SQL Server 2014, I have a date field named LAST_BASELINE_UPDATE_DATE that is stored as datetime.
I used the CONVERT function to convert to mm/dd/yyyy:
convert(date,LAST_BASELINE_UPDATE_DATE,101) as BASELINE_UPDATE_DATE
What I want to be able to do is then select the MAX value of BASELINE_UPDATE_DATE without creating a table. Is this even possible?
It's a little unclear from your post what your data is and what you're trying to get out. Here are a couple solutions, hopefully one of which is applicable
Assuming you want your result as a string formatted mm/dd/yyyy you can do this
select convert(varchar(10), max(LAST_BASELINE_UPDATE_DATE), 101))
from YourTable
If you just need it as a date, just do
select max(LAST_BASELINE_UPDATE_DATE)
from YourTable
if LAST_BASELINE_UPDATE_DATE is already a string (formatted mm/dd/yyyy) and you want it as a date,
select max(convert(date, LAST_BASELINE_UPDATE_DATE, 101))
from YourTable
I think you are complicating this. Just do the conversion on the max datetime values.
declare #table table (LAST_BASELINE_UPDATE_DATE datetime)
insert into #table
values
('20160701 12:21'),
('20160705 03:21'),
('20160401 19:21'),
('20161201 04:21')
select
convert(varchar(10),max(LAST_BASELINE_UPDATE_DATE),101)
from
#table
This method converts your single returned row, which is the max() value of your datetime columns, as opposed to converting every row and then finding the max value.

SQL Server / TSQL cast smalldatetime using isnull

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...

Resources