troubleshooting Question

DateDiff Minute GetDate Between C# vs SQL

Avatar of kvnsdr
kvnsdr asked on
Microsoft SQL Server
13 Comments2 Solutions1696 ViewsLast Modified:
I've been using the following C#/SQL combo to get a solution. It works as-is but I would like to have a pure-SQL solution if possible.

[C#/SQL Combo]
string strCurrentTimeOnly = DateTime.Now.ToShortTimeString(); // 10:17 AM
string strDayofWeek = DateTime.Now.DayOfWeek.ToString(); // Thursday
string sql = " Select * from tbl_Users where col3 = 'True' " +
" and DateDiff(n, col_TimeOnly, '" + strCurrentTimeOnly + "') between 1 and 1 " +
" Or col_DayOfWeek = '" + strDayOfWeek + "')";

[SQL using GetDate()]
" Select * from tbl_Users where col3 = 'True' " +
" and DateDiff(minute, col_TimeOnly, GetDate()) between 1 and 1 " +
" Or col_DayOfWeek = '" + strDayOfWeek + "')";

The SQL above returns nothing... I think the problem is GetDate() represents datetime and not just time only ....


Mark Wills
Topic Advisor
Join our community to see this answer!
Unlock 2 Answers and 13 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 13 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros