Solved

datecheck in sql

Posted on 2001-09-13
6
233 Views
Last Modified: 2008-02-01
Hi,

I have a table with a smallDateTime Field. How can I check in a query whether this date is equal to the current date?

Thanks,
Floris

0
Comment
Question by:florisb
6 Comments
 
LVL 3

Accepted Solution

by:
ahoor earned 50 total points
ID: 6479524
If the table is your_table with a date column date_col:

select * from your_table
where  convert(char(10),date_col,112) = convert(char(10),getdate(),112)
0
 
LVL 2

Author Comment

by:florisb
ID: 6479592
Hmmm, but somehow it doesn't work in the query below; any ideas?

thanks so far.





SELECT *
WHERE club_id = @club_id AND convert(char(10),begindatum,112) <= convert(char(10), getdate(), 112)
ORDER BY begindatum
0
 
LVL 10

Expert Comment

by:bret
ID: 6479795
That would be because *that* query is not checking equality, it is checking "less than or equal", which is a very different thing.  You also don't have a "FROM" clause, which could cause some problems...


Try just:

select *
from <tablename>
where club_id = @club_id
AND begindatum <= getdate()
order by begindatum

-bret
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 5

Expert Comment

by:amitpagarwal
ID: 6480280
Hope the query below helps.
Thanks,
Amit.

SELECT * from tableName
WHERE club_id = @club_id AND
datediff(day, begindatum, getdate()) = 0
ORDER BY begindatum

In datediff, if smalldatetime values are used, they are converted to datetime values internally for the calculation. Seconds and milliseconds in smalldatetime values are automatically set to 0 for the purpose of the difference calculation.
0
 
LVL 3

Expert Comment

by:ahoor
ID: 6481860
Hangt er vanaf wat je wilt...

Datediff I would not recommend, personally.

Why didn't the query you gave work, except for the obvious missing 'from' clause?
0
 
LVL 2

Author Comment

by:florisb
ID: 6497947
Hi,

Ahoor, your code simply solved the 'problem'.

Thanks to all,
Floris.


0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you thought ransomware was bad, think again! Doxware has the potential to be even more damaging.
February 24, 2017 — On February 23, Travis Ormandy, a vulnerability researcher at Google, reported on Twitter (https://twitter.com/taviso/status/834900838837411840) that massive stores of data have been leaked by CloudFlare, a company that provide…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question