Icon Crear Crear

Mastering SQL Datetime functions

Completar frases

Drills to master SQL datetime functions

Descarga la versión para jugar en papel

0 veces realizada

Creada por

Estados Unidos

Top 10 resultados

Todavía no hay resultados para este juego. ¡Sé el primero en aparecer en el ranking! para identificarte.
Crea tu propio juego gratis desde nuestro creador de juegos
Compite contra tus amigos para ver quien consigue la mejor puntuación en esta actividad

Top juegos

  1. tiempo
    puntuacion
  1. tiempo
    puntuacion
tiempo
puntuacion
tiempo
puntuacion
 
game-icon

Completar frases

Mastering SQL Datetime functionsVersión en línea

Drills to master SQL datetime functions

por Good Sam
1

LENGTH TRIM REPLACE SUBSTRING CONCAT

Problem 1 : String Functions
First , a brief touch on essential string functions that are particularly useful in data engineering :

( ) : Merges two or more strings into one .
( ) : Extracts a substring from a string .
( ) : Replaces all occurrences of a substring within a string .
( ) : Removes spaces from the beginning and end of a string .
( ) : Returns the length of a string .

These functions help in cleaning and preparing data for further analysis .

2

AS SELECT FROM AS orders DATE order_date_str CAST order_date

Problem 2 : Convert String to Date
Question : You have a date stored as a string 2024 - 04 - 12 in the orders table under the column order_date_str . Write a query to convert this to a DATE data type .

Solution :

( )
;

SELECT CAST ( order_date_str AS DATE ) AS order_date
FROM orders ;

Explanation : CAST ( ) is used to convert the string to a DATE type . This is essential for performing date - specific operations that aren't possible on strings .

3

table_name AS YEAR DATE date_string MONTH EXTRACT EXTRACT AS CAST FROM AS CAST AS FROM DATE FROM date_string month SELECT year

Problem 3 : Extract Year and Month from Date String

Question : Given a DATE string 2024 - 04 - 12 , extract the year and month separately .

Solution :

( ( ) ) ,
( ( ) )
;


SELECT EXTRACT ( YEAR FROM CAST ( date_string AS DATE ) ) AS year ,
EXTRACT ( MONTH FROM CAST ( date_string AS DATE ) ) AS month
FROM table_name ;

Explanation : EXTRACT ( ) function is used to pull specific parts from a date value , here extracting year and month after casting the string to a date .

4

TO_CHAR FROM DATE CAST 'DD-Mon-YY' SELECT formatted_date AS AS date_string table_name

Problem 4 : Date Formatting
Question : Convert a DATE from YYYY - MM - DD format to DD - Mon - YY format in SQL .

Solution :

( ( ) , )
;



SELECT TO_CHAR ( CAST ( date_string AS DATE ) , 'DD - Mon - YY' ) AS formatted_date
FROM table_name ;

Explanation : TO_CHAR ( ) function converts dates into specified string formats , enhancing readability and formatting for reporting purposes .

5

INTERVAL '3 months' SELECT dual new_date date '2024-04-12' AS FROM

Problem 5 : Adding Intervals to Dates
Question : Add 3 months to a date 2024 - 04 - 12 .

Solution :

+
;



SELECT date '2024 - 04 - 12' + INTERVAL '3 months' AS new_date
FROM dual ;

Explanation : The INTERVAL keyword is used to specify a period to be added to a date , useful for calculating future or past dates relative to a known date .

6

date FROM days_difference SELECT dual '2024-04-12' AS '2024-05-15' date

Problem 6 : Difference Between Dates
Question : Calculate the number of days between two dates , 2024 - 04 - 12 and 2024 - 05 - 15 .

Solution :

-
;



SELECT date '2024 - 05 - 15' - date '2024 - 04 - 12' AS days_difference
FROM dual ;

Explanation : Subtracting one date from another directly results in the difference in days between them , crucial for duration or period calculations .

7

