Skip to content
Advertisement

Oracle birthday column regex constrain violated

I want all my dates in my database in the following yyyy-mm-dd date format. For example, ‘2020-05-28’. But this doesn’t seem to work as I thought it would when I insert a date into my User table which contains the birthday column.

Here’s my birthday column constraint :

I get the following error when I try to insert : ‘2020-05-28’ or any other date for that matter :

Advertisement

Answer

You are wrongly interpreting the concept of the DATE datatype. Probably, you are mixing the display format of dates (the value that you see when you display a date value), and their internal representation.

A date is just that: a date. It is stored internally in some database specific format, that you don’t need to worry about. So there is no need for a check constraint to control the format.

Whenever you need to display date, you can use function to_char() to format it as a string, like:

You can also set session parameter NLS_DATE_FORMAT so all dates are automatically formated in a given format when displayed:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement