4

i would need to help how can i save into my variable @today from CURRENT_DATE() value on VARCHAR type as you can see in first SET row and in the second row in my variable @yesterday i would like to save the value in DATE type.

SET @today=CAST(CURRENT_DATE() AS VARCHAR(50)),
@yesterday=CAST(DATE_ADD(CURRENT_DATE, INTERVAL -1 DAY) AS DATE)

SELECT @today, @yesterday

Thanks for your help

1
  • why store as var-char? If you select a date-type field out from mysql, it'll be a string yyyy-mm-dd in pretty much all clients. Commented Sep 12, 2013 at 18:51

1 Answer 1

5

That may help:

SELECT CAST(CURRENT_DATE() AS CHAR(50)) INTO @today;
SELECT CAST(DATE_ADD(CURRENT_DATE, INTERVAL -1 DAY) AS DATE) INTO @yesterday;

SELECT @today, @yesterday

EDIT: Your version also works, but You can't cast into VARCHAR()

SET @today=CAST(CURRENT_DATE() AS CHAR(50)) , 
@yesterday=CAST(DATE_ADD(CURRENT_DATE, INTERVAL -1 DAY) AS DATE) ;
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your comment it is helpful, but the result of @yesterday is (BLOB). Is it correct?
How can I use a variable within the Where clause?
@Reya276 first you need to SET it (as @today) , and then WHERE YOUR_DATE=@TODAY

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.