EXTRACT AS FROM AS day SELECT month EXTRACT FROM event_timestamp event_logs event_timestamp year event_timestamp EXTRACT FROM FROM YEAR EXTRACT HOUR SECOND AS WEEK MINUTE AS FROM MONTH AS EXTRACT AS FROM hour DAY event_timestamp EXTRACT FROM second week AS event_timestamp EXTRACT minute event_timestamp FROM event_timestamp event_timestamp

Problem 7 : Scenario with TIMESTAMP rather than DATE

Scenario :
Suppose you have a table named event_logs with a column event_timestamp of type TIMESTAMP which stores the date and time of when events occurred in the system .

Problem :
Extract the year , month , day , week number , hour , minute , and second from the event_timestamp for each event and display them along with the original timestamp .

Solution :
Here's an SQL query that demonstrates how to use these date and time functions :


,
( ) ,
( ) ,
( ) ,
( ) ,
( ) ,
( ) ,
( )

;



SELECT
event_timestamp ,
EXTRACT ( YEAR FROM event_timestamp ) AS year ,
EXTRACT ( MONTH FROM event_timestamp ) AS month ,
EXTRACT ( DAY FROM event_timestamp ) AS day ,
EXTRACT ( WEEK FROM event_timestamp ) AS week ,
EXTRACT ( HOUR FROM event_timestamp ) AS hour ,
EXTRACT ( MINUTE FROM event_timestamp ) AS minute ,
EXTRACT ( SECOND FROM event_timestamp ) AS second
FROM
event_logs ;
Explanation :
EXTRACT ( YEAR FROM event_timestamp ) : Extracts the year from the event_timestamp .
EXTRACT ( MONTH FROM event_timestamp ) : Extracts the month from the timestamp .
EXTRACT ( DAY FROM event_timestamp ) : Retrieves the day of the month from the timestamp .
EXTRACT ( WEEK FROM event_timestamp ) : Gets the week number of the year from the timestamp .
EXTRACT ( HOUR FROM event_timestamp ) : Extracts the hour of the day from the timestamp .
EXTRACT ( MINUTE FROM event_timestamp ) : Retrieves the minute from the timestamp .
EXTRACT ( SECOND FROM event_timestamp ) : Gets the second from the timestamp .

8

end_time SELECT end_time FROM end_time start_time EPOCH FROM end_time minutes_difference seconds EXTRACT events 3600 24 start_time)) % 3600 end_time FLOOR events 60 AS EXTRACT FROM start_time)) % 60 FROM start_time EXTRACT start_time days_difference SELECT SELECT events FROM FROM FROM EPOCH events EPOCH EXTRACT FROM seconds_difference AS end_time AS SELECT FROM EXTRACT FROM AS start_time end_time hours_difference 3600 EXTRACT EPOCH EPOCH EPOCH start_time))/(3600 minutes EPOCH AS SELECT 60 hours AS FLOOR EXTRACT FROM AS

Problem 8 : Calculating time intervals ( Part 1 )
Calculating the difference between dates and expressing that difference in various time units such as seconds , minutes , hours , weeks , months , and years involves using specific SQL functions and calculations .

Example Scenario :
Consider two TIMESTAMP columns start_time and end_time in a table named events . Let's see how to compute the differences in various units :

Seconds , Minutes , Hours and Days ( PostgreSQL , MySQL , SQL Server , Oracle )

- - Seconds
( ( - ) )
;

- - Minutes
( ( - ) ) /
;

- - Hours
( ( - ) ) /
;

- - Days
( ( - * )

- - Hours , Minutes , Seconds

( ( ( - ) ) / ) ,
( ( ( ( - ) / ) ,
( ( -
;











- - Seconds
SELECT EXTRACT ( EPOCH FROM ( end_time - start_time ) ) AS seconds_difference
FROM events ;

- - Minutes
SELECT EXTRACT ( EPOCH FROM ( end_time - start_time ) ) / 60 AS minutes_difference
FROM events ;

- - Hours
SELECT EXTRACT ( EPOCH FROM ( end_time - start_time ) ) / 3600 AS hours_difference
FROM events ;

- - Days
SELECT EXTRACT ( EPOCH FROM ( end_time - start_time ) ) / ( 3600 * 24 ) AS days_difference

- - Hours , Minutes , Seconds
SELECT
FLOOR ( EXTRACT ( EPOCH FROM ( end_time - start_time ) ) / 3600 ) AS hours ,
FLOOR ( ( EXTRACT ( EPOCH FROM ( end_time - start_time ) ) % 3600 ) / 60 ) AS minutes ,
EXTRACT ( EPOCH FROM ( end_time - start_time ) ) % 60 AS seconds
FROM events ;

These queries utilize the age ( ) function in PostgreSQL , which computes the difference between two timestamps as an interval type , and the EXTRACT ( ) function to pull specific components from that interval . The calculations can be adapted to other SQL dialects , but the functions and precise syntax may vary . Each SQL dialect might have different nuances in how it handles date and time calculations , especially for more complex breakdowns like months and weeks together .

9

EXTRACT end_time WEEK AS FLOOR FROM FROM weeks AS EXTRACT events AS start_time age end_time events years AS EXTRACT FROM MONTH AS end_time MONTH months_difference end_time end_time EXTRACT AS FROM start_time AS FROM SELECT age weeks end_time FROM end_time age DAY FROM age WEEK YEAR years_difference YEAR FROM age SELECT events FROM age start_time)) / 7 EXTRACT EXTRACT events start_time end_time age FROM EXTRACT YEAR end_time SELECT MONTH months SELECT FROM weeks_difference months start_time AS EXTRACT age FROM age start_time)) * 12 end_time start_time)) * 12 events age SELECT FROM YEAR start_time FROM FROM EXTRACT start_time)) % 4 EXTRACT start_time

Problem 9 : Calculating time intervals ( Part 2 )

Weeks , Months , Years ( PostgreSQL , MySQL , SQL Server , Oracle )
The calculation of weeks , months , and years often requires more specific handling because these units don't have a fixed size ( e . g . , months can have 28 to 31 days , years can have 365 or 366 days ) .

- - Weeks
( ( , ) )
;

- - Months
( ( , + ( ( , ) )
;

- - Years
( ( , ) )
;
Months and Weeks , Years , Months , and Weeks ( PostgreSQL Example )
Calculating a combination of years , months , weeks , and days involves breaking down the period piece by piece .

- - Months and Weeks

( ( , + ( ( , ) ) ,
( ( , - - Assuming roughly 4 weeks per month
;

- - Years , Months , and Weeks

( ( , ) ) ,
( ( , ) ) ,
( ( ( , )
;







- - Weeks
SELECT EXTRACT ( WEEK FROM age ( end_time , start_time ) ) AS weeks_difference
FROM events ;

- - Months
SELECT EXTRACT ( YEAR FROM age ( end_time , start_time ) ) * 12 + EXTRACT ( MONTH FROM age ( end_time , start_time ) ) AS months_difference
FROM events ;

- - Years
SELECT EXTRACT ( YEAR FROM age ( end_time , start_time ) ) AS years_difference
FROM events ;
Months and Weeks , Years , Months , and Weeks ( PostgreSQL Example )
Calculating a combination of years , months , weeks , and days involves breaking down the period piece by piece .

- - Months and Weeks
SELECT
EXTRACT ( YEAR FROM age ( end_time , start_time ) ) * 12 + EXTRACT ( MONTH FROM age ( end_time , start_time ) ) AS months ,
EXTRACT ( WEEK FROM age ( end_time , start_time ) ) % 4 AS weeks - - Assuming roughly 4 weeks per month
FROM events ;

- - Years , Months , and Weeks
SELECT
EXTRACT ( YEAR FROM age ( end_time , start_time ) ) AS years ,
EXTRACT ( MONTH FROM age ( end_time , start_time ) ) AS months ,
FLOOR ( EXTRACT ( DAY FROM age ( end_time , start_time ) ) / 7 ) AS weeks
FROM events ;

These queries utilize the age ( ) function in PostgreSQL , which computes the difference between two timestamps as an interval type , and the EXTRACT ( ) function to pull specific components from that interval . The calculations can be adapted to other SQL dialects , but the functions and precise syntax may vary . Each SQL dialect might have different nuances in how it handles date and time calculations , especially for more complex breakdowns like months and weeks together .

educaplay suscripción