Click here to Skip to main content
15,888,011 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear all,

I would like to know, how can i can get the following sql server query below, to be read in c#:

select * from [dbo].[database] where UploadDate >= Convert(nvarchar(10), getdate() - 1, 101)

I have started by of creating the following code but I am not sure, if this may be the right approach. Any help would be much appreciated.

C#
var yesterday = DateTime.Today.AddDays(-1);
        string cmdstr = "select * from [dbo].[database] where UploadDate = 'yesterday' ";
Posted

The way I do it is:
SQL
SELECT * FROM MyTable WHERE DATEDIFF(d, UploadDate, GETDATE()) = 1
 
Share this answer
 
Comments
miss786 28-Feb-14 6:31am    
hi, Thank you for your suggestion. I am still a little unclear, how do i go about writing sql query in c#, to be able to retriever yesterday's date's data. Its currently throwing -- Conversion failed when converting date and/or time from character string.-- error for the above code in my original post. Many thanks for your time.
OriginalGriff 28-Feb-14 7:09am    
This may sound like a stupid question, but what did you declare UploadDate as in your database?
miss786 28-Feb-14 7:31am    
Hi, Thank you reply. 'UploadDate is declared as 'datetime' type in the database. I manage to get it work, not sure if its the right approach, but its currently working. Thank you for your time and support. Many thanks.
Did DateAdd[^] function for MS Access stop wroking? No!

SQL
SELECT DateAdd("d", -1, Date()) As Yesterday;
 
Share this answer
 
Working solution code:

C#
var yesterday = DateTime.Today.AddDays(-1);
string cmdstr = "select * from [dbo].[database] where UploadDate = @value";
SqlCommand cmd = new SqlCommand(cmdstr, conn);
cmd.Parameters.AddWithValue("@value", yesterday);
 
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