Convert DOUBLE column to TIMESTAMP in Firebird database - sql

I have a Firebird database that saves the datetime field as a DOUBLE. I have created a ColdFusion datasource connection, so I can query the data remotely. While the rest of the data is being returned correctly, the datetime field is unreadable. I have tried using CAST and CONVERT to no avail. How can I convert this to a timestamp?
An example of the data stored is: 43016.988360

You can't just convert a DOUBLE PRECISION to a TIMESTAMP, not without explicitly defining how you want it mapped and writing that conversion yourself (or hoping there is an existing third-party UDF that does this for you).
A TIMESTAMP in Firebird is a date + time represented as an 8 byte value, where the date range is from January 1, 1 a.d. to December 31, 9999 a.d. and the time range is 00:00 to 23:59.9999 (so, 100 microsecond precision).
A DOUBLE PRECISION is - usually - the wrong type for storing date and time information, and as you haven't provided how that double value should be interpreted, we can't help you other than saying: there is no default method in Firebird to do this.
Based on the comments below, it looks like the value is a ColdFusion date value stored as double precision with the number of days since December 30th 1899, see also why is ColdFusion's Epoch Time Dec 30, 1899?. If this is really the case, then you can use the following for conversion to a TIMESTAMP:
select timestamp'1899-12-30 00:00' + 43016.988360 from rdb$database
Which will yield the value 2017-10-08 23:43:14.304. Using the value 43182.4931754 from the comments will yield 2018-03-23 11:50:10.354. That is a millisecond off from your expectation, but that might be a rounding/presentation issue, eg I get the exact expected date if I use 43182.49317539 instead.
I would strongly suggest you carefully test this with known values.

Related

SQLite strftime() returning null with integers

I have a date with the column name "date" in my table "productions" stored as integer with Unix format ( example: 1548263300000). And I want to retrieve only the year. When I do:
SELECT strftime('%Y ',date) as year
FROM productions
it returns null.
When I change the type to TEXT into my table and I store the date in string format ( example 2020-05-01 ), the same sql returns me "2020" which is correct and what I was looking for.
Why strftime() doesn't work with integers since the SQLite documentation say you can work with TEXT,INTEGER and REAL for dates? How to use date functions with integers?
extra information:
In this tutorial, they also use strftime with integers and it seems to work for them, so I understand from that, that the functions are available no matter what type you use ( text,int,real): link
when I use:
SELECT strftime('%Y',DATETIME(ROUND(date/ 1000), 'unixepoch'))
FROM productions;
it works fine, but I don't understand why I have to do all this when I use integers but when I use text, it works directly.
You can use strftime() but you have to add the 'unixepoch' modifier:
strftime('%Y', date / 1000, 'unixepoch')
so your date / 1000 is recognized as the number of seconds since 1970-01-01.
From Date And Time Functions:
The "unixepoch" modifier (11) only works if it immediately follows a
timestring in the DDDDDDDDDD format. This modifier causes the
DDDDDDDDDD to be interpreted not as a Julian day number as it normally
would be, but as Unix Time - the number of seconds since 1970.

How to convert UNIX epoch days to DATE in Snowflake?

I have some values which correspond to the number of days since Epoch and want to store them in DATE column in Snowflake.
Example:
33057
Corresponds to Sunday, July 4, 2060
Would be really helpful if someone can point me to the correct Snowflake documentation(if available out of the box) for such
I think this will work:
select dateadd(day, <your number> - 33057, '2060-07-04')
So, for the specific conversion of "33057", this would be:
select dateadd(day, 33057 - 33057, '2060-07-04')
But it should work for any integer.
TO_TIMESTAMP_x(numeric_expr) is what your looking for. And if you only want the date part I would then truncate via ::DATE
numeric_expr
A number of seconds (if scale = 0 or is absent) or fractions of a second (e.g. milliseconds or nanoseconds) since the start of the Unix epoch (1970-01-01 00:00:00 UTC). If a non-integer decimal expression is input, the scale of the result is inherited.
if you have milliseconds you can use the TO_TIMESTAMP_x(numeric_expr, scale) form
select to_timestamp(12334567) as t_from_s
,to_timestamp(12334567000, 3) as t_from_ms;
gives
T_FROM_S T_FROM_MS
1970-05-23 18:16:07.000 1970-05-23 18:16:07.000
We use the _NTZ variant for all our usages, as we have all data in UTC from the servers.

CAST vs ssis data flow implicit conversion difference

I have a SSIS package which transfers some data from Oracle to SQL Server.
In Oracle dates are stored as float, e.g. 42824 == '2017-04-01' - application which uses the database is written in Delphi.
While select CAST(42824 as datetime)
in Management Studio results in '2017-04-01 00:00:00.000', the same value (42824) inserted by package into datetime column in SQL Server table shows 2017-03-30 00:00:00.000.
Note: Source data type for this number is DT_R8, changing the type to DT_UI4 in Data Conversion component changes nothing
Can anyone explain this?
About date serials
The value stored in Oracle (42824) is known as date serial , it is also used in Microsoft Excel.
Date Serial represents the number of Days between the date value and the initial value that is 1899-12-30
You can Read more about Date Serials at:
Why is 1899-12-30 the zero date in Access / SQL Server instead of 12/31?
convert Excel Date Serial Number to Regular Date
CAST method
From Microsoft Docs - CAST and CONVERT (Transact-SQL):
Only supported when casting from character data to datetime or smalldatetime. When character data that represents only date or only time components is cast to the datetime or smalldatetime data types, the unspecified time component is set to 00:00:00.000, and the unspecified date component is set to 1900-01-01
So CAST function consider the value 1900-01-01 as an initial value when casting dates. So we need to subtract 2 days when using it to convert Date Serials
There are 2 ways to convert it to date using SQL Server:
select DATEADD(d,42824,'1899-12-30')
select CAST(36464 - 2 as SmallDateTime)
SSIS Implicit conversion
Also according to this Microsoft docs article
DBTYPE_DATE (This is an automation DATE type. It is internally represented as a double.. The whole part is the number of days since December 30, 1899 and the fractional part is the fraction of a day. This type has an accuracy of 1 second, so has an effective scale of 0.)
So implicit conversion in SSIS consider the value 1899-12-30 as an initial value when casting dates. So there is no need to subtract 2 days when using it to convert Date Serials

