Date in where clause

时间:2019-04-08 13:38:36

标签: sql oracle

I'm having to reverse engineer an Oracle query. I'm familiar with SQL but not Oracle and I'm using SQL OPENQUERY to the linked Oracle server. I've gone through the larger portions of the query and figured most of the syntax and getting results but when I get to the following in the where clause I get "missing expression" error. (simplified for clarity)

 SELECT USER_DATE
 FROM TABLE
 WHERE USER_DATE >= {1}

I can query the table and see that the column USER_DATE is indeed a date field so I don't understand the meaning of >= {1}. This query came to me as "this is how the other dept uses this query, make it work for us" and I don't have access to this other dept. Can someone explain how this supposedly works?

2 个答案:

答案 0 :(得分:3)

The {1} is just a placeholder for an actual value. Once you've populated this with a proper date value it will run just fine like this:

SELECT USER_DATE
FROM TABLE
WHERE USER_DATE >= '01/01/2019'

You can also use the to_date function for a specific format:

SELECT USER_DATE
FROM TABLE
WHERE USER_DATE >= to_date('01/01/2019', 'mm/dd/yyyy')

答案 1 :(得分:0)

很可能是远程Oracle数据库中的默认日期格式与“其他”数据库中的日期有关。大多数数据库允许您将日期选择为字符串,也可以将日期与字符串进行比较。比较将字符串隐式转换为日期。

最有可能隐式转换在这里失败。正确的方法是查看参数是否真的是字符串,以及在那里显示的日期格式。然后在{1}右侧使用to_date。

为使其暂时不出错,您可以将to_char放在左侧。当然,您的输出将不正确,但是由于可以运行SQL,因此您可以进行故障排除