Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Im using DateTime.Now(); in saving transaction in my program and its saves like this "2/21/2014 7:21:56 PM". I want to get all the transaction within the day. How can I get all the transaction within "2/21/2014".


C#
string strQuery = "Select Item_Purchased_Number,ProductID,ProductName,quantity,Branch, Date_Purchased from Customer_Purchased Where Branch = 'Manila'" + "'AND Date_Purchased = '" + DateTime.Now.AddHours(-24).ToString() ;


This is my query. Please help me. Badly needed. Advance thanks. :)
Posted
Updated 21-Feb-14 1:18am
v4

OK, the first thing to notice is that DateTime.Now is a very specific time: it is literally the moment in time when you access the property. And when you subtract 24 hours from that, you get the same exact time, but yesterday. Since this time is accurate to milliseconds or less, it is very, very unlikely that your query will ever work, unless you are lucky and posts are made at the exact same time each day!
So, start by looking at DateTime.Now.Date[^] which discards the time part and goes back to midnight.
Then change your query to look at values greater than that:
C#
string strQuery = "Select Item_Purchased_Number,ProductID,ProductName,quantity,Branch, Date_Purchased from Customer_Purchased Where Branch = 'Manila'" + "'AND Date_Purchased >= '" + DateTime.Now.Date.ToString();
(or better still, use a parameterised query and don't convert to string - this can cause problems in some systems, since the string format can be very different depending on the PC system settings).

If you want specific days results, then the way I do it is to use the SQL DATEDIFF function:
C#
...WHERE DATEDIFF(d, Date_Purchased, " + DateTime.Now.ToString() + ")=0
Will find today, "=1" will be yesterday, and so forth.
 
Share this answer
 
Comments
DLSU-D Student 25-Feb-14 11:20am    
what if I will get the weekly or the monthly? DateTime.Now.Date.AddDays(-7) for weekly? Is this correct?
OriginalGriff 25-Feb-14 11:31am    
Weekly is not that simple, because generally speaking weeks start on the same day of the week each time, so you have to work it out based on the current day of the week instead - you can't just subtract 7 and hope! :laugh:
Try:
DateTime now = DateTime.Now;
DateTime startOfWeek = now.AddDays(0-now.DayOfWeek);
For weeks started on Sunday.

DLSU-D Student 25-Feb-14 11:26am    
@OriginalGriif there's an error "Incorrect syntax near '2'." What does this means?
OriginalGriff 25-Feb-14 11:33am    
It means I forgot to enclose the date - I normally use parameterized queries fro everything to avoid SQL Injection.
...WHERE DATEDIFF(d, Date_Purchased, '" + DateTime.Now.ToString() + "')=0
DLSU-D Student 25-Feb-14 12:58pm    
string strQuery = "Select Item_Purchased_Number,ProductID,ProductName,quantity,Branch, Date_Purchased from Customer_Purchased Where Branch = 'SM Dasmariñas'" + "'AND DATEDIFF(d, Date_Purchased, '" + DateTime.Now.ToString() +)=0);

Is this CORRECT? I get some errors. BTW Griff. Thanks for helping. :)
Please try is as below (SQL injection-proof).

Note:
For simplicity,I extracted date part only.So complete it with your other where clause validations.

C#
var sqlCommand = new SqlCommand();
sqlCommand.CommandText = "Item_Purchased_Number,ProductID,ProductName,quantity,Branch, Date_Purchased from Customer_Purchased where Date_Purchased >= @DatePurchased";
sqlCommand.Parameters.AddWithValue("@DatePurchased", DateTime.Today);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900