I am considering the following with regards to implementing a check constraint on a column I am calling year_month
. My objective is to have a check constraint to enforce YYYY-MM
.
- My intention is to create the column with data type:
varchar(7)
example value that would be saved from my front end:2017-08
Would I then add a regular expression to enforce only numbers, dashes and max 7 in my check constraint setup? Or could I add some sort of regular expression to check for this pattern more exactly?
date
datatype using the first day of the month. Makes it easier to do comparisons & other operationsDATEPART(day, DateColumn) = 1
to constrain the value to the first of the month.YearMonth
class in Java. SQL lacks such a type. So the next best option is indeed to store a String of seven characters in the standard ISO 8601 format ofYYYY-MM
. This format happens to be chronological when sorted alphabetically.