Dates & Time in SQL

Date & Time in the context of data is a powerful source we can leverage to interpret. Now it could also be a tremendous amount of confusion.

You can quickly see how these discrepencies and variations can lead to frequent confusion. Luckily, the environment of the database allows us to interrogate it and ask what the server time is. The command to do so is the following @@global & @@session.

Timezone querying

There are keywords following @@ symbols, @@global.time_zone & @@session.time_zone will query for the global & local session timezones respectively. They both will normally depend on the local operating system. Global will specify the whole database's timezone handling. The session depends on the session and can differ between databases & views.

Timezone Functions

Due to the importance of handling date time & its inherent complexity, there are a bunch of different timezone functions:

SELECT now();
SELECT YEAR(now());
SELECT MONTH(now());
SELECT DAY(now());
SELECT TIME(now());
SELECT HOUR(now());
SELECT MINUTE(now());
SELECT SECOND(now());

You have functions like NOW() that will return a current date/time stamp. Then you can select from date/time stamps specific units of time like YEAR,DAY,MINUTE, etc. Many others exist that will be listed here.

Time & Date Functions

References

Web Links

Note Links

Referenced By