- 1. Basics
- How to Compare Arrays in PostgreSQL
- How to Concatenate Strings in PostgreSQL
- How to Convert the Case of a String
- How to Create an Array
- How to Insert Array Data Into a Table
- How to Insert Data Into an Array
- How to Modify Arrays
- How to Query Arrays
- How to Replace Substrings
- How to Trim Strings
- How to Use string_agg()
- How to Use substring()
- How to Use substring() with Regular Expressions
- How to Insert
- How to Update
- How to Delete
- 2. Database Management
- How to Create a Table
- How to Drop a Table
- How to Rename a Table
- How to Truncate a Table
- How to Duplicate a Table
- How to Add a Column
- How to Drop a Column
- How to Rename a Column
- How to Add a Default Value to a Column
- How to Remove a Default Value From a Column
- How to Add a Not Null Constraint
- How to Remove a Not Null Constraint
- How to Create an Index
- How to Drop an Index
- How to Create a View
- How to Drop a View
- How to Alter Sequence
- 3. Dates And Time
- How to Exclude Current or Partial Weeks
- How to Use BETWEEN Correctly
- How to Query Date and Time
- How to Group by Time
- How to Round Timestamps
- How to Convert Local Time to UTC
- 4. Analysis
- How to Use Lateral Joins
- How to Use nullif()
- How to Get the First Row per Group
- How to Use generate_series to Avoid Gaps In Data
- How to Do Type Casting
- How to Write a Common Table Expression
- How to Import a CSV
- How to Compare Two Values When One Is Null
- How to Use Coalesce
- How to Write a Case Statement
- How to Query a JSON Column
- How to Use Filter Clause to Have Multiple Counts
- How to Calculate Cumulative Sum-Running Total
- 1. Basics
- How to Concatenate Strings in MS-SQL Server
- How to Convert the Case of a String
- How to replace a Substring in a String
- How to Trim Strings
- How to Use string_agg()
- How to Use substring()
- How to Insert
- How to Update
- How to Delete
- 2. Database Management
- How to Create a Table in MS-SQL
- How to Drop a Table in MS-SQL
- How to Rename a Table in MS-SQL
- How to Truncate a Table in MS-SQL
- How to Duplicate a Table in MS-SQL
- How to Add a Column in MS-SQL
- How to Drop a Column in MS-SQL
- How to Rename a Column in MS-SQL
- How to Add a Default Value to a Column in MS-SQL
- How to Remove a Default Value From a Column in MS-SQL
- How to Add a Not Null Constraint in MS-SQL
- How to Remove a Not Null Constraint in MS-SQL
- How to Create an Index in MS-SQL
- How to Drop an Index in MS-SQL
- How to Create a View in MS-SQL
- How to Drop a View in MS-SQL
- How to Alter Sequence in MS-SQL
- 3. Dates And Time
- How to Exclude Current or Partial Weeks
- How to Use BETWEEN Correctly
- How to Query Date and Time in MS-SQL
- How to Group by Time
- How to Extract a Component From a Datetime
- 4. Analysis
- How to Use Lateral Joins
- How to Use nullif()
- How to Get the First Row per Group
- How to Do Type Casting in MS-SQL
- How to Write a Common Table Expression
- How to Import a CSV
- How to Use Coalesce
- How to Write a Case Statement
- How to Query a JSON Column
- How to Calculate Cumulative Sum-Running Total
- 1. Basics
- How to Concatenate Strings in MySQL
- How to Convert the Case of a String
- How to replace a Substring in a String
- How to Trim a String
- How to use group_concat()
- How to do you Use a substring()
- How to Use substring() with Regular Expressions
- How to use insert into statement
- How to use Update statement in SQL
- How to use Delete SQL Statement
- 2. Database Management
- How to Create a Table in MySQL
- How to Drop a Table in MySQL
- How to Rename a Table in MySQL
- How to Truncate a Table in MySQL
- How to Duplicate a Table in MySQL
- How to Add a Column in MySQL
- How to Drop a Column in MySQL
- How to Rename a Column in MySQL
- How to Add a Default Value to a Column in MySQL
- How to Remove a Default Value From a Column in MySQL
- How to Add a Not Null Constraint in MySQL
- How to Remove a Not Null Constraint in MySQL
- How to Create an Index in MySQL
- How to Drop an Index in MySQL
- How to Create a View in MySQL
- How to Drop a View in MySQL
- How to Alter Sequence in MySQL
- 3. Dates And Time
- How to Exclude Current or Partial Weeks
- How to Use BETWEEN Correctly
- How to Query Date and Time in MySQL
- How to Group by Time
- How to Round Timestamps
- 4. Analysis
- How to Use nullif()
- How to Get the First Row per Group
- How to Do Type Casting in MySQL
- How to Write a Common Table Expression
- How to Import a CSV
- How to Use Coalesce
- How to Write a Case Statement
- How to Query a JSON Column
- How to Calculate Cumulative Sum-Running Total
- 1. Basics
- How to Concatenate Strings in Oracle
- How to Convert the Case of a String
- How to Replace Substrings
- How to Trim Strings
- How to Use listagg()
- How to Use substring()
- How to Use substring() with Regular Expressions
- How to Insert
- How to Update
- How to Delete
- 2. Database Management
- How to Create a Table
- How to Drop a Table
- How to Rename a Table
- How to Truncate a Table
- How to Duplicate a Table
- How to Add a Column
- How to Drop a Column
- How to Rename a Column
- How to Add a Default Value to a Column
- How to Remove a Default Value From a Column
- How to Add a Not Null Constraint
- How to Remove a Not Null Constraint
- How to Create an Index
- How to Drop an Index
- How to Create a View
- How to Drop a View
- How to Alter Sequence
- 3. Dates And Time
- How to Exclude Current or Partial Weeks
- How to Use BETWEEN Correctly
- How to Query Date and Time
- How to Group by Time
- How to Round Timestamps
- 4. Analysis
- How to Use nullif()
- How to Get the First Row per Group
- How to Do Type Casting
- How to Write a Common Table Expression
- How to Import a CSV
- How to Use Coalesce
- How to Write a Case Statement
- How to Query a JSON Column
- How to Use Filter Clause to Have Multiple Counts
- How to Calculate Cumulative Sum-Running Total
- 1. Basics
- How to Concatenate Strings in BigQuery
- How to Convert the Case of a String
- How to replace a Substring in a String
- How to Trim a String
- How to Use string_agg()
- How to use substring() function
- How to Use substring() with Regular Expressions
- How to Use BETWEEN Correctly
- How to use insert into statement
- How to use Update statement in SQL
- How to use Delete SQL Statement
- 2. Database Management
- How to Create a Database in BigQuery
- How to Create a Table in BigQuery
- How to Drop a Table in BigQuery
- How to Rename a Table in BigQuery
- How to Truncate Table in BigQuery
- How to Duplicate a Table in BigQuery
- How to Add a Column in BigQuery
- How to Drop a Column in BigQuery
- How to Add a Default Value to a Column in BigQuery
- How to Add a Not Null Constraint in BigQuery
- How to Remove a Not Null Constraint in BigQuery
- How to Create a View in BigQuery
- How to Drop a View in BigQuery
- 3. Dates And Time
- How to Exclude Current or Partial Weeks
- How to Query Date and Time
- How to Group by Time
- How to Round Timestamps
- 4. Analysis
- How to Use nullif()
- How to Get the First Row per Group
- How to Do Type Casting
- How to Write a Common Table Expression
- How to Compare Two Values When One Is Null
- How to Use Coalesce
- How to Write a Case Statement
- How to Calculate Cumulative Sum-Running Total
SQL Date and Time Functions and Data Types
- 1. Basics
- How to Compare Arrays in PostgreSQL
- How to Concatenate Strings in PostgreSQL
- How to Convert the Case of a String
- How to Create an Array
- How to Insert Array Data Into a Table
- How to Insert Data Into an Array
- How to Modify Arrays
- How to Query Arrays
- How to Replace Substrings
- How to Trim Strings
- How to Use string_agg()
- How to Use substring()
- How to Use substring() with Regular Expressions
- How to Insert
- How to Update
- How to Delete
- 2. Database Management
- How to Create a Table
- How to Drop a Table
- How to Rename a Table
- How to Truncate a Table
- How to Duplicate a Table
- How to Add a Column
- How to Drop a Column
- How to Rename a Column
- How to Add a Default Value to a Column
- How to Remove a Default Value From a Column
- How to Add a Not Null Constraint
- How to Remove a Not Null Constraint
- How to Create an Index
- How to Drop an Index
- How to Create a View
- How to Drop a View
- How to Alter Sequence
- 3. Dates And Time
- How to Exclude Current or Partial Weeks
- How to Use BETWEEN Correctly
- How to Query Date and Time
- How to Group by Time
- How to Round Timestamps
- How to Convert Local Time to UTC
- 4. Analysis
- How to Use Lateral Joins
- How to Use nullif()
- How to Get the First Row per Group
- How to Use generate_series to Avoid Gaps In Data
- How to Do Type Casting
- How to Write a Common Table Expression
- How to Import a CSV
- How to Compare Two Values When One Is Null
- How to Use Coalesce
- How to Write a Case Statement
- How to Query a JSON Column
- How to Use Filter Clause to Have Multiple Counts
- How to Calculate Cumulative Sum-Running Total
- 1. Basics
- How to Concatenate Strings in MS-SQL Server
- How to Convert the Case of a String
- How to replace a Substring in a String
- How to Trim Strings
- How to Use string_agg()
- How to Use substring()
- How to Insert
- How to Update
- How to Delete
- 2. Database Management
- How to Create a Table in MS-SQL
- How to Drop a Table in MS-SQL
- How to Rename a Table in MS-SQL
- How to Truncate a Table in MS-SQL
- How to Duplicate a Table in MS-SQL
- How to Add a Column in MS-SQL
- How to Drop a Column in MS-SQL
- How to Rename a Column in MS-SQL
- How to Add a Default Value to a Column in MS-SQL
- How to Remove a Default Value From a Column in MS-SQL
- How to Add a Not Null Constraint in MS-SQL
- How to Remove a Not Null Constraint in MS-SQL
- How to Create an Index in MS-SQL
- How to Drop an Index in MS-SQL
- How to Create a View in MS-SQL
- How to Drop a View in MS-SQL
- How to Alter Sequence in MS-SQL
- 3. Dates And Time
- How to Exclude Current or Partial Weeks
- How to Use BETWEEN Correctly
- How to Query Date and Time in MS-SQL
- How to Group by Time
- How to Extract a Component From a Datetime
- 4. Analysis
- How to Use Lateral Joins
- How to Use nullif()
- How to Get the First Row per Group
- How to Do Type Casting in MS-SQL
- How to Write a Common Table Expression
- How to Import a CSV
- How to Use Coalesce
- How to Write a Case Statement
- How to Query a JSON Column
- How to Calculate Cumulative Sum-Running Total
- 1. Basics
- How to Concatenate Strings in MySQL
- How to Convert the Case of a String
- How to replace a Substring in a String
- How to Trim a String
- How to use group_concat()
- How to do you Use a substring()
- How to Use substring() with Regular Expressions
- How to use insert into statement
- How to use Update statement in SQL
- How to use Delete SQL Statement
- 2. Database Management
- How to Create a Table in MySQL
- How to Drop a Table in MySQL
- How to Rename a Table in MySQL
- How to Truncate a Table in MySQL
- How to Duplicate a Table in MySQL
- How to Add a Column in MySQL
- How to Drop a Column in MySQL
- How to Rename a Column in MySQL
- How to Add a Default Value to a Column in MySQL
- How to Remove a Default Value From a Column in MySQL
- How to Add a Not Null Constraint in MySQL
- How to Remove a Not Null Constraint in MySQL
- How to Create an Index in MySQL
- How to Drop an Index in MySQL
- How to Create a View in MySQL
- How to Drop a View in MySQL
- How to Alter Sequence in MySQL
- 3. Dates And Time
- How to Exclude Current or Partial Weeks
- How to Use BETWEEN Correctly
- How to Query Date and Time in MySQL
- How to Group by Time
- How to Round Timestamps
- 4. Analysis
- How to Use nullif()
- How to Get the First Row per Group
- How to Do Type Casting in MySQL
- How to Write a Common Table Expression
- How to Import a CSV
- How to Use Coalesce
- How to Write a Case Statement
- How to Query a JSON Column
- How to Calculate Cumulative Sum-Running Total
- 1. Basics
- How to Concatenate Strings in Oracle
- How to Convert the Case of a String
- How to Replace Substrings
- How to Trim Strings
- How to Use listagg()
- How to Use substring()
- How to Use substring() with Regular Expressions
- How to Insert
- How to Update
- How to Delete
- 2. Database Management
- How to Create a Table
- How to Drop a Table
- How to Rename a Table
- How to Truncate a Table
- How to Duplicate a Table
- How to Add a Column
- How to Drop a Column
- How to Rename a Column
- How to Add a Default Value to a Column
- How to Remove a Default Value From a Column
- How to Add a Not Null Constraint
- How to Remove a Not Null Constraint
- How to Create an Index
- How to Drop an Index
- How to Create a View
- How to Drop a View
- How to Alter Sequence
- 3. Dates And Time
- How to Exclude Current or Partial Weeks
- How to Use BETWEEN Correctly
- How to Query Date and Time
- How to Group by Time
- How to Round Timestamps
- 4. Analysis
- How to Use nullif()
- How to Get the First Row per Group
- How to Do Type Casting
- How to Write a Common Table Expression
- How to Import a CSV
- How to Use Coalesce
- How to Write a Case Statement
- How to Query a JSON Column
- How to Use Filter Clause to Have Multiple Counts
- How to Calculate Cumulative Sum-Running Total
- 1. Basics
- How to Concatenate Strings in BigQuery
- How to Convert the Case of a String
- How to replace a Substring in a String
- How to Trim a String
- How to Use string_agg()
- How to use substring() function
- How to Use substring() with Regular Expressions
- How to Use BETWEEN Correctly
- How to use insert into statement
- How to use Update statement in SQL
- How to use Delete SQL Statement
- 2. Database Management
- How to Create a Database in BigQuery
- How to Create a Table in BigQuery
- How to Drop a Table in BigQuery
- How to Rename a Table in BigQuery
- How to Truncate Table in BigQuery
- How to Duplicate a Table in BigQuery
- How to Add a Column in BigQuery
- How to Drop a Column in BigQuery
- How to Add a Default Value to a Column in BigQuery
- How to Add a Not Null Constraint in BigQuery
- How to Remove a Not Null Constraint in BigQuery
- How to Create a View in BigQuery
- How to Drop a View in BigQuery
- 3. Dates And Time
- How to Exclude Current or Partial Weeks
- How to Query Date and Time
- How to Group by Time
- How to Round Timestamps
- 4. Analysis
- How to Use nullif()
- How to Get the First Row per Group
- How to Do Type Casting
- How to Write a Common Table Expression
- How to Compare Two Values When One Is Null
- How to Use Coalesce
- How to Write a Case Statement
- How to Calculate Cumulative Sum-Running Total
SQL databases provide data types related to date and time data, as you learned in the SQL Data Types lesson. Now you can see the details of how these data types are related, how to create expressions involving different date data types and use some SQL date and time functions.
Here is the sample table used in the examples. Building on the apple example, a new table apple_sells
is added to the apple database. The apple_sells
table stores one record per each apple sales transaction.
The schema of the table is:
Customer Name | Apple Variety | Date of Sell | Tons Sold | Ton Price | Delivery date | Starting Freeze |
---|---|---|---|---|---|---|
SMart | Red Delicious | 2020-02-23 | 60 | 54.56 | 2020-02-28 | 2020-01-08 10:00 |
JB | Red Globus | 2020-03-02 | 35 | 61.50 | 2020-03-12 | 2020-01-15 09:30 |
JB | Magic Green | 2020-04-13 | 15 | 58.50 | 2020-04-22 | 2020-01-07 08:23 |
SMart | Red Globus | 2020-05-21 | 100 | 61.00 | 2020-05-28 | 2020-01-15 09:30 |
SMart | Red Delicious | 2020-05-21 | 90 | 54.56 | 2020-05-30 | 2020-01-08 10:00 |
The starting_freeze
column tracks when the apples entered into the refrigerator rooms. This information is provided to customers to meet food regulations.
Date Arithmetic
The SQL Data Types related to date and time are DATE
, TIMESTAMP
, TIME
and INTERVAL
. You can construct arithmetic operations or expressions involving different data types. Here are a few of the frequently used expressions:
Arithmetic operation | Result data type |
---|---|
TIMESTAMP + INTERVAL | TIMESTAMP |
TIMESTAMP - TIMESTAMP | INTERVAL |
INTERVAL + INTERVAL | INTERVAL |
INTERVAL * INTEGER | INTERVAL |
TIMESTAMP - INTERVAL | TIMESTAMP |
CAST(DATE, TIMESTAMP) + INTERVAL | TIMESTAMP |
CAST(DATE, TIMESTAMP) - TIMESTAMP | INTERVAL |
Suppose you need to inform the customers how long the apples have been in the refrigerator room:
SELECT customer_name,
apple_variety,
delivery_date,
CAST(delivery_date AS TIMESTAMP) - starting_freeze AS freeze_time
FROM apple_sells
To calculate the freeze_time
value:
- Convert
delivery_date
value from date to timestamp. This conversion is done by adding the time 00:00:00 to the date value. - Calculate the difference between
delivery_date
andstarting_freeze
. As both are timestamp values, the result is an interval data type value representing the elapsed time betweenstarting_freeze
anddelivery_date
.
Here is the result:
Customer Name | Apple Variety | Delivery Date | Freeze Time |
---|---|---|---|
SMart | Red Delicious | 2020-02-28 | 50 days 14:00:00 |
JB | Red Globus | 2020-03-12 | 56 days 14:30:00 |
JB | Magic Green | 2020-04-22 | 105 days 15:37:00 |
SMart | Red Globus | 2020-05-28 | 133 days 14:30:00 |
SMart | Red Delicious | 2020-05-30 | 142 days 14:00:00 |
State regulations for apples say Red Delicious must be consumed no later than 180 days since the starting freeze date and the other varieties at 240 days. To inform your customers of the deadline, you calculate the consume_deadline:
SELECT customer_name,
apple_variety,
delivery_date,
CAST(delivery_date, TIMESTAMP) - starting_freeze AS
time_stored_at_delivery,
starting_freeze +
case
when apple_variety = ‘Red Delicious’ then
INTERVAL 180 day
when apple_variety <> ‘Red Delicious’ then
INTERVAL 240 day
end AS deadline_to_consume
FROM apple_sells
The CASE statement handles the different apple varieties. The 180 days is set with the expression: INTERVAL 180 day
.
Here are the results:
customer_name | apple_variety | delivery_date | time_stored_at_delivery | deadline_to_consume |
---|---|---|---|---|
SMart | Red Delicious | 2020-02-28 | 50 days 14:00:00 | 2020-07-06 10:00:00 |
JB | Red Globus | 2020-03-12 | 56 days 14:30:00 | 2020-09-11 09:30:00 |
JB | Magic Green | 2020-04-22 | 105 days 15:37:00 | 2020-09-03 08:23:00 |
SMart | Red Globus | 2020-05-28 | 133 days 14:30:00 | 2020-09-11 09:30:00 |
SMart | Red Delicious | 2020-05-30 | 142 days 14:00:00 | 2020-07-06 10:00:00 |
Functions to Manipulate SQL Date and Time Data Type
There are several SQL date and time functions to extract, manipulate and transform date and time data types. These examples cover CURRENT_DATETIME()
, the function EXTRACT()
and the SQL dialect specific TO_CHAR()
function.
CURRENT_DATETIME()
returns the current date and time as a TIMESTAMP
data type. Simply put it in your SQL where you need the current date and time.
The function EXTRACT()
enables you to extract an element from a date or timestamp value. For example, you can extract the year from a date value, or the hour from a timestamp value. Here is EXTRACT()
function syntax:
EXTRACT (FIELD FROM SOURCE)
Suppose you want to extract the month and day from the starting_freeze time for the year 2018:
SELECT ‘on ‘starting_freeze,
EXTRACT(MONTH FROM starting_freeze) AS month_freeze,
EXTRACT(DAY FROM starting_freeze) AS day_freeze
FROM apple_sells
WHERE EXTRACT(YEAR FROM starting_freeze ) = 2018
Here are the results:
starting_freeze | month_freeze | day_freeze |
---|---|---|
2020-01-08 10:00:00 | 1 | 8 |
2020-01-15 09:30:00 | 1 | 15 |
2020-01-07 08:23:00 | 1 | 7 |
2020-01-15 09:30:00 | 1 | 15 |
2020-01-08 10:00:00 | 1 | 8 |
Other date functions are oriented to date formatting. One popular example is the TO_CHAR()
function. This is a SQL dialect specific function, but is provided by all databases.
Here is an example of the Postgresql TO_CHAR()
function:
TO_CHAR ( EXPRESSION , FORMAT)
The expression can be a date, timestamp, or interval that is converted to a string according to the specified format. You want to show starting_freeze in a more readable format: Refrigerated at: 09:30:00 on Jan-15-2020.
Here is the query:
SELECT ‘Refrigerated at ‘||
TO_CHAR( starting_freeze, ‘HH24:MI:SS’)||
‘ on ‘||
TO_CHAR( starting_freeze, ‘Mon-DD-YYYY’) AS freezing_date
FROM apple_sells
Here are the results:
Freezing Date |
---|
Refrigerated at 10:00:00 on Jan-08-2020 |
Refrigerated at 09:30:00 on Jan-15-2020 |
Refrigerated at 08:23:00 on Jan-07-2020 |
Refrigerated at 09:30:00 on Jan-15-2020 |
Refrigerated at 10:00:00 on Jan-08-2020 |
Closing Words
In this lesson we learned several date expressions, functions, and interesting date arithmetic expressions where the result data type is different from the operators data types. Another function manipulated and formatted the date and timestamp values. In the next lesson covers interesting string related functions. Keep going, learn SQL
and increase your skills!
IN THIS PAGE