Search code examples
sqloracle-databasedateddlcreate-table

Restricting a date between two specific dates, SQL Developer Oracle


How to check if inputted date is between 2 set dates (preferably a set date and the current date)

I'm creating the BOK table, and want the AAR (Year) to be between 1900 and current year (can be specified manually), Note I'm not very experienced with this and still learning. Using Oracle SQL developer.

   CREATE TABLE BOK
    (
    ISBN      VARCHAR2(50),
    TITTEL    VARCHAR2(50),
    UTGIVER   VARCHAR2(50),
    AAR       DATE     CHECK (AAR between'1900/01/01' AND '2017/01/01')
    )
Error starting at line : 1 in command -

Error report -
ORA-01861: stringconstant doesnt match formatstring
01861. 00000 -  "literal does not match format string"
*Cause:    Literals in the input must be the same length as literals in
           the format string (with the exception of leading whitespace).  If the
           "FX" modifier has been toggled on, the literal must match exactly,
           with no extra whitespace.
*Action:   Correct the format string to match the literal.

Solution

  • You're using a string to specify a date value. This causes the database to attempt to convert it to a date with its default format, which fails since your string doesn't match that format. Instead of relying on this default format, you would be better served specifying it explicitly:

    CREATE TABLE BOK
    (
        ISBN      VARCHAR2(50),
        TITTEL    VARCHAR2(50),
        UTGIVER   VARCHAR2(50),
        AAR       DATE CHECK (AAR between TO_DATE('1900/01/01', 'yyyy/mm/dd') AND 
                                          TO_DATE('2017/01/01', 'yyyy/mm/dd'))
    )