hi,
how can i compare todays date with a date in my database? lets say i want to compare today date with my tran_date in my table, what is the syntax to do it?
Questions:
1) What platform are you on? What hardware, what OS?
2) What version of Perl?
3) What database are you using?
4) What is the datatype of the date field you want
to compare with today's date? Hardy Merrill
Mission Critical Linux, Inc.
I'm not sure this will work - it's been quite a while since I've done anything with MySQL. I'm looking at the book "MySQL" by Paul Dubois for reference - on p.538 it describes a DATE_SUB function - that stands for DATE SUBTRACT.
If you want to subtract 1 day from today, I think you could use this:
SELECT DATE_SUB(SYSDATE(), INTERVAL 1 DAY)
You'll have to check out the other date/time functions that MySQL offers if you need to do other things.
But since your question was how to find the *difference* between 2 dates, one MySQL function I can see that might help is PERIOD_DIFF(period1, period2), and that gives you the difference in months. Another useful function might be TO_DAYS - use that to convert the dates into days since year 0, and then take the difference:
SELECT TO_DAYS(SYSDATE()) - TO_DAYS(TRAN_DATE) AS DATE_DIFF
FROM MY_TABLE
and that will(I think?) will give you the difference in days.
Another that may be helpful is UNIX_TIMESTAMP - if you give that function a date or datetime string, it will return the number of seconds since the Epoch and that date - you could so something like this:
SELECT UNIX_TIMESTAMP(SYSDATE()) as TODAY,
UNIX_TIMESTAMP(TRAN_DATE) as TRAN_DATE
then you could subtract TRAN_DATE from TODAY to get the number of seconds between the 2 dates, and then you'd have to convert that into days, hours, minutes, seconds. There are quite a few ways you could go about it - these are just a few ideas. You should check out
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.