Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If the <code>MyDate</code> column is a <code>datetime</code>, as it appears to be, then it's already in the right "format". <strong>Don't</strong> convert it to a <code>varchar(50)</code> in the predicate condition - this makes your query non-sargable and will kill performance on any indexes you might have.</p> <p>Instead, take your parameters as <code>date</code> or <code>datetime</code> instances:</p> <pre><code>SELECT ... FROM MyTable WHERE MyDate &gt;= @BeginDate AND MyDate &lt;= @EndDate </code></pre> <p>Your query should not depend on a specific date format in the input parameters - those parameters are not <code>varchar</code> types, they are <code>datetime</code> (or <code>date</code>). When you run this query or stored procedure from whatever environment the application is in and supply binding parameters (you <strong>are</strong> using bind parameters, right?), said library will automatically handle any formatting issues.</p> <p>If you try to use the <code>&gt;=</code> and <code>&lt;=</code> operators on character representations of dates, with any format other than the ISO standard <code>yyyymmdd</code>, you will get the wrong results, because the alphabetical order is different from the temporal order. Don't do this.</p> <p>If you simply need to write an ad-hoc query, i.e. this isn't being run from any programming environment, then simply do not use the <code>dd/mm/yyyy</code> format. Use the ISO date format instead; it is unambiguous and implicitly convertible to <code>datetime</code> values:</p> <pre><code>SELECT ... FROM MyTable WHERE MyDate &gt;= '20091231' AND MyDate &lt;= '20100231' </code></pre> <p>Honestly, no other solution is acceptable in my mind. For ad-hoc queries, always use the unambiguous ISO standard for dates. For applications connecting to the database, always use bind parameters. If you're doing anything else, you're writing code that's either unreliable, insecure, or both.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload