skip to Main Content

I need to converting a statement from MySQL to Postgres. The error is on this statement "INTERVAL $3 DAY". I am following a certain tutorial on building web apps using Go.

MySQL – insrt:= INSERT INTO snippets (title, content, created, expires) VALUES(?, ?, UTC_TIMESTAMP(), DATE_ADD(UTC_TIMESTAMP(), INTERVAL ? DAY))

My current statement Postgres insrt:= INSERT INTO snippets (title, content, created, expires) VALUES ($1, $2, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP + INTERVAL $3 DAY)

I tried changing the statement using chat GPT but I stuck there.
"ERROR: syntax error at or near "$3" (SQLSTATE 42601)"

2

Answers


  1. The documentation says the argument to interval is a string. You could try:

    CURRENT_TIMESTAMP + INTERVAL '$3 DAY'
    

    Reading a tutorial like Go: working with PostgreSQL might be helpful.

    Login or Signup to reply.
  2. According to chat GPT 4 with my prompting I get this code snippet below, I have tried to test it and it worked, according to my GPT result gives 10 days ahead of its time as expected.

    insrt:= INSERT INTO snippets (title, content, created, expires) VALUES ($1, $2, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP + $3 * INTERVAL '1 day')
    

    Another approach of mine incase interval is unknown;

    insrt:= INSERT INTO snippets (title, content, created, expires) VALUES ($1, $2, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP + CAST($3 AS INTERVAL))
    

    Hope it helps

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search