skip to Main Content

Is it possible to pass Date value without single quotes in select statements

Query :

select * from Schema_1.table_name where "Date" >=2019-01-01 and "Date"<=2020-12-01

Instead of

select * from Schema_1.table_name where "Date" >='2019-01-01' and "Date"<='2020-12-01'

Thanks in advance

2

Answers


  1. Is it possible to pass Date value without single quotes

    Yes. You can replace them with (named) dollar quotes $$: demo at db<>fiddle

    select * from Schema_1.table_name 
    where "Date" >= $$2019-01-01$$ --unnamed dollar quotes
      and "Date" <= $$2020-12-01$$;
    
    select * from Schema_1.table_name 
    where "Date" >= $here_is_a_date$2019-01-01$here_is_a_date$ --named dollar quotes
      and "Date" <= $here_is_another_date$2020-12-01$here_is_another_date$;
    

    The make_date() function accepts the year, month and day as integers, so you can get away without quotes of any kind:

    select * from Schema_1.table_name 
    where "Date" >= make_date(2019,1,1)
      and "Date" <= make_date(2020,12,1);
    

    Technically, you could also cast a date-looking integer to ::text first, then to a ::date, if you like being controversial:

    select * from Schema_1.table_name 
    where "Date" >= 20190101::text::date
      and "Date" <= 20201201::text::date;
    
    Login or Signup to reply.
  2. PostgreSQL provides another way, called “dollar quoting”, to write string constants, example

    select $$2024-05-10$$::date;
    

    Check the official documentation, for further info

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