Does SQL has some standard data-time function cross database? - sql

Does SQL has some standard data-time function cross database? Such as :
extract year, month, day, hour, minute or second
format to specific formatter
parse from string

I believe the answers are yes, no, and no.
The extraction functions are extract(<whatever> from date). I don't think there is a standard for parsing and formatting. However, to_char() and to_date() are used across multiple databases.

Related

What is the alternative of datefromparts in Presto (AWS Athena)

What is the best performance alternative of datefromparts SQL function in AWS Athena (Presto DB)?
The use case is:
I have the date parts (i.e. the day, month, and year) and need the date from these.
You would typically use parse_date(), with the proper format specifiers. If your date is in ISO format, you can directly use from_iso_date() (or from_iso_timestamp()).
On the other hand, if you need to extract dates part, you can use extract(), like:
extract(hour from current_timestamp)
Note that Presto also offers a full range of short function name that correspond to the possible extraction parts: year(), quarter(), month(), ...

Difference between TO_[fill in the blank]() and CAST() or CONVERT() in SQL

I'm taking an advanced SQL course in Oracle and we have been discussing various TO() functions for multiple modules. Things like TO_DATE(), TO_CHAR(), TO_TIMESTAMP(), etc.
We then learned about the CAST() function which seems to have the same purpose but for a large amount of applications.
What's the difference between CAST() and the array of TO() functions?
To put it another way, when would I specifically want to use the former over the latter?
to_date, to_char, and to_timestamp are functions that exist in the Oracle database. convert is a function that exists in SQL Server. SQL Server doesn't have a to_date, to_char, or to_timestamp. Oracle doesn't have a convert*. Different databases will have different conversion functions.
cast is an ANSI standard function so it will basically exist everywhere. But it will generally be less flexible. You can't specify a format mask for example.
Technically, as #a_horse_with_no_name points out, Oracle does have a convert function. It just has nothing to do with converting data from one data type to another so it's completely unrelated to the SQL Server function.

What is the purpose of these number signs around date value type?

Can anyone explain the correct usage/requirement of using these '#' number signs. Do they simply have to surround any date type in SQL?
Thanks
SELECT * FROM Orders
OrderDate BETWEEN #07/04/1996# AND #07/09/1996#;
These are the MS Access syntax for date constants.
In most other databases, you just use a string to represent a date. In my opinion, you should use one of the ISO standard formats for this (either YYYYMMDD or my preference YYYY-MM-DD). So a valid date in most databases would be '2014-01-01'. In Access, this can be written #2014-01-1#.
I suppose that is a vendor specific SQL modification. If I am not mistaken, that is specific for MS Access databases that the date should be delimited with a hash.

Number of days between two dates - ANSI SQL

I need a way to determine the number of days between two dates in SQL.
Answer must be in ANSI SQL.
ANSI SQL-92 defines DATE - DATE as returning an INTERVAL type. You are supposed to be able to extract scalars from INTERVALS using the same method as extracting them from DATEs using – appropriately enough – the EXTRACT function (4.5.3).
<extract expression> operates on
a datetime or interval and returns an
exact numeric value representing the
value of one component of the datetime
or interval.
However, this is very poorly implemented in most databases. You're probably stuck using something database-specific. DATEDIFF is pretty well implemented across different platforms.
Here's the "real" way of doing it.
SELECT EXTRACT(DAY FROM DATE '2009-01-01' - DATE '2009-05-05') FROM DUAL;
Good luck!
I can't remember using a RDBMS that didn't support DATE1-DATE2 and SQL 92 seems to agree.
I believe the SQL-92 standard supports subtracting two dates with the '-' operator.
SQL 92 supports the following syntax:
t.date_1 - t.date_2
The EXTRACT function is also ANSI, but it isn't supported on SQL Server. Example:
ABS(EXTRACT(DAY FROM t.date_1) - EXTRACT(DAY FROM t.date_2)
Wrapping the calculation in an absolute value function ensures the value will come out as positive, even if a smaller date is the first date.
EXTRACT is supported on:
Oracle 9i+
MySQL
Postgres

Sql Ansi to manage DateTime values

I'm developing a multi-database system.
I want the difference between two dates in seconds.
In SQL Server I got:
DATEDIFF(second,stardate,enddate)
In MySql:
TIME_TO_SEC(TIMEDIFF(stardate,enddate))
My question:
Does Sql Ansi have functions to manage DateTime values? i.e.: There are datetime functions generic for all databases?
According to SQL:1999, date1-date0 should give you a value of type INTERVAL, a struct from which you should be able to extract YEAR, MONTH, DAY, etc.
I've never used it and I don't think it's widely supported (though I may not be up-to-date). If you're doing time arithmetic in the database layer and you want to be cross-DBMS compatible the usual solution is simply to use integer timestamps (of whatever resolution, but Unix time is common) and plain old integer arithmetic which is completely reliable cross-platform.