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

标签 sql database ibm-midrange

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

为什么我的查询开始时,用户会看到一个对话框,其中包含我指定为 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 文件是(Ex. 2005-03-11-09.54.18.296000)格式和我的查询值 StartDate 在(例如 01/01/14)

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

编辑:

使用 Roopeshtimestamp > 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

WHERE 子句中有错误。解析器需要“)”。继续吗? [选择是]

我选择日期 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

WHERE 子句中有错误。解析器需要“)”。继续吗? [选择是]

我选择日期 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)

最佳答案

我不是续集专家...

但我怀疑有一种方法可以告诉它你想提示输入实际日期..

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

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

关于sql - 在 WHERE 子句中格式化 [TIMESTAMP] 字段值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23366121/

相关文章:

php - MySql 查询仅返回有效的值组合

php - 构建和搜索 GPS 坐标的 SQL 数据库/表

c++ - MySQL++,存储实时数据

php - 为什么 sqlite 不创建我的数据库,即使它已经在 phpinfo() 中检查过,因为它已启用

mysql - 复制关系表数据

sql - 主变量或参数的转换错误 *N

php - 需要帮助为使用 MAMP 托管的本地 WordPress 导入 SQL 数据库

MySql 不同的问题 : Error Code: 1242 Subquery returns more than 1 row

php - 在 IBM i (AS400) DB2 中导入/导出 CSV

.net - AS400 桥接的首选方法是什么?