| |
|
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.
|
 |
|

08-17-07, 19:36
|
|
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
|
|

08-17-07, 21:26
|
|
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?
|
|

08-17-07, 21:38
|
|
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.
|
|

08-17-07, 21:52
|
|
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
|
|

08-17-07, 21:54
|
|
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.
|
|

08-17-07, 21:55
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
|
|
would you at least do me the courtesy of trying my query 
|
|

08-17-07, 22:02
|
|
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.
|
|

08-17-07, 22:05
|
|
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"
|
|

08-17-07, 22:14
|
|
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
|
|

08-17-07, 22:14
|
|
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
|
|

08-17-07, 22:15
|
|
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
|
|

08-17-07, 22:21
|
|
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)
|
|

08-17-07, 22:28
|
|
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
|
|

08-17-07, 22:38
|
|
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?
|
|

08-17-07, 23:00
|
|
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

|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|