troubleshooting Question

SQL Query - DateTime

Avatar of Endelm
Endelm asked on
Microsoft SQL ServerMicrosoft SQL Server 2005SQL
7 Comments1 Solution310 ViewsLast Modified:
Hi,

I have a table with a lot of datetimes.

SELECT dtField
FROM MyTable
ORDER BY dtField DESC

Gives me:

ResultNr         dtField
1                     2007-05-22
2                     2007-05-21
3                     2007-05-18
..                     ..

How can I make a SELECT query that returns me a single row:

select...from MyTable......where dtField = 'SomeDate'

...so that:
- If SomeDate exists, I get that.
- If SomeDate doesn´t exist, I get the date that happened just before that.

F.EX select.....from MyTable where dtField = '2007-05-22'    -- returns me the date 2007-05-22

F.EX select.....from MyTable where dtField = ' 2007-05-20'    -- returns me the date2007-05-18 (because  ' 2007-05-20' doesn't exist)

Does anybody know how to do this?

Thank you.
ASKER CERTIFIED SOLUTION
derekkromm

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 7 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 1 Answer and 7 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