1

I have a problem with PostgreSQL to_date() function, in my code mentioned bellow I get syntax error but can't find where.

SELECT * from crosstab('select upit, sat, count(*) as broj
                                from upiti
                                where datum >= to_date('26-10-2014','DD-MM-YYYY') AND datum <= to_date('29-10-2014','DD-MM-YYYY')
                                group by upit,sat
                                ORDER BY upit,sat',

'select rbrSat from sat order by rbrSat') as ct (upit text, s00 INT, s01 INT, s02 INT, s03 INT, s04 INT, s05 INT, s06 INT, s07 INT, s08 INT, s09 INT, s10 INT, s11 INT, s12 INT, s13 INT, s14 INT, s15 INT, s16 INT, s17 INT, s18 INT, s19 INT, s20 INT, s21 INT, s22 INT, s23 INT);

It says that syntax error is near "2014". Anyone knows where is the problem?

4
  • 1
    This snippet looks fine. You should edit the question and add in the full query (and, you probably don't need to_date() when using dates in YYYY-MM-DD format). Commented Oct 26, 2014 at 15:00
  • I suspect you have a missing end quote ' earlier in the query, making the previous string end just before 2014 instead of starting just before it. Commented Oct 26, 2014 at 15:01
  • edited with full query Commented Oct 26, 2014 at 15:11
  • yeah i pasted the wrong snippet with that YYYY, but te real problem was in ' that was supposed to be ''. Commented Oct 26, 2014 at 19:03

1 Answer 1

3

You're trying to use unescaped single quotes ' inside a single quoted string. If you need to add a single quote inside a string, it needs to be doubled, as in '';

SELECT * from crosstab('select upit, sat, count(*) as broj
                        from upiti
                        where datum >= to_date(''26-10-2014'',''DD-MM-YYYY'') 
                          AND datum <= to_date(''29-10-2014'',''DD-MM-YYYY'')
                            group by upit,sat
                            ORDER BY upit,sat',

...

Sign up to request clarification or add additional context in comments.

2 Comments

Better to use dollar quoting here ($$), in fact, quite handy.
Using an ANSI date literal: DATE '2014-10-26' makes it even shorter

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.