SQL - ISDATE() Function



The SQL ISDATE() function allows you to determine whether a value is a valid date. This function will always return the same result if you use it with the convert function. ISDATE() functions return the integer value 1, indicating that the input expression is a valid date; if they return the integer value 0, it indicates that the input expression is not a date.

The ISDATE() function returns 0, If you provide a datetime2 value, informing you that it is not a date.

Syntax

Following is the syntax for the SQL ISDATE() function −

ISDATE(expression)

Parameters

This function accepts only one parameter as discussed below −

  • expression − the expression that to be checked.

Note − If the given date contains only a time part the month() function returns 1.

Example

In the following query, we are checking whether the expression is valid or not −

SELECT ISDATE('2023-02-16');

Output

When we execute the above query, the output is obtained as follows −

+---------+
| no name |
+---------+
| 1       |
+---------+

Example

Here, we are trying to find the given expression is valid or not using the following query −

SELECT ISDATE('Tutorialspoint');

Output

On executing the above query, the output is displayed as follows −

+---------+
| no name |
+---------+
| 0       |
+---------+

Example

The ISDATE() function will return 0 when the parameter is NULL, indicating that the NULL value is not a valid date.

In the following example we are going to use null to check whether the expression is valid or not by executing the below query −

SELECT ISDATE(NULL) as Result;

Output

The output for the above query is produced as given below −

+--------+
| Result |
+--------+
| 0      |
+--------+

Example

Look at the following example, where we are using the literal name of the month in the expression to check whether the expression is valid or not by using the following query −

SELECT ISDATE('November 27 2000') as Result;

Output

If we execute the above query, the result is produced as follows −

+--------+
| Result |
+--------+
| 1      |
+--------+

Example

Considering the following example, where we are using the time as an input parameter and checking whether the expression is valid or not by running the following query.

SELECT ISDATE('2:36:00') as Result;

Output

On executing the above query, it will generate the following output as shown below −

+--------+
| Result |
+--------+
| 1      |
+--------+

Example

Let us look into the following example, where we are using the ISDATE() function with a variable and checking whether the expression is valid or not by running the following query −

DECLARE @expression VARCHAR(23);
SET @expression= '2023/02/16';
SELECT ISDATE(@expression) AS Result;

Output

On executing the above query, it will generate the following output as shown below −

+--------+
| Result |
+--------+
| 1      |
+--------+

Example

Consider the following example, where we are going to print valid date or invalid date, depending on the return value is 1 or 0, by using the following query −

To use the return value, you can use a conditional expression (rather than simply display either 0 or 1).

IF ISDATE('2023-02-16')=1  
   PRINT 'Valid Date'  
ELSE  
   PRINT 'Invalid Date';

Output

On running the above query, it will generate an output as shown below −

Valid Date

Example

The return value of the ISDATE() depends upon the type of language and date format settings that are going to chosen by the user.

Let’s consider the simple example where we going to observe how changing the language setting can cause the same value to have several outcomes.

In British language

Execute the following query, where the language was set to the British.

SET LANGUAGE British;
SELECT ISDATE('16/02/2023') AS 'in British';

Output

When we execute the above query, the output is obtained as follow −

+------------+
| in British |
+------------+
| 1          |
+------------+

In us_english language

We are going to use the same value but the difference was the language was set to us_english

SET LANGUAGE us_english;
SELECT ISDATE('16/02/2023') AS 'in us_english'; 

Output

When the above query gets executed, it will generate the following output as shown below −

+---------------+
| in us_english |
+---------------+
| 0             |
+---------------+

In dmy format

The ISDATE() function depends upon the type of format setting chosen by the user.

Considering the simple example, where we are going to observe how format settings makes the same value to have different return values.

Execute the following query, where we are setting the format setting to dmy.

SET DATEFORMAT dmy;
SELECT ISDATE('16/02/2023') AS 'DATEFORMAT is dmy';

Output

Running the above query, generate the following output as shown below −

+-------------------+
| DATEFORMAT is dmy |
+-------------------+
| 1                 |
+-------------------+

In mdy format

Considering the same value and execute the following query by changing the format setting to mdy.

SET DATEFORMAT mdy;
SELECT ISDATE('16/02/2023') AS 'DATEFORMAT is mdy'; 

Output

When we execute the above query, the output is obtained as follows

+-------------------+
| DATEFORMAT is mdy |
+-------------------+
| 0                 |
+-------------------+
sql-date-functions.htm
Advertisements