Domanda

I have a SQL Server database that when I open it on screen I want it to just show the records that have been added today.

I have the date stored in a field called DateAdded and it is of DateTime2 data type.

I tried this

string dateAdded = today.ToShortDateString();

string query = "SELECT CustomerID, Title, FirstName, LastName, AppStatus 
FROM Customer     
WHERE DateAdded ='" + dateAdded + "'";

This displays no records which I would assume as it has the time attribute stored with the date.

I searched online to try and find the answer, but none seemed to be specific for just records added today.

I'm also going to need to search on date ranges based on user inputs, and it could be on any range for eg view records from 09/01/2013 to 18/03/2013.

What is the best way to go about writing the sql query for this?

EDIT - My Answer

The answer Carl gave worked, but I didn't understand how to change the values so I could have different date ranges. I though I should post my final result on here, which I based on the answers I received, so it might help someone else. :)

using (SqlConnection conn = new SqlConnection(connectionString))
        {
            int year = date1.Year;
            int month = date1.Month;
            int day = date1.Day;

            string dateAdded = year + "-" + month + "-" + day + " ";

            int year2 = date2.Year;
            int month2 = date2.Month;
            int day2 = date2.Day;

            string dateAdded2 = year2 + "-" + month2 + "-" + day2 + " ";

            string query = "SELECT CustomerID, Title, FirstName, LastName, AppStatus, DateAdded FROM Customer WHERE DateAdded >= @date1 AND DateAdded <= @date2";

            using (SqlCommand cmd = new SqlCommand(query, conn))
            {
                cmd.Parameters.AddWithValue("date1", dateAdded + "00:00:00");
                cmd.Parameters.AddWithValue("date2", dateAdded2 + "23:59:59");
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(holder);
            }
        }
È stato utile?

Soluzione

From the OP's responses changed the SQL to this

DECLARE @StartDate DATETIME2, @EndDate DATETIME2

SET @StartDate = DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)
SET @EndDate = DATEADD(day, DATEDIFF(day, 0, GETDATE()+1), 0)

SELECT CustomerID, Title, FirstName, LastName, AppStatus 
    FROM Customer     
    WHERE DateAdded >= @StartDate AND DateAdded < @EndDate

This will get anything on the current date (you can replace Getdate() with a date of your choice for re-usability)

Altri suggerimenti

How about this one:

var sqlCommand = new SqlCommand();
sqlCommand.CommandText = "SELECT CustomerID, Title, FirstName, LastName, AppStatus FROM Customer WHERE DateAdded >= @DateAdded";
sqlCommand.Parameters.AddWithValue("@DateAdded", DateTime.Today);

Try your SQL query like below..

Query:

string query = "SELECT CustomerID, Title, FirstName, LastName, AppStatus FROM Customer
WHERE CONVERT(VARCHAR(10), dateAdded, 101) = CONVERT(VARCHAR(10), @DateAdded, 101)";

Parametized Command :

string dateAdded = today.ToShortDateString();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = query ;
cmd.Parameters.AddWithValue("@DateAdded", dateAdded);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top