IBM i Date Diff with CYYMMDD - can't use DATE()

(title edited)
Good afternoon, all!
Using IBM i version 7.1 and looking to calculate difference between two dates in a query. Since nothing is ever easy, one date is in CYYMMDD format, the other (curdate()) is YYYY-MM-DD. I tried to CAST my CYYMMDD formatted date (field name APENGD) as a varchar(10) then wrapped that in a CAST as a date (since decimals can't be CASTed as dates):
Cast(Cast(APENGD + 19000000 As varchar(10)) As date) As math
but I only see a result ++++++++++++++ for whatever reason. I was able to test a few different versions of this and found I can't use DATE anywhere...can anyone suggest an alternative??
Thanks in advance!
Matt
casting varchar to date only works when the string includes separators.
At 7.1 you could use TIMESTAMP_FORMAT(), but you'd end up with a timestamp instead of just a date. But that's easily dealt with.
Date(Timestamp_format(char(APENGD + 19000000),'YYYYMMDD')) As math
My prefered solution when dealing with numeric/character value dates is creating a User Defined Function to handle conversion.
You could write your own, or use the one I do. iDate written by Alan Campin. Then your code would simple be:
idate(APENGD,'*CYMD') as nath
Note that if you're trying to use date differences in a WHERE clause, like so
WHERE CURRENT_DATE - 3 months <= idate(APENGD,'*CYMD')
The above will perform poorly since an existing index over APENGD can't be used (directly). Assuming a recent(6.1+) version of the OS, you can create a new index that includes the expression you're using to convert APENGD to date.
Or you could code it using the Date->Numeric function ConvertToIdate that Alan helpfully includes. That would allow existing indexes to be used.
WHERE ConvertToiDate(CURRENT_DATE - 3 months,'*CYMD') <= APENGD
The DDL was not offered [to define the column APENGD]. No matter, as the following should suffice, mostly irrespective the definition; either as a string or as a zero-scale numeric. The effect depends on the SQL recognition of a 14-character [up to 26-character, since some v7 release] character-string as an unformatted [i.e. lacking any delimiters, thus digits-only] TIMESTAMP representation:
date(timestamp((APENGD + 19000000) concat '000000'))
IBM i 7.3->Database->Reference->SQL reference->Language elements->Data types->Datetime values->String representations of datetime values->Timestamp strings
A string representation of a timestamp is a character or a Unicode graphic string that starts with a digit and has a length of at least 14 characters. …
If you want calculate difference between 2 dates, you can use:
`TIMESTAMPDIFF(32, cast(MYTIMESTAMP1 - MYTIMESTAMP2 as char(22)))`
The first argument of function specify the type of result.
1 : millisecond
16 : days
2 : second
32 : week
4 : minutes
64 : month
8 : hour
128 : trimester
256 : Year

character_length Teradata SQL Assistant

I have to run column checks for data consistency and the only thing that is throwing off my code is checking for character lengths for dates between certain parameters.
SEL
sum(case when ( A.date is null or (character_length(A.date) >8)) then 1 else 0 end ) as Date
from
table A
;
The date format of the column is YYYY-MM-DD, and the type is DA. When I run the script in SQL Assistant, I get an error 3580 "Illegal use of CHARACTERS, MCHARACTERS, or OCTET_LENGTH functions."
Preliminary research suggests that SQL Assistant has issues with the character_length function, but I don't know how to adjust the code to make it run.
with chareter length are you trying to get the memory used? Becuase if so that is constant for a date field. If you are trying to get the length of the string representation i think LENGTH(A.date) will suffice. Unfortanatly since teradata will pad zeros on conversions to string, I think this might always return 10.
UPDATE :
Okay so if you want a date in a special 'form' when you output it you need to select it properly. In teradata as with most DBs Date are not store in strings, but rather as ints, counting days from a given 'epoch' date for the database (for example the epoch might be 01/01/0000). Each date type in teradata has a format parameter, which places in the record header instructions on how to format the output on select. By default a date format is set to this DATE FROMAT 'MM/DD/YYYY' I believe. You can change that by casting.
Try SELECT cast(cast(A.date as DATE FORMAT 'MM-DD-YYYY') as CHAR(10)) FROM A. and see what happens. There should be no need to validate the form of the dates past a small sample to see if the format is correct. The second cast forces the database to perform the conversion and use the format header specified. Other wise what you might see is the database will pass the date in a date form to SQL Assitant and sql assitant will perform the conversion on the application level, using the format specified in its own setting rather then the one set in the database.