限制两个特定日期之间的日期,SQL Developer Oracle
Restricting a date between two specific dates, SQL Developer Oracle
如何检查输入的日期是否在两个设定日期之间(最好是设定日期和当前日期)
我正在创建 BOK table,并希望 AAR(年份)介于 1900 年和当前年份之间(可以手动指定),请注意我对此不是很有经验并且仍在学习.使用 Oracle SQL 开发人员。
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.
使用date
前缀:
AAR DATE CHECK (AAR between date '1900-01-01' AND date '2017-01-01')
您正在使用字符串指定日期值。这会导致数据库尝试将其转换为具有默认格式的日期,但由于您的字符串与该格式不匹配而失败。不要依赖这个默认格式,你最好明确指定它:
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'))
)
和 Gordon 一样,我喜欢 ANSI 标准日期文字(对于没有时间部分的日期)。
但是请注意,"between" 包括两个端点。如果希望年份在1900年和当前年份之间,条件应该是
check (aar >= date '1900-01-01' and aar < date '2017-01-01')
请注意,<= date '2016-12-31'
只有在保证 aar
永远不会包含时间组件(例如 09:30:00
)的情况下才会起作用,但这通常是一种糟糕的做法依靠这样的东西。
虽然...你解释说 "aar" 意思是 "year." 但它是 DATE 数据类型。那么它是什么 - 要求始终是 1 月 1 日 00:00:00 的 "date"?奇怪。
然后,你提到了SYSDATE。 las,SYSDATE 在不同的调用中可能有不同的值,因此它(以及包含它们的任何其他类似函数或表达式)在检查约束中是非法的。检查条件应该为真或假,而不管谁在什么地方、什么时候检查它。如果这是一个要求,有一些解决方法(例如,可能有一个 "date_created" 列,使用 SYSDATE 来填充它,并且检查约束可以在 table 级别,跨越两列) .
如何检查输入的日期是否在两个设定日期之间(最好是设定日期和当前日期)
我正在创建 BOK table,并希望 AAR(年份)介于 1900 年和当前年份之间(可以手动指定),请注意我对此不是很有经验并且仍在学习.使用 Oracle SQL 开发人员。
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.
使用date
前缀:
AAR DATE CHECK (AAR between date '1900-01-01' AND date '2017-01-01')
您正在使用字符串指定日期值。这会导致数据库尝试将其转换为具有默认格式的日期,但由于您的字符串与该格式不匹配而失败。不要依赖这个默认格式,你最好明确指定它:
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'))
)
和 Gordon 一样,我喜欢 ANSI 标准日期文字(对于没有时间部分的日期)。
但是请注意,"between" 包括两个端点。如果希望年份在1900年和当前年份之间,条件应该是
check (aar >= date '1900-01-01' and aar < date '2017-01-01')
请注意,<= date '2016-12-31'
只有在保证 aar
永远不会包含时间组件(例如 09:30:00
)的情况下才会起作用,但这通常是一种糟糕的做法依靠这样的东西。
虽然...你解释说 "aar" 意思是 "year." 但它是 DATE 数据类型。那么它是什么 - 要求始终是 1 月 1 日 00:00:00 的 "date"?奇怪。
然后,你提到了SYSDATE。 las,SYSDATE 在不同的调用中可能有不同的值,因此它(以及包含它们的任何其他类似函数或表达式)在检查约束中是非法的。检查条件应该为真或假,而不管谁在什么地方、什么时候检查它。如果这是一个要求,有一些解决方法(例如,可能有一个 "date_created" 列,使用 SYSDATE 来填充它,并且检查约束可以在 table 级别,跨越两列) .