I use concatenation to prepare a query string with parameter values
When I use single quotes:
p_ReqStr_old := '
with prm as
(
select
1::int4 as id, ' || '
to_timestamp(''' || to_char(p_BegRepDate, 'DD.MM.YYYY') || ''',''DD.MM.YYYY'')::timestamp as p_BegDate,
to_timestamp(''' || to_char(p_EndRepDate, 'DD.MM.YYYY') || ''',''DD.MM.YYYY'')::timestamp as p_EndDate, ' ||
cast(p_IsIncludeTestLpu as varchar(1)) || '::int8 as p_IsIncludeTestLpu
)';
I receive the following result:
with prm as
(
select
1::int4 as id,
to_timestamp('01.01.2023','DD.MM.YYYY')::timestamp as p_BegDate,
to_timestamp('31.01.2023','DD.MM.YYYY')::timestamp as p_EndDate, 2::int8 as p_IsIncludeTestLpu
)
When I use dollar quoting:
p_ReqStr_new := $$
with prm as
(
select
1::int4 as id,
to_timestamp(to_char(p_BegRepDate, 'DD.MM.YYYY'), 'DD.MM.YYYY')::timestamp as p_BegDate,
to_timestamp(to_char(p_EndRepDate, 'DD.MM.YYYY'), 'DD.MM.YYYY')::timestamp as p_EndDate,
cast(p_IsIncludeTestLpu as varchar(1))::int8 as p_IsIncludeTestLpu
)$$;
I receive this undesirable result:
with prm as
(
select
1::int4 as id,
to_timestamp(to_char(p_BegRepDate, 'DD.MM.YYYY'), 'DD.MM.YYYY')::timestamp as p_BegDate,
to_timestamp(to_char(p_EndRepDate, 'DD.MM.YYYY'), 'DD.MM.YYYY')::timestamp as p_EndDate,
cast(p_IsIncludeTestLpu as varchar(1))::int8 as p_IsIncludeTestLpu
)
Is there a way to receive a result with date values with dollar quoting mechanics?
When you switched to dollar quoting, you also removed the concatenation. If you want to keep concatenation, then keep it. That does mean you would need to keep closing and reopening the dollar quotes on each side of the
||, just like you did when using the single quotes. Switching the quote mark just means you don't need to escape the literal single quote marks anymore, it doesn't change how concatenation works.Maybe you should use the FORMAT function instead.