tags:

views:

64

answers:

2

I have a mail webform where I have to show the user only (-1) one day behind messages, so how do pass (yesterday)date as parameter and retrieve the only records of one day back ? This query is for 'ALL' messages, but I need to filter (yesterday) one day back messages and add a hyperlink or add in a dropdown ?

select MSG_SRNO,MSG_SUBJECT,MSG_ID,MSG_CHKD,
   DOF_SENT,DOF_SEEN from  MESSAGES_MAILBOX where  USER=1234
+1  A: 

In sql, try

SELECT DATEADD(day,-1, GETDATE());
Stephen Wrighton
can i use this as a SQL statement ?
ahmed
A: 

This should work:

DateTime yesterday = DateTime.Now.AddDays(-1); 

string query = string.Format("select MSG_SRNO,MSG_SUBJECT,MSG_ID,MSG_CHKD,DOF_SENT,DOF_SEEN from  MESSAGES_MAILBOX where DATEPART(year,DOF_SENT) = {0} AND DATEPART(month,DOF_SENT) = {1} AND DATEPART(day,DOF_SENT) = {2}",yesterday.Year,yesterday.Month, yesterday.Day);
alejandrobog
thank you, but could you explain me about this statement ?
ahmed
@alejandrobog: This is what SqlCommand + Parameters are for. You really shouldn't advocate straight string manipulation to create sql queries. This leads to applications that are vulnerable to sql injection attacks.
R0MANARMY
Since I have no context of the app this is just to illustrate the query. Also there no risk of sql injection in this sample, the parameter doesnt come from a user input.
alejandrobog
@ahmed the previous query select all messages from yesterday by comparing the year,month and day to yesterday.
alejandrobog
Thankyou all for your comments and help. I will give a try and let you know.
ahmed