
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Specify Date Format on Creating a Table in SQL
In SQL, date and time values are handled using specific data types, such as DATE, DATETIME, TIMESTAMP, etc. Each database management system (DBMS) may have its conventions for date formats, so understanding how to specify and use these formats is essential for working with dates in SQL.
The following are the data types to handle date and time in SQL:
- DATE: Stores date values (year, month, day).
- DATETIME: Stores date and time values (year, month, day, hour, minute, second).
- TIMESTAMP: Stores date and time values, with additional precision (often including time zone information).
Creating Tables with Date Fields
To create a table with a date field, you use the CREATE TABLE statement and specify the appropriate data type for the date column.
Basic Syntax
CREATE TABLE table_name ( column_name DATE, another_column DATETIME);
Example 1: Creating a Table with a DATE Column
Let's create a table to store employee information, including their hiring dates:
CREATE TABLE Employees ( EmployeeID INT PRIMARY KEY, Name VARCHAR(100), HireDate DATE);In this example, the HireDate column is defined using the DATE data type. The DATE type does not include time information and stores only the year, month, and day.
Example 2: Creating a Table with a DATETIME Column
Now, we'll create a table to track project milestones, including exact timestamps for each milestone:
CREATE TABLE ProjectMilestones ( MilestoneID INT PRIMARY KEY, MilestoneName VARCHAR(100), MilestoneDate DATETIME);Here, MilestoneDate is defined using the DATETIME data type, which allows storing both date and time values.
Inserting Date Values into Tables
When inserting date values into these tables, you need to ensure that the dates are formatted correctly according to the DBMS's requirements. The following are some of the examples to work with various date and time formats:
- YYYY-MM-DD: Standard format for DATE data type.
- YYYY-MM-DD HH:MI: Standard format for DATETIME and TIMESTAMP data types.
Example 1: Inserting Dates into the Employees Table
INSERT INTO Employees (EmployeeID, Name, HireDate) VALUES (1, 'John Doe', '2024-09-16');In this example, the date is formatted as YYYY-MM-DD, which is suitable for the DATE type.
Example 2: Inserting Dates and Times into the ProjectMilestones Table
INSERT INTO ProjectMilestones (MilestoneID, MilestoneName, MilestoneDate) VALUES (1, 'Project Kickoff', '2024-09-16 09:00:00');Here, the date and time are formatted as YYYY-MM-DD HH:MI:SS, appropriate for the DATETIME type.
Handling Date Formats in Queries
When querying data, you might need to format or manipulate date values. SQL provides various functions for handling dates.
Example 1: Formatting Dates in SELECT Queries
To display dates in a specific format, you can use the TO_CHAR() function in Oracle or FORMAT function in SQL Server.
Oracle Example
SELECT EmployeeID, Name, TO_CHAR(HireDate, 'DD-Mon-YYYY') AS FormattedHireDateFROM Employees;
SQL Server Example
SELECT EmployeeID, Name, FORMAT(HireDate, 'dd MMM yyyy') AS FormattedHireDateFROM Employees;
Example 2: Extracting Parts of Dates
You may need to extract specific parts of a date, such as the year or month.
Oracle Example
SELECT EmployeeID, Name, EXTRACT(YEAR FROM HireDate) AS HireYearFROM Employees;
SQL Server Example
SELECT EmployeeID, Name, YEAR(HireDate) AS HireYearFROM Employees;