Open In App

SQL Query to Check Given Format of a Date

Last Updated : 16 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Date validation is a common requirement when working with databases. In SQL, ensuring that a date adheres to a specific format is important for maintaining data consistency and preventing errors during analysis or processing.

This article will guide us through the process of using SQL queries to check whether a date follows a given format. We’ll focus on Microsoft SQL Server and demonstrate the use of the ISDATE() function, along with practical examples and output explanations.

Importance of validating Given Format of a Dates in SQL

Dates play an important role in database management, whether for recording transactions, tracking events, or scheduling tasks. To ensure data accuracy and reliability, it’s important to validate dates in the desired format. SQL Server provides the ISDATE() function to help us verify the validity of dates. In this guide, we’ll show how to:

  • Create a sample database and table.
  • Insert data into the table.
  • Validate whether a given date follows the expected format using the ISDATE() function.

Step 1: Setting Up the Database and Table

To begin, we need to set up our environment by creating a sample database and defining the structure of a table that will hold our data. This setup allows us to simulate a real-world scenario where we need to validate date formats

Creating the Database

First, we create a new database named geeks to store our sample data. Then, we select this database to use for our operations.

CREATE DATABASE geeks;
USE geeks;

Creating the Table

Next, create a table named department to store information such as ID, salary, name, and join date of employees.

CREATE TABLE department
(
ID int,
SALARY int,
NAME Varchar(20),
JoinDate datetime
);

Inserting Sample Data

To populate our department table with sample data, we insert records with various join dates in different formats.

INSERT INTO department VALUES (1, 34000, 'Neha', '09-24-2013')
INSERT INTO department VALUES (2, 33000, 'Hema', '02-02-2015')
INSERT INTO department VALUES (3, 36000, 'Jaya', '09-09-2017')
INSERT INTO department VALUES (4, 35000, 'Priya', '05-18-2018')
INSERT INTO department VALUES (5, 34000, 'Ketan', '02-25-2019')

Verifying the Data

To ensure the data has been inserted correctly, we can select from the department table to view the contents.

SELECT * FROM department;

Output

department-table

Department table

Step 2: Using the ISDATE() Function to Validate Date Format

The ISDATE() function in SQL is used to determine whether a given string can be interpreted as a valid date. This function is important for filtering out invalid date entries or ensuring that user inputs adhere to a specific format.

Syntax

SELECT ISDATE(‘Date’) AS “Format”;

To check a column’s values, use:

SELECT *, ISDATE(‘ColumnName’) AS “Format” FROM TABLENAME ;

Example: Checking the JoinDate Column

In this example, we use the ISDATE() function to validate the format of the JoinDate column in our department table.

Query:

SELECT TOP 1000 [ID]
,[SALARY]
,[NAME]
,[JoinDate], ISDATE(JoinDate) as Format
FROM [department]

Output

Check-JoinDate-Format

Checking the JoinDate Column

Explanation:

In this case, 1 indicates that the JoinDate is in a valid format (in this case, MM-DD-YYYY). Any records with a 0 would mean that the date format does not match the expected pattern.

Conclusion

Validating dates in SQL is an essential step to ensure data integrity and consistency. By using the ISDATE() function, we can quickly verify whether a date is in a recognized format or filter out invalid date entries from our dataset. These techniques are particularly useful when working with user inputs or imported data, making our database operations more reliable and efficient. With the knowledge of date validation using SQL Server, we can now confidently handle date-related tasks in your projects

FAQs

How to select date format in SQL query?

We can use the FORMAT() function (in SQL Server) or the DATE_FORMAT() function (in MySQL) to display dates in a specific format. For example, FORMAT(OrderDate, 'dd-MM-yyyy') formats the date as dd-MM-yyyy.

How to select SQL by date?

Use the WHERE clause with the DATE or BETWEEN keywords to filter data by date. For example: SELECT * FROM Orders WHERE OrderDate = ‘2023-12-01’;

How to get date format dd mm yyyy in SQL?

In MySQL, use DATE_FORMAT(OrderDate, '%d-%m-%Y'). In SQL Server, use FORMAT(OrderDate, 'dd-MM-yyyy') to format the date as dd-mm-yyyy



Next Article
Article Tags :

Similar Reads

three90RightbarBannerImg