{"id":272,"date":"2013-08-25T21:26:48","date_gmt":"2013-08-25T21:26:48","guid":{"rendered":"http:\/\/www.corrspt.com\/blog\/?p=272"},"modified":"2013-08-25T21:28:13","modified_gmt":"2013-08-25T21:28:13","slug":"query-sqlboql-dates-clause","status":"publish","type":"post","link":"http:\/\/www.corrspt.com\/blog\/2013\/08\/25\/query-sqlboql-dates-clause\/","title":{"rendered":"Query SQL \/ BOQL with Dates in where clause using Java"},"content":{"rendered":"<p>Hi everyone,<br \/>\nOne of the things that I struggled in the beginning (<em>a long time ago in a galaxy far away, cof cof<\/em>) with SQL queries (or BOQL queries if you&#8217;re using the <a title=\"XEO Framework\" href=\"http:\/\/xeoframework.org\" target=\"_blank\">XEO framework<\/a>) was how to make queries using dates in the where clause, something like this:<\/p>\n<pre class=\"brush:sql\">select * from table where column_date &gt; SOMETHING<\/pre>\n<p>Not knowing how to do date comparisons I usually did stuff like this ( evil code ahead, beware ) :<\/p>\n<pre class=\"brush:sql\">select * from some_table where to_char(column_data,'DD-MON-YYYY') &gt; to_char('23-05-2013','DD-MON-YYYY');<\/pre>\n<p>Usually in Java you have <em>java.util.Date<\/em> instances which I <em>heroically<\/em> used like the following, in order to be able to create the previous query:<\/p>\n<pre class=\"brush:java\">SimpleDateFormat sdf = new SimpleDateFormat(\"dd-MM-yyyy\");\r\njava.util.Date date = loadDateFromSomewhere();\r\nString sql = \"select * from some_table where to_char(column_data,'DD-MON-YYYY') &gt; ('\"+sdf.format(date)+\"','DD-MON-YYYY')\";<\/pre>\n<p>Well now I don&#8217;t do this kind of thing, eh eh&#8230; it&#8217;s way easier to do such queries using proper data types. Check in a Java SQL Prepared statement:<\/p>\n<pre class=\"brush:java\">Connection conn = getConnection();\r\nPreparedStatement statement = conn.prepareStatement(\"select * from some_table where column_date &gt; ?\");\r\nstatement.setDate(1, new java.sql.Date(loadDateFromSomewhere().getTime()));<\/pre>\n<p>Notice how you pass a java.sql.Date instance (created using the information from the java.util.Date instance) to the parameter. This has the added benefit that no data conversions are done (meaning extra performance). And if you have an index on a date column <strong>that index will be used<\/strong> ( I also learned that when you apply a function to an indexed column, that index won&#8217;t be used properly as the function has to be applied to every row in the table &#8211; <a title=\"SQL Index and Functions\" href=\"http:\/\/www.sqlteam.com\/article\/avoid-enclosing-indexed-columns-in-a-function-in-the-where-clause\" target=\"_blank\">check this<\/a> )<\/p>\n<p>In BOQL by the way it would be like this:<\/p>\n<pre class=\"brush:java\">boObjectList.list(getEboContext(), \"select Object where column_data &gt; ?\", new Object[]{ new java.sql.Date(date.getTime()) });<\/pre>\n<p>Important notes:<\/p>\n<ul>\n<li>In SQL\/BOQL queries you use <em>java.sql.Date<\/em> and <em>java.sql.Timestamp<\/em> instances (you don&#8217;t use <em>java.util.Date<\/em> instances)<\/li>\n<li>Use <em>java.sql.Date<\/em> instances <strong>when you want to compare Dates only (ignoring time)<\/strong><\/li>\n<li>Use <em>java.sql.Timestamp<\/em> instances <strong>when you want to compare Dates including time information<\/strong><\/li>\n<\/ul>\n<p>And <em>voil\u00e1<\/em>&#8230; Dates in SQL\/BOQL queries (using Java) without much effort. Hope it was useful,\u00a0 Happy coding!<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi everyone, One of the things that I struggled in the beginning (a long time ago in a galaxy far away, cof cof) with SQL queries (or BOQL queries if you&#8217;re using the XEO framework) was how to make queries &hellip; <a href=\"http:\/\/www.corrspt.com\/blog\/2013\/08\/25\/query-sqlboql-dates-clause\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[28,45,27,46],"class_list":["post-272","post","type-post","status-publish","format-standard","hentry","category-java","tag-date","tag-java","tag-sql","tag-tip"],"_links":{"self":[{"href":"http:\/\/www.corrspt.com\/blog\/wp-json\/wp\/v2\/posts\/272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.corrspt.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.corrspt.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.corrspt.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.corrspt.com\/blog\/wp-json\/wp\/v2\/comments?post=272"}],"version-history":[{"count":4,"href":"http:\/\/www.corrspt.com\/blog\/wp-json\/wp\/v2\/posts\/272\/revisions"}],"predecessor-version":[{"id":276,"href":"http:\/\/www.corrspt.com\/blog\/wp-json\/wp\/v2\/posts\/272\/revisions\/276"}],"wp:attachment":[{"href":"http:\/\/www.corrspt.com\/blog\/wp-json\/wp\/v2\/media?parent=272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.corrspt.com\/blog\/wp-json\/wp\/v2\/categories?post=272"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.corrspt.com\/blog\/wp-json\/wp\/v2\/tags?post=272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}