Note that there are some explanatory texts on larger screens.

plurals
  1. POMS SQL Date Only Without Time
    primarykey
    data
    text
    <h2>Question</h2> <p>Hello All,</p> <p>I've had some confusion for quite some time with essentially flooring a DateTime SQL type using T-SQL. Essentially, I want to take a DateTime value of say 2008-12-1 14:30:12 and make it 2008-12-1 00:00:00. Alot of the queries we run for reports use a date value in the WHERE clause, but I either have a start and end date value of a day and use a BETWEEN, or I find some other method.</p> <p>Currently I'm using the following: <code> WHERE CAST(CONVERT(VARCHAR, [tstamp], 102) AS DATETIME) = @dateParam </code></p> <p>However, this seems kinda clunky. I was hoping there would be something more simple like <code>CAST([tstamp] AS DATE)</code></p> <p>Some places online recommend using DATEPART() function, but then I end up with something like this:</p> <pre> <code> WHERE DATEPART(year, [tstamp]) = DATEPART(year, @dateParam) AND DATEPART(month, [tstamp]) = DATEPART(month, @dateParam) AND DATEPART(day, [tstamp]) = DATEPART(day, @dateParam) </code> </pre> <p>Maybe I'm being overly concerned with something small and if so please let me know. I just want to make sure the stuff I'm writing is as efficient as possible. I want to eliminate any weak links.</p> <p>Any suggestions?</p> <p>Thanks,<br /> C <br /><br /></p> <h2>Solution</h2> <p>Thanks everyone for the great feedback. A lot of useful information. I'm going to change around our functions to eliminate the function on the left hand side of the operator. Although most of our date columns don't use indexes, it is probably still a better practice.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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