Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Storing Dates and Times | Date/Time and Special Data Types in Practice
SQL Data Types Explained

bookStoring Dates and Times

Understanding how to accurately store dates and times is essential for any database that handles schedules, events, or logs. SQL provides dedicated data types for this purpose, making it easy to represent and retrieve these values. The two most fundamental types are DATE and TIME. The DATE type stores calendar dates—year, month, and day—without any information about the time of day. A typical format for a DATE value is "YYYY-MM-DD", such as "2024-06-01". The TIME type, on the other hand, stores just the time of day, without any reference to a particular date. Its standard format is "HH:MM:SS", like "14:30:00". These formats are widely supported and are designed to be unambiguous and consistent across SQL implementations.

CREATE TABLE meetings (
    id SERIAL PRIMARY KEY,
    meeting_date DATE,
    meeting_time TIME
);

After defining a table with DATE and TIME columns, you can insert and retrieve values using these standard formats. When adding records, provide dates in the "YYYY-MM-DD" format and times in the "HH:MM:SS" format. When querying, SQL will return these values in the same format, making it easy to filter or sort by date or time. For example, you might want to find all meetings scheduled on a certain day or at a specific time. By using the appropriate data types, you ensure this data is stored efficiently and can be compared or manipulated using SQL's built-in date and time functions.

-- Insert a new meeting
INSERT INTO meetings (meeting_date, meeting_time)
VALUES ('2024-06-15', '09:00:00');

-- Query meetings on a specific date
SELECT * FROM meetings
WHERE meeting_date = '2024-06-15';

-- Query meetings at a specific time
SELECT * FROM meetings
WHERE meeting_time = '09:00:00';

1. Which type would you use to store only the time of day?

2. What is the standard format for DATE values in SQL?

question mark

Which type would you use to store only the time of day?

Select the correct answer

question mark

What is the standard format for DATE values in SQL?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 1

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Suggested prompts:

Can you explain how to filter meetings between two dates?

How do I update the date or time of an existing meeting?

What other date and time data types are available in SQL?

bookStoring Dates and Times

Deslize para mostrar o menu

Understanding how to accurately store dates and times is essential for any database that handles schedules, events, or logs. SQL provides dedicated data types for this purpose, making it easy to represent and retrieve these values. The two most fundamental types are DATE and TIME. The DATE type stores calendar dates—year, month, and day—without any information about the time of day. A typical format for a DATE value is "YYYY-MM-DD", such as "2024-06-01". The TIME type, on the other hand, stores just the time of day, without any reference to a particular date. Its standard format is "HH:MM:SS", like "14:30:00". These formats are widely supported and are designed to be unambiguous and consistent across SQL implementations.

CREATE TABLE meetings (
    id SERIAL PRIMARY KEY,
    meeting_date DATE,
    meeting_time TIME
);

After defining a table with DATE and TIME columns, you can insert and retrieve values using these standard formats. When adding records, provide dates in the "YYYY-MM-DD" format and times in the "HH:MM:SS" format. When querying, SQL will return these values in the same format, making it easy to filter or sort by date or time. For example, you might want to find all meetings scheduled on a certain day or at a specific time. By using the appropriate data types, you ensure this data is stored efficiently and can be compared or manipulated using SQL's built-in date and time functions.

-- Insert a new meeting
INSERT INTO meetings (meeting_date, meeting_time)
VALUES ('2024-06-15', '09:00:00');

-- Query meetings on a specific date
SELECT * FROM meetings
WHERE meeting_date = '2024-06-15';

-- Query meetings at a specific time
SELECT * FROM meetings
WHERE meeting_time = '09:00:00';

1. Which type would you use to store only the time of day?

2. What is the standard format for DATE values in SQL?

question mark

Which type would you use to store only the time of day?

Select the correct answer

question mark

What is the standard format for DATE values in SQL?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 1
some-alt