1

I have a table called order with userID, itemID , dateOfOrder

How do I insert a row with today's date?

I've tried for example:

INSERT INTO lending 
VALUES(111, 1, SELECT CURDATE())

but it doesn't work. any idea what is the correct syntax?

3
  • 2
    Maybe getdate()? What database? Commented Jan 13, 2013 at 20:15
  • SELECT CURDATE() works fine and it returns the correct date. my problem is with the syntax within the insert. Commented Jan 13, 2013 at 20:16
  • Ah, I see now. The SELECT is superfluous in this case. SQL statements aren't structured that way, you just call the function to get the value you need. Commented Jan 13, 2013 at 20:18

2 Answers 2

1

Problem solved:

INSERT INTO lending VALUES(111, 1, CURDATE())

instead of

INSERT INTO lending VALUES(111, 1, **SELECT** CURDATE())
Sign up to request clarification or add additional context in comments.

Comments

0

You didn't specify which RDBMS you are using, so here are two of the main ones: If you are using MySql look at the NOW() method. If you are using Oracle check out SYSDATE. For anything else, e.g. MS SQL, some quick googling should find you an answer.

5 Comments

Or simply use the standard CURRENT_DATE function. That works in nearly all DBMS.
I've always used NOW, SYSDATE, etc. because I always seem to need the time as well.
The (ANSI) standard function would be CURRENT_TIMESTAMP
@a_horse_with_no_name Good to know. And looking at the docs, NOW and CURRENT_TIMESTAMP are actually the same thing in MySQL - checks out in sqlfiddle. SYSDATE and CURRENT_TIMESTAMP turn out different in Oracle, but the timestamp can take a format mask according to the docs so it can probably work out in the end. Learned some new stuff, thanks =)
NOW() isn't good for me as it returns the date and hour... i need just the date. anyhow the problem has been solved.. see my reply above :)

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.