I have a table stored in SQL Server database. One of the fields of the table is of type DATETIME
. I also have a ASP Calendar
whose date is compared with the DATETIME
filed of Database.
The problem is I don't want to compare the time. Only dates has to be compared. What is the SQL query for doing so.
This is what I used:
SqlCommand myCommand = new SqlCommand("
SELECT AUDIT.AUDIT_DETAILS, USERS.USER_NAME, AUDIT.DATE_TIME, IP
FROM USERS JOIN AUDIT ON (USERS.USER_ID = AUDIT.USER_ID)
WHERE USERS.USERS_NAME LIKE '%"+TextBox1.Text+"%' AND CONVERT(VARCHAR(10),AUDIT.DATE_TIME,110) like "+Calendar1.SelectedDate.Date+"'%'", conn);
I'm getting correct output in the SQL Server explorer but when run from browser through asp.net the query result is empty
You can get only date from Datetime in SQL like this
CONVERT(DATE,GETDATE())
And after that you can compare to that value.
Try modifying your query as below
SqlCommand myCommand = new SqlCommand("SELECT AUDIT.AUDIT_DETAILS, USERS.USER_NAME, AUDIT.DATE_TIME, IP
FROM USERS JOIN AUDIT ON (USERS.USER_ID = AUDIT.USER_ID)
WHERE USERS.USERS_NAME LIKE '%"+TextBox1.Text+"%' AND
CONVERT(VARCHAR(10),AUDIT.DATE_TIME,106) == CONVERT(VARCHAR(10),CAST('"+Calendar1.SelectedDate.Date+"' as DATETIME),106)", conn);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.