-
I am using postgresql, pgadmin4 v9.3 and SQLPage v0.34.0 Calling sqlpage file with this url http://localhost:8080/do_report?SDate=1/4/25&EDate=30/4/25&PDate= In this file I have this code set EndDate =
case
when $id is not null
then (select "End" from reports where id=$id::int)
else
coalesce($EDate,'1/1/20')::date
end; If $EDate='30/4/25' the set operation fails with error
If If I put the sql shown in the error message
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
This is a classic date format localization issue in PostgreSQL. Here's what's happening: The ProblemPostgreSQL's date parsing behavior depends on the The format
SolutionsOption 1: Use Unambiguous Date Format (Recommended)Change your URL to use ISO format or a clearly unambiguous format:
Then your SQL becomes: set EndDate =
case
when $id is not null
then (select "End" from reports where id=$id::int)
else
coalesce($EDate,'2020-01-01')::date
end; Option 2: Explicit Date ParsingUse PostgreSQL's set EndDate =
case
when $id is not null
then (select "End" from reports where id=$id::int)
else
coalesce(to_date($EDate, 'DD/MM/YY'), '2020-01-01'::date)
end; Option 3: Set DateStyle in SQLPage ConnectionYou can set the datestyle in the connection initialization script. In SET datestyle = 'DMY'; -- Day-Month-Year format Option 4: Handle Multiple FormatsFor maximum flexibility, you could create a function to try multiple formats: set EndDate =
case
when $id is not null
then (select "End" from reports where id=$id::int)
else
coalesce(
case
when $EDate ~ '^\d{4}-\d{2}-\d{2}$' then $EDate::date
when $EDate ~ '^\d{1,2}/\d{1,2}/\d{2}$' then to_date($EDate, 'DD/MM/YY')
else '2020-01-01'::date
end,
'2020-01-01'::date
)
end; RecommendationI'd strongly recommend Option 1 - using ISO date format (
The discrepancy you're seeing between pgAdmin and SQLPage is likely due to different See: |
Beta Was this translation helpful? Give feedback.
-
Obvious when explained - thank you so much |
Beta Was this translation helpful? Give feedback.
This is a classic date format localization issue in PostgreSQL. Here's what's happening:
The Problem
PostgreSQL's date parsing behavior depends on the
datestyle
setting. When you run SQL directly in pgAdmin, it's using your local PostgreSQL session settings, but SQLPage may be connecting with different locale/datestyle settings.The format
30/4/25
is ambiguous - it could be:Solutions
Option 1: Use Unambiguous Date Format (Recommended)
Change your URL to use ISO format or a clearly unambiguous format:
Then your SQL becomes:
set En…