YugabyteDB is a PostgreSQL-compatible distributed database that supports the majority of PostgreSQL syntax.
YugabyteDB also inherits all of the awesome built-in function that PostgreSQL provides.
I was checking and noticed that there is not a function that can be used to let me know if a value is a valid date.
That’s not a problem because I can create my own!
Example:
CREATE OR REPLACE FUNCTION is_date(d TEXT)
RETURNS BOOLEAN
AS
$$
BEGIN
BEGIN
RETURN d::DATE = d::DATE;
EXCEPTION
WHEN OTHERS THEN
RETURN FALSE;
END;
END;
$$
LANGUAGE PLPGSQL;
yugabyte=# CREATE OR REPLACE FUNCTION is_date(d TEXT)
yugabyte-# RETURNS BOOLEAN
yugabyte-# AS
yugabyte-# $$
yugabyte$# BEGIN
yugabyte$# BEGIN
yugabyte$# RETURN d::DATE = d::DATE;
yugabyte$# EXCEPTION
yugabyte$# WHEN OTHERS THEN
yugabyte$# RETURN FALSE;
yugabyte$# END;
yugabyte$# END;
yugabyte$# $$
yugabyte-# LANGUAGE PLPGSQL;
CREATE FUNCTION
yugabyte=# SELECT is_date('2023-10-13') is_date;
is_date
---------
t
(1 row)
The function even works on Leap Year dates!
yugabyte=# SELECT is_date('2020-02-29') is_date;
is_date
---------
t
(1 row)
yugabyte=# SELECT is_date('2020-02-30') is_date;
is_date
---------
f
(1 row)