3

I have an SQL SELECT statement to select records from my table based on the current date. What I want to do is compare the dates without the timestamps because the times do not match. I just care for the date.

Here is what I have:

string SQL = @"SELECT Trans_Set_Id FROM EDI10002 " +
             @"WHERE (Tp_Id = '" + tpid + "' AND Rec_Date = '" + DateTime.Now.ToShortDateString() + @"')";  

The issue I'm having is how do I get the date only from the field "Rec_Date"?

AdamL
  • 12,421
  • 5
  • 50
  • 74
hmakled
  • 353
  • 4
  • 12

6 Answers6

4

If you're using sql server 2008 and above, you could do this:

select cast( getdate() as date)
AdamL
  • 12,421
  • 5
  • 50
  • 74
2

What Database are you using?

Have a look at the DATEPART function for mssql http://msdn.microsoft.com/en-us/library/ms174420.aspx

Jras
  • 518
  • 3
  • 12
  • Take a look at this link, I think it has what you are looking for http://stackoverflow.com/questions/1503298/sql-statement-to-select-all-rows-from-previous-day – Jras Mar 08 '13 at 18:03
2

A slightly old-fashioned way to do it is:

string SQL = "SELECT Trans_Set_Id FROM EDI10002 WHERE (Tp_Id = '" + tpid + "' AND CONVERT(varchar(10),Rec_Date, 101) = CONVERT(varchar(10), " + DateTime.Now.ToShortDateString() + ", 101))";

you're basically converting to MM/dd/YYYY and then truncating off the end.

Edit: With getdate()

string SQL = "SELECT Trans_Set_Id FROM EDI10002 WHERE (Tp_Id = '" + tpid + "' AND CONVERT(varchar(10),Rec_Date, 101) = CONVERT(varchar(10), getdate(), 101))";
RandomUs1r
  • 4,010
  • 1
  • 24
  • 44
1

Try using TRUNC(Rec_Date) if you only care about the date portion and want to drop the time portion. Something like:

... "' AND TRUNC(Rec_Date) = '" + DateTime.Today...

(This worked in Oracle... it may just be an Oracle thing...)

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
1

The following should work in all versions of SQL Server, regardless of the type of Rec_Date:

SELECT Trans_Set_Id 
FROM EDI10002 
WHERE Tp_Id = @tpid 
      AND (Rec_Date >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)
           AND Rec_Date < DATEADD(day, DATEDIFF(day, 0, GETDATE()), 1))

The use of DATEADD(day, DATEDIFF(...), ...) gets the beginning of a specific day. This means that if you have an index defined on Rec_Date, it may be used to retrieve the requested rows.

Clockwork-Muse
  • 12,806
  • 6
  • 31
  • 45
1

First you want to get out of the habit of building dynamic sql statements using string concatenation, you want to use parameterized queries.

Secondly in your example, you don't need to supply the date from the client, it can all be done in sql.

string SQL = @"SELECT Trans_Set_Id FROM EDI10002 " +
   @"WHERE (Tp_Id = @tpid" + 
   @" AND convert(varchar(10, Rec_Date, 121) = CONVERT(varchar(10), getdate(), 121) )";

or if you're using 2008

string SQL = @"SELECT Trans_Set_Id FROM EDI10002 " +
   @"WHERE (Tp_Id = @tpid" + 
   @" AND cast(Rec_Date as date) = cast(getdate() as date) )";

Add @tpid as a paremter to your sql command, and you're done.

jmoreno
  • 12,752
  • 4
  • 60
  • 91