If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > How to get current date

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-17-07, 19:36
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 791
How to get current date

Hi all,

Can someone tell me if it is possible to select records having a timestamp by using the CURDATE() function?

I need to compare the current date with a timestamp record. Where I am having a problem is with the minutes and seconds on the timestamp. I don't need these as I only am concerned with the year, month and day.

Should I use in addition to the timestamp, a date field as well?

Thanks.. Frank
Reply With Quote
  #2 (permalink)  
Old 08-17-07, 21:26
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
could you please be a bit more specific

what is the datatype of the column in question? DATETIME? TIMESTAMP? DATE? or perhaps INTEGER with a unix timestamp?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 08-17-07, 21:38
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 791
It is of datatype timestamp. Sorry, I should have indicated that. It is minutes and seconds attached to the timestamp that make it impossible for me to get the right results. Or results that I need.
Reply With Quote
  #4 (permalink)  
Old 08-17-07, 21:52
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
where daColumn >= current_date
  and daColumn < dateadd(current_date, interval 1 day)
this is the "best practice" approach, as it allows the optimizer to use the index (if any) on the column
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 08-17-07, 21:54
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 791
Code:
SELECT *
FROM
`employee_UI_Error_Log`
WHERE
`employee_UI_Error_Log`.`stamp` =  'CURDATE()'
This is basically what I need. I need to limit my result set to those records of the day. Unfortunately, TIMESTAMP has minutes and seconds attached. I need the timestamp because I need the time for reports. I was hoping I could use timestamp for both of my needs.
Reply With Quote
  #6 (permalink)  
Old 08-17-07, 21:55
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
would you at least do me the courtesy of trying my query
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 08-17-07, 22:02
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 791
Quote:
Originally Posted by r937
would you at least do me the courtesy of trying my query
Yes of course Rudy. doing it now. I think we posted at the same time.
Reply With Quote
  #8 (permalink)  
Old 08-17-07, 22:05
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 791
Code:
SELECT *
FROM
`employee_UI_Error_Log`
WHERE
`employee_UI_Error_Log`.`stamp` >=  'current_date'
AND `employee_UI_Error_Log`.`stamp` < dateadd('current_date, interval 1 day')
Ok, this yields the following error message
"FUNCTION dateadd does not exist"
Reply With Quote
  #9 (permalink)  
Old 08-17-07, 22:14
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,609
You need to adjust the MySQL "crabby factor", or use date_add() with an underscore.

-PatP
Reply With Quote
  #10 (permalink)  
Old 08-17-07, 22:14
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
try it after removing all the quotes

you don't need the backticks, either
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #11 (permalink)  
Old 08-17-07, 22:15
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
ah, yes, thank you pat, in mysql it's date_add, not dateadd

but the quotes are defo wrong
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #12 (permalink)  
Old 08-17-07, 22:21
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 791
Thanks guys. When I added the underscore, the error went away however, shouldn't it be returning what is in the db? I have 2 test rows in there now and they will not return. Here is the DDL
Code:
SELECT *
FROM
employee_UI_Error_Log
WHERE
employee_UI_Error_Log.stamp >=  'current_date'
AND employee_UI_Error_Log.stamp < date_add(current_date, interval 1 day)
Reply With Quote
  #13 (permalink)  
Old 08-17-07, 22:28
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
okay, you removed the backticks but left a couple of the quotes

please remove all the quotes
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #14 (permalink)  
Old 08-17-07, 22:38
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 791
Quote:
Originally Posted by r937
okay, you removed the backticks but left a couple of the quotes

please remove all the quotes
Rudy, that worked. Thank you..

I have never experienced backticks that has ever stopped a query from working before. Is it good practice to write sql without them or is there a rule as to when to use them and not to?
Reply With Quote
  #15 (permalink)  
Old 08-17-07, 23:00
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
backticks are required whenever the table or column name (a) contains a special character, like a space, or (b) is a reserved word

since "best practice" says you should never do either, thus you will never need to use backticks at all

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On