简体   繁体   English

在SQL JDBC查询中使用Datediff

[英]Using datediff in sql jdbc query

I'm attempting to create a JDBC query with the following statement 我正在尝试使用以下语句创建JDBC查询

String query = "SELECT COLUMN1,DATECOLUMN2 FROM tableName +
            "where datediff(d,DATECOLUMN2,getdate()) <= 1";
st = conn1.createStatement();
rs = st.executeQuery(query);  //receiving error here

I am receiving the following error message 我收到以下错误消息

java.sql.SQLException: "d" is not a recognized table hints option. If it is intended as a parameter to a table-valued function or to the CHANGETABLE function, ensure that your database compatibility mode is set to 90.

I'm sure the query isn't recognizing the datediff function for some reason I am not sure why since i was previously using HQL in the same application and retrieving the values. 由于某些原因,我确定查询无法识别datediff函数,因为我以前在同一应用程序中使用HQL并检索了值,所以我不确定为什么。

In an attempt to use an alternative function I used 为了尝试使用替代功能,我使用了

{fn TIMESTAMPADD( SQL_TSI_DAY, 1, CURRENT_TIMESTAMP)}

but it also failed I later on found that this is only used for Derby Database's 但这也失败了,后来我发现这仅用于Derby Database的

Can someone assist me in using the proper sql function to compare a date with the current date using JDBC 有人可以协助我使用适当的sql函数使用JDBC将日期与当前日期进行比较

String query = "SELECT COLUMN1,DATECOLUMN2 FROM tableName "+
               "where datediff(day,DATECOLUMN2,getdate()) <= 1";

You have a comma before from. 之前您有一个逗号。 Based on the error messages you are running this against SQL server. 根据错误消息,您正在针对SQL Server运行此消息。

String query = "SELECT COLUMN1,DATECOLUMN2 FROM tableName " 
              +" where datediff(d,DATECOLUMN2,getdate()) <= 1";

The comma after the "d" should be a dot: “ d”后的逗号应为点号:

where datediff(d.DATECOLUMN2,getdate())
--------------- ^ dot here

The posted snippet doesn't have a closing double quote between tableName and + , but I figure that is just a typo. 发布的代码段在tableName+之间没有双引号,但是我认为这只是一个错字。 However, in your real code, where precisely is the double quote? 但是,在您的实际代码中,双引号到底在哪里? Is it directly after tablename , like this 这样直接在tablename之后吗

String query = "SELECT COLUMN1,DATECOLUMN2 FROM tableName" +

or after the space that follows tablename , like this tablename后面的空格之后,像这样

String query = "SELECT COLUMN1,DATECOLUMN2 FROM tableName "+

It is very likely the former, because in that case the resulting query would look exactly the way as to cause the error you are getting. 很有可能是前者,因为在这种情况下,结果查询看起来与导致您得到错误的方式完全一样。 Take a look at this: 看看这个:

SELECT COLUMN1,DATECOLUMN2 FROM tableNamewhere datediff(d,DATECOLUMN2,getdate()) <= 1

You can see that where merges with the table name and datediff becomes an alias. 您会看到与表名合并的wheredatediff成为别名。 What follows is interpreted as table hints. 以下内容被解释为表提示。 (You can specify table hints without WITH in older versions of SQL Server/older compatibility levels.) Consequently, SQL Server stumbles over d , as that is indeed an incorrect table hint. (您可以在旧版本的SQL Server /旧版本的兼容级别中指定不带WITH的表提示。)因此,SQL Server偶然发现了d ,因为这确实是不正确的表提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM