Convert float value from a db2 database to a date format? - sql

I have a database from a custom application.
The values in the database for a date is 44416.254005, and the application that uses this database resolves that number to the following date 8/8/2021 6:05:46 AM.
Another example is 44416.268113 to 8/8/2021 6:26:05 AM.
ex. 44328.93713 = 5/12/2021 10:29:28 PM
I have an old backup of this database and need to figure out how the conversions are made?

This looks like the an Excel format for a date. If so, you can convert to seconds and add to 1899-12-31:
select timestamp '1899-12-31 00:00:00' + (44416.268113 * 60 * 60 * 24) second

import datetime
def xldate_to_datetime(xldate):
start = datetime.datetime(1899, 12, 30)
delta = datetime.timedelta(days=xldate)
return start + delta

Related

How to deal with a date stored as a string in an int field in Teradata?

I've got a table in Teradata that stores a date in an 8 character INT field in the following form "YYYYMMDD", so for today it would store "20180308". If I try to CAST it as a date like this:
CAST(date_field AS DATE FORMAT 'YYYY-MM-DD')
It transforms the date to some future date in the year 3450 or something.
I think it's an error that this data isn't either stored as a date object. Is there anyway to overcome this glitch? I don't have access to change this unfortunately.
Thanks
It's not an 8 character integer, it's an 8 digit integer.
Teradata stores dates using
(year - 1900) * 10000
+ (month * 100)
+ day
This results in 1180308 for today and 20180308 will return 3918-03-08
To cast it to a date you need to use
cast(intdate-19000000 as date)
select cast('20180308' as date format 'yyyymmdd') ;

Data in SQL not in proper date format, how to change it into the right format?

I have a column called ProcessTimeOnHold that shows how long it takes us to process an order.
Any order that takes less than 24 hours to process is shown in this format HH:MM:SS. As far as I can tell this data is in the proper date format however any order that takes longer than 24 hours shows up in this format D.HH:MM:SS So, if an order took 29 hours and 34 minutes it will show up as 1.05:34:00.
The problem I have is if I try to convert or manipulate the data for example into minutes the orders in the proper data format will work but anything after 24 hours will return an error.
For example if I try to convert to minutes with this:
LTRIM(DATEDIFF(MINUTE, 0, ProcessTimeOnHold))
It will convert all orders into minutes until it runs into an order with the D.HH:MM:SS format and then it will return this error:
Error Message: Conversion failed when converting datetime from
character string
Any way that I can change my entire column into datetime?
You might want to search for a less verbose solution, but this is one option, using string manipulation:
SELECT DATEDIFF(MINUTE, 0, (convert(time,RIGHT(ProcessTimeOnHold,8)))) +
(case when ProcessTimeOnHold like '%.%'
then
(cast((LEFT(ProcessTimeOnHold,CHARINDEX(ProcessTimeOnHold,'.')-1)) AS INT) * 24 * 60)
else 0 end) as TotalMinutes
(Edited based on comment)
Here's a way to get just the minutes:
declare #table table (ProcessTimeOnHold varchar(16));
insert #table (ProcessTimeOnHold)
values ('00:20:00')
, ('01:20:00')
, ('1.05:34:00');
select ProcessTimeOnHold
, datediff(minute,0,right(ProcessTimeOnHold,8))
+ isnull(nullif(replace(left(ProcessTimeOnHold,charindex('.',ProcessTimeOnHold)),'.',''),''),0) * 1440 'Minutes'
from #table
RETURNS:
ProcessTimeOnHold Minutes
----------------- -----------
00:20:00 20
01:20:00 80
1.05:34:00 1774
Try this one-
ANSI format: 2006.10.23
SELECT [ANSI]=CONVERT(char,CURRENT_TIMESTAMP,102)

How to filter SQL by time (greater and less than)?

