PostgreSQLdata validation

Check Date Format in PostgreSQL with Regex

Validate date strings in text columns using PostgreSQL regex. SQL examples for YYYY-MM-DD date format checks in Postgres.

The Regex Pattern

Regex pattern

^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$

Use this pattern with PostgreSQL's ~ operator.

PostgreSQL SQL Example

-- Use PostgreSQL regex to check that string dates in a legacy text column conform to YYYY-MM-DD format before migration
SELECT *
FROM your_table
WHERE your_column ~ '^\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$';

How This Works

Use PostgreSQL regex to check that string dates in a legacy text column conform to YYYY-MM-DD format before migration. The regex pattern is used with PostgreSQL's ~ operator to filter rows at the database level.

Using regex directly in SQL is efficient for use cases like data validation โ€” it avoids loading data into application memory just to filter it with a regex.

Need a custom SQL + Regex query?

Describe your specific use case in plain English. RegSQL generates the exact PostgreSQL query with the right regex pattern for your schema.

Related SQL Patterns