In SQL Server I have a DATETIME
column which includes a time element.
Example:
'14 AUG 2008 14:23:019'
What is the best method to only select the records for a particular day, ignoring the time part?
Example: (Not safe, as it does not match the time part and returns no rows)
DECLARE @p_date DATETIME
SET @p_date = CONVERT( DATETIME, '14 AUG 2008', 106 )
SELECT *
FROM table1
WHERE column_datetime = @p_date
Note: Given this site is also about jotting down notes and techniques you pick up and then forget, I'm going to post my own answer to this question as DATETIME stuff in MSSQL is probably the topic I lookup most in SQLBOL.
Update Clarified example to be more specific.
Edit Sorry, But I've had to down-mod WRONG answers (answers that return wrong results).
@Jorrit: WHERE (date>'20080813' AND date<'20080815')
will return the 13th and the 14th.
@wearejimbo: Close, but no cigar! badge awarded to you. You missed out records written at 14/08/2008 23:59:001 to 23:59:999 (i.e. Less than 1 second before midnight.)
Date can be compared in sqlserver using string comparision: e.g.
In sqlserver
In C# Pass the short string of date value using ToShortDateString() function. sample: DateVariable.ToShortDateString();
How to get the DATE portion of a DATETIME field in MS SQL Server:
One of the quickest and neatest ways to do this is using
It avoids the CPU busting "convert the date into a string without the time and then converting it back again" logic.
It also does not expose the internal implementation that the "time portion is expressed as a fraction" of the date.
Get the date of the first day of the month
Get the date rfom 1 year ago