SAS timestamp from scientific notation to yyyy/mm/dd hh:mm:ss - sql

Problem:
My timestamp is being displayed in scientific notation. I would like to display the column without scientific notation, and create a second column formatted as a long date, yyyy/mm/dd hh:mm:ss.
Steps taken:
I've already converted the column from a UNIX Epoch (1960) timestamp to SAS time (1970) timestamp. But scientific notation persists. I tried date20. doesn't do the trick, either.
Timestamp in Scientific Notation
My current insufficient code fails to format the timestamp column as a date.
proc print data=heart._23a;
format timestamp date9.;
run;
Results:
It results in no errors, but it redimensions my matrix to a 1x3. I need to obtain a matrix of the same dimension, just with a reformatted timestamp. I appreciate any help, but please keep it simple, I am in unknown territory!

datetime17. is the standard timestamp format in SAS, though you have many other choices as well. ymddttm. is the closest to what you're looking for, I believe.
One important distinction here: SAS has two concepts, date and datetime. date is number of days since 1/1/1960 and has no time part, while datetime is number of seconds since 1/1/1960 00:00:00 and has both time and date. You can use datepart to convert datetime -> date, or dhms to convert date -> datetime.
Your question also seems to get the two epochs backwards. UNIX epoch is 1970. SAS epoch is 1960.
Finally, if you want to display the raw number of seconds, use w.d format instead of bestw.d format - format timestampvar 14. for example, where w is number of characters (digits) wide total including decimal.

Related

What is expected input date pattern for date_format function in databricks spark SQL

I am trying to better understand the date_format function offered by Spark SQL.As per the official databricks documentation (I am using databricks), this function expects any date/ string in a valid datetime format. Below is the link for the same.
I am finding it difficult to understand what is the exact definition of "valid" here. I am trying to understand the functionality through two examples here.
Input string in YYYY-MM-DD format (2021-07-09), for which I get the expected results correctly:
Input string in DD-MM-YYYY format (20-07-2021), and I get null:
Why is this happening? How did this function understand that the parameter that I am passing is indeed in YYYY-MM-DD format? It could also have been YYYY-DD-MM.
My requirement is that I implement a logic that could handle all kinds of valid date formats (MM-DD-YYYY, YYYY-MM-DD, DD-MM-YYYY) and format the dates accordingly.
The following is valid input and output formats for ANSI date/time data types:
Example: ANSIDATE yyyy-mm-dd 2007-02-28 TIME WITH TIME ZONE hh:mm:ss.ffff... [+|-]th:tm
The valid range of time zone offset is from -14:00 to +14:00. date complies with the ANSI SQL standard definition for the Gregorian calendar: "NOTE 85 - Datetime data types will allow dates in the Gregorian format to be stored in the date range 0001-01-01 CE through 9999-12-31 CE
See Databricks SQL datetime patterns for details on valid formats. The function checks that the resulting dates are valid dates in the Proleptic Gregorian calendar, otherwise it returns NULL
When you use "20-07-2021" it does not conform to "yyyy-mm-dd" so results in NULL
Alternately, you can use make_date function which Creates a date from year, month, and day fields. Or better use to_date function
select date_format(to_date('9/15/2021', 'MM/dd/yyyy'), 'yyyy/MM/dd')
See Datetime Patterns for Formatting and Parsing in Spark.

Converting timestamp on whole table in bigquery

I have this table which stores millions of rows of data. This data has a date that indicates when was the data entered. I store the data in NUMERIC schemas with EPOCH UNIX as the format. However, I wanted to convert them to human date (yyyy-mm-dd hh:mm:ss) and later sort them by date not queried date.
However, it took me so long to find a suitable way. Here's my attempt.
I used SELECT CAST(DATE(timestamp) AS DATE) AS CURR_DT FROM dataset.table but it gave me this error:
No matching signature for function DATE for argument types: NUMERIC. Supported signatures: DATE(TIMESTAMP, [STRING]); DATE(DATETIME); DATE(INT64, INT64, INT64) at [1:13]
I used this method BigQuery: convert epoch to TIMESTAMP but still didn't fully understand
I'm a novice in coding so I hope you guys understand the situation. Thanks!
If I am understanding your question correctly you would like to take a numeric EPOCH time that is stored as an integer and convert it to a timestamp?
If so you can use the following in BigQuery Standard SQL:
select TIMESTAMP_SECONDS(1606048220)
It gives the output of:
2020-11-22 12:30:20 UTC
Documentation
If you only want the date component, then you would convert to a date after converting to a timestamp. Presumably you have seconds, so you would use TIMESTAMP_SECONDS() -- but there are similar functions for milliseconds and microseconds.
For just the date:
select date(timestamp_seconds(col))
Note that this removes the time component.

Convert DOUBLE column to TIMESTAMP in Firebird database

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.

How to use datediff equivalent in Oracle with YYYYMMDD formatted number?

I have Oracle database columns with the number format YYYYMMDD. I have not been successful in using this format with datediff to get the difference between two dates. The documentation I've read online uses a different format:
DATEDIFF(day,'2008-06-05','2008-08-05')
What's the best way for me to get number of days between two dates given the format available to me in Oracle? Answers not involving datediff are acceptable as long as it gets the number of days between two dates with the format YYYYMMDD.
Simple subtraction in Oracle:
SELECT TO_DATE('20080805','YYYYMMDD') - TO_DATE('20080605','YYYYMMDD')
FROM DUAL;
Oracle doesn't have a DATEDIFF() function. Instead, you can use simple arithmetic with Oracle dates, where subtracting one date from another gives the number of days, and where you can add an subtract days from a given date. (You can also subtract fractions of days, but that might be outside the scope of this answer.)
To convert your NUMBER dates of the format YYYYMMDD to actual dates, just use the TO_DATE() function (I am pretty sure that Oracle will implicitly convert the NUMBER value to a VARCHAR2 before converting to a date; if not, use TO_CHAR() to do that explicitly).
TO_DATE(20150301, 'YYYYMMDD')
To get the difference between two dates, you can do the following:
SELECT TO_DATE(my_number_date1, 'YYYYMMDD') - TO_DATE(my_number_date2, 'YYYYMMDD')
FROM my_table;
Incidentally, if you want to get intervals instead of days, convert to timestamp (using TO_TIMESTAMP()) instead of converting to date.

SQLite order by date

My date is not in standard form. This is how it is: 07/15/2013 06:53:05 and is stored as string in the database.
How can I query it to order it by date.
This query is not working.
SELECT jobno, ondate FROM Reports ORDER BY DATE(ondate)
When I run this query it orders it alphabetically and not date wise.
As written here, SQLite doesn't have a date type, so you can do this:
SELECT jobno, ondate
FROM Reports
ORDER BY substr(ondate,7)||substr(ondate,1,2)||substr(ondate,4,2)
sql fiddle demo
This is why this format is not a recommend date/time storage mechanism in SQLite.
You can use strftime to covert the data into something that can be well-ordered (note that we just have to specify the American-ish format string), e.g.
SELECT jobno, ondate FROM Reports
ORDER BY strftime('%m/%d/%Y %H:%M:%S', ondate)
However, this approach (as well as the approach in the other answer) will not be able to use indices and performance on large data may suffer!
From SQLite Datatypes: 1.2 Date and Time Datatype the advice is:
SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
I recommend using ISO 8601 as it's still human-readable, is well-understood (and culture neutral), can encode the TZ (just be consistent!), and can be indexed well.