Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Server Query on todays date using datetime2
    primarykey
    data
    text
    <p>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.</p> <p>I have the date stored in a field called DateAdded and it is of DateTime2 data type.</p> <p>I tried this</p> <pre><code>string dateAdded = today.ToShortDateString(); string query = "SELECT CustomerID, Title, FirstName, LastName, AppStatus FROM Customer WHERE DateAdded ='" + dateAdded + "'"; </code></pre> <p>This displays no records which I would assume as it has the time attribute stored with the date.</p> <p>I searched online to try and find the answer, but none seemed to be specific for just records added today.</p> <p>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.</p> <p>What is the best way to go about writing the sql query for this?</p> <p>EDIT - My Answer</p> <p>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. :)</p> <pre><code>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 &gt;= @date1 AND DateAdded &lt;= @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); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload