FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


MySQL Date Addition with Variable Interval Type





mexiwithacan
Here's something that's been driving me crazy for a while now: I cannot get MySQL to perform date additions using a variable to indicate the type of interval being added or subtracted.

For example, the following SQL query works in returning tomorrow's date:

Code:
SET @interval_count = 1;
SELECT CURDATE() + INTERVAL @interval_count DAY;


However, when I instead want to specify the interval type instead of the interval count with a variable, MySQL doesn't understand my request. The following SQL query does not work:

Code:
SET @interval_type = DAY;
SELECT CURDATE() + INTERVAL 1 @interval_type;


Does anyone know how I can vary the interval type in date arithmetic with a variable easily and gracefully instead of having to resort to CASE statements?
mexiwithacan
Peter Brawley helped me out again on the MySQL Developer Forums by responding to my thread there.

Basically, the PREPARE statement was the missing piece to the puzzle I was trying to solve.

On an interesting note, I searched for "Peter Brawley" on Google and discovered that he has authored a book on MySQL, which helps to explain why he's been so quick to respond to two of my more burning and frustrating questions.
Related topics
PHP/Mysql - beginner!
Creating a page with php
FEEDBACK PLEASE! My art glass website.
Formatting mySQL date
SQL.....Where to start?
SQL Line breaking?
MySql DATE and ORDERING something by the DATE and displaying
Adding Comments to a Webpage
How can I check...
Number or date/time field for timestamp?
Check the closest event in Calendar?
time related problem
Checkbox and Date filter with mysql and PHP
Help with a query[solved]
Reply to topic    Frihost Forum Index -> Support and Web Hosting -> Web Hosting Support

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.