I want to query a subset from a dataset. Each row has a time stamp of the following format:
2014-04-25T17:25:14
2014-04-25T18:40:16
2014-04-25T18:44:57
2014-04-25T19:10:32
2014-04-25T20:22:12
...
Currently, I use the following query to select a time-based subset:
time LIKE '%2014-04-25T18%' OR time LIKE '%2014-04-25T19%'
This becomes quite complicated when you start to filter by mintutes or seconds.
Is there a way to run a query that such as ...
time > '%2014-04-25T18%' AND time < '%2014-04-25T19%'
A regular expression would be okay, too.
The database is a SpatiaLite database. The time column is of type VARCHAR.
If the date is being treated as a string and based on the example above:
time LIKE '%2014-04-25T18%' AND time <> '%2014-04-25T18:00:00:000'
Otherwise, you could convert the date to seconds since midnight and add 60 minutes to that to create the range part of the filter
DECLARE #test DATETIME = '2014-04-25T17:25:14'
SELECT #test
, CONVERT(DATE,#test) AS JustDate
, DATEDIFF(s,CONVERT(DATETIME,(CONVERT(DATE,#test))), #test) AS SecondsSinceMidnight
-- 60 seconds * 60 minutes * 24 hours = 86400
Thanks to your posts and this answer I came up with this solution:
SELECT * FROM data
WHERE DATETIME(
substr(time,1,4)||'-'||
substr(time,6,2)||'-'||
substr(time,9,2)||' '||
substr(time,12,8)
)
BETWEEN DATETIME('2014-04-25 18:00:00') AND DATETIME('2014-04-25 19:00:00');

Concatenating date and time fields

I have a table invoices with this fields:
invDate -> a date field
invTime -> a time field
I need to do querys like
SELECT top 10 * from invoices WHERE DATETIME(invDate+invTime)
BETWEEN DATETIME('2013-12-17 17:58') AND DATETIME()
or something like that. I don't know how to concatenate the invDate and invTime to create a datetime field. The only thing that i could do is this horribly thing:
DATETIME( YEAR(invDate), MONTH(invDate), DAY(invDate), 17, 52 ) AS MyDatetime
Couldn't even get hour and time with hour(invTime) and minute(invTime):
DATETIME( YEAR(invDate), MONTH(invDate), DAY(invDate),
HOUR(invTime), MINUTE(invTime) ) AS MyDatetime
I'm doing the querys throught the VFP Odbc Driver via PHP.
You were pretty close. If the value coming from PHP is not of a date/time, how could VFP interpret it properly. VFP also has a function CTOT() (character to time), and expects it in the format of 'yyyy-mm-ddThh:MM:ss??'
yyyy = 4 digit year
mm = 1 OR 2 digit month
dd = 1 OR 2 digit day
T -- literally the letter "T"
hh = 1 OR 2 digit hour (but typical is 2 anyhow)
MM = 1 or 2 digit minute (but typical is 2)
ss = 1 or 2 digit for seconds -- not required
?? = "AM" or "PM" if you wanted to explicitly provide that vs 24 hour clock
The MM and ss are optional, so if you finished with "T1" would be 1:00:00am
Now, to finish your query.
WHERE DATETIME(invDate+invTime)
BETWEEN DATETIME('2013-12-17 17:58') AND DATETIME()
Since this appears to be querying all invoices between a given date/time and NOW (via DateTime()), you don't even need between, you can do
WHERE YourTable.Column > CTOT( '2013-12-17T17:58')
If you specifically DID have a date/time range to consider, THEN you could do something like
WHERE YourTable.Column BETWEEN CTOT( '2013-12-05T10:00') AND CTOT( '2013-12-14T11:58')
PROBLEMS WITH your DATE() and TIME() implementations
The problem is Date() is a function to either return current date, or create based on y/m/d provided such as date( 2013, 12, 7 ). If you are passing a string, use CTOD( 'mm/dd/yyyy' ) such as CTOD( 12, 7, 2013 ).
As for the TIME() function that just expects a number and is of no use for you. From the OleDbProvider, your best bet is to just create a php function that builds a single string in the CTOT() format I've described and pass to the php function the date and time fields. Then use that as your "CTOT( functionReturnResult )"
To add a Date and a Time Field together you will need to convert them both to a same datatype 1st and than just simply add them together something like this....
DECLARE #D DATE = '2013-12-17'
DECLARE #T TIME = '17:58:00'
SELECT CAST(#D AS DATETIME) + CAST(#T AS DATETIME)
Result
2013-12-17 17:58:00.000
Your Query
SELECT top 10 *
from invoices
WHERE CAST(invDate AS DATETIME) + CAST(invTime AS DATETIME)
BETWEEN '20131217 17:58:00.000' AND GETDATE()

Parsing a time span string in sql

I am reading in from a datasource that is giving me time in a varchar in the format d:h:m:s:f, The day portion is always 0, the column represents a time of day. I would like to add this column to a datetime I already have.
Entry_Date Entry_Time
3/3/2009 12:00:00 0:16:17:6:0
8/24/2011 12:00:00 0:8:39:18:0
9/4/2010 12:00:00 0:12:33:18:0
If I was using C# I would just do TimeSpan.ParseExact but I do not know how to handle this in a purely sql fashion.
I would cast to time but I am using Sql Server 2005 and that does not have the time type.
How would I add the time to the neighboring datetime?
As simple as this:
select Entry_Date + cast(Entry_Time as datetime) combinedCol
from YourTable
Here is a working fiddle: http://sqlfiddle.com/#!3/85c3c/1
Enjoy!
Original Asker's note:
SQL can not handle the fractional seconds however they can be trimed off so the query would be
select Entry_Date + cast(left(Entry_Time, len(Entry_Time) - 2) as datetime) combinedCol
from YourTable