SQL Query to Check Given Format of a Date
Here, we are going to see how to find the SQL Query to check whether the date passed to query is the date of the given format or not. In this article, we will be making use of the Microsoft SQL Server as our database.
Here, we will first create a database named “geeks” then we will create a table “department” in that database. After, that we will execute our query on that table.
CREATE DATABASE geeks;
Using the Database:
We have the following table named as department in our geeks database.
CREATE TABLE department ( ID int, SALARY int, NAME Varchar(20), JoinDate datetime );
Adding value into the table:
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')
To verify the contents of the table use the following SQL query:
SELECT * FROM department;
Checking whether the date passed to query is the date of the given format or not:
SQL has IsDate() function which is used to check the passed value is date or not of specified format, it returns 1(true) when the specified value is date otherwise it return 0(false).
SELECT ISDATE(‘Date’) AS “Format”;
SELECT *, ISDATE(‘ColumnName’) AS “Format” FROM TABLENAME ;
SELECT TOP 1000 [ID] ,[SALARY] ,[NAME] ,[JoinDate], ISDATE(JoinDate) as Format FROM [department]
Attention reader! Don’t stop learning now. Learn SQL for interviews using SQL Course by GeeksforGeeks.