在WHERE子句中格式化[TIMESTAMP]字段值?

时间:2014-04-29 13:30:21

标签: sql database ibm-midrange

我正在编写一个连接2个表的SEQUEL ViewPoint查询。第一个表格包含以下字段:ActionSSANMEMNORSCODEUSERIDTIMESTAMP,格式为(例如2005-03-11-09.54.18.296000)。其他表格仅用于通过加入NAME上的两个表来获取成员SSAN

为什么我的查询开始,会向用户显示一个对话框,其中包含我指定为StartDate的日期。我们的想法是返回table1.TIMESTAMP值大于用户选择StartDate的所有所需字段。

我当前的查询如下:

 SELECT         memno.1 EDTCDE(L), name.2, 'SYS1' NAME(SYS), "&&startdate" NAME(StartDate), CURRENT DATE NAME(CurDate)
 FROM           library1/table1, library1/table2
 JOIN               SSAN.1=SSAN.2
 WHERE          TIMESTAMP>StartDate AND RSCODE='STP'
 UNION 
 SELECT         memno.1 EDTCDE(L), name.2, 'SYS2' NAME(SYS), "&&startdate" NAME(StartDate), CURRENT DATE NAME(CurDate)
 FROM           library2/table1, library2/table1
 JOIN               SSAN.1=SSAN.2
 WHERE          TIMESTAMP>StartDate AND RSCODE='STP'
 ORDER BY   sys ASC, memno ASC

结果是:

Fields TIMESTAMP and STARTDATE in WHERE clause are not compatible.
Cause. . . . .: You are trying to compare two fields that not compatible. One of the following is true:
1 -- One field is numeric and the other is not numeric (character or date/time)
2 -- One field is character and the other is not character (numeric or date/time)
3 -- One field is double-byte and the other is single byte.

这显然是由于我的table1.TIMESTAMP字段属于(例如2005-03-11-09.54.18.296000)格式而我的查询值StartDate位于(例如01/01/14)

如何格式化TIMESTAMP子句中的WHERE值,以便将其与StartDate值进行比较? (或者有更好的方法可以解决这个问题吗?在SQL和数据库QWerying方面,我还是很绿色)

修改

使用 Roopesh 'timestamp > cast(StartDate as datetime)的建议会导致:

 SELECT         memno.1 EDTCDE(L), name.2, 'SYS1' NAME(SYS), "&&startdate" NAME(StartDate), CURRENT DATE NAME(CurDate)
 FROM           library1/table1, library1/table2
 JOIN               SSAN.1=SSAN.2
 WHERE          TIMESTAMP>Cast(StartDate as datetime) AND RSCODE='STP'
 UNION 
 SELECT         memno.1 EDTCDE(L), name.2, 'SYS2' NAME(SYS), "&&startdate" NAME(StartDate), CURRENT DATE NAME(CurDate)
 FROM           library2/table1, library2/table1
 JOIN               SSAN.1=SSAN.2
 WHERE          TIMESTAMP>Cast(StartDate as datetime) AND RSCODE='STP'
 ORDER BY   sys ASC, memno ASC

There is an error in the WHERE clause. Parser expected ")". Continue Anyway? [选择是]

我选择日期01/01/14,它在sql中显示为"01/01/14 NAME(StartDate)并收到:

 Identifier 'AS' preceding ' datetime)' is used incorrectly.
    Proper SQL syntax rules have been viloated. The identifier cannot occur where it has been found in the statement. Instad of 'AS', SQL syntax rules allow only: ) ,. If you are using *SEQUEL object authority checking, you cannot use runtime variables in place of the allowed values.

使用 Notulysses '建议:

变量StartDate属于Date类型,长度为10,默认为01/01/2014

 SELECT         memno.1 EDTCDE(L), name.2, 'SYS1' NAME(SYS), "&&startdate" NAME(StartDate), CURRENT DATE NAME(CurDate)
 FROM           library1/table1, library1/table2
 JOIN               SSAN.1=SSAN.2
 WHERE          CAST(TIMESTAMP as Date)>StartDate AND RSCODE='STP
 UNION 
 SELECT         memno.1 EDTCDE(L), name.2, 'SYS2' NAME(SYS), "&&startdate" NAME(StartDate), CURRENT DATE NAME(CurDate)
 FROM           library2/table1, library2/table1
 JOIN               SSAN.1=SSAN.2
 WHERE          CAST(TIMESTAMP as Date)>StartDate AND RSCODE='STP
 ORDER BY   sys ASC, memno ASC

There is an error in the WHERE clause. Parser expected ")". Continue Anyway? [选择是]

我选择日期01/01/14,它在sql中显示为"01/01/14 NAME(StartDate)并收到:

Identifier 'AS' preceding ' Date)>Sta' is used incorrectly.
Proper SQL syntax rules have been viloated. The identifier cannot occur where it has been found in the statement. Instad of 'AS', SQL syntax rules allow only: ) ,. If you are using *SEQUEL object authority checking, you cannot use runtime variables in place of the allowed values.

与常规SQL相比,ViewPoint语法可能很奇怪。很高兴我们不久就会把它当作我们商店的标准工具.......

EDIT2(解决方案):

正如 JamesA 所述,诀窍是使用DATE函数:DATE(TIMESTAMP)

2 个答案:

答案 0 :(得分:3)

我不是SEQUEL专家......

但我怀疑是否有办法告诉你要提示实际约会..

然后您的查询可以使用TIMESTAMP_ISO(StartDate)将日期转换为时间戳。

请注意,将日期转换为时间戳而不是时间戳到日期会更好,因为转换表列中的值将导致索引无法使用。

答案 1 :(得分:2)

使用DATE函数将时间戳转换为日期部分:

SELECT         memno.1 EDTCDE(L), name.2, 'SYS1' NAME(SYS), "&&startdate" NAME(StartDate), CURRENT DATE NAME(CurDate)
FROM           library1/table1, library1/table2
JOIN               SSAN.1=SSAN.2
WHERE          DATE(TIMESTAMP)>StartDate AND RSCODE='STP'
UNION 
SELECT         memno.1 EDTCDE(L), name.2, 'SYS2' NAME(SYS), "&&startdate" NAME(StartDate), CURRENT DATE NAME(CurDate)
FROM           library2/table1, library2/table1
JOIN               SSAN.1=SSAN.2
WHERE          DATE(TIMESTAMP)>StartDate AND RSCODE='STP'
ORDER BY   sys ASC, memno ASC