pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedCasting a collation in an ORDER BY ... COLLATE
3+ messages / 3 participants
[nested] [flat]
* Casting a collation in an ORDER BY ... COLLATE
@ 2022-05-21 23:38 Kip Cole <kipcole9@gmail.com>
0 siblings, 2 replies; 3+ messages in thread
From: Kip Cole @ 2022-05-21 23:38 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
I’m trying to cast a string value to a collation name without success. My suspicion is this isn’t possible
based upon the error message. But perhaps wiser heads than mine have a suggestion on how to do
this?
cldr_sql=# select * from models order by name collate 'en-x-icu'::regcollation;
ERROR: syntax error at or near "'en-x-icu'"
LINE 1: select * from models order by name collate 'en-x-icu'::regco…
Utilmately the objective is to interpolate the collation value into a prepared query so
this is just the first step to validate that casting a COLLATE name is possible or not.
Suggestions and ideas most welcome.
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Casting a collation in an ORDER BY ... COLLATE
@ 2022-05-21 23:53 David G. Johnston <david.g.johnston@gmail.com>
parent: Kip Cole <kipcole9@gmail.com>
1 sibling, 0 replies; 3+ messages in thread
From: David G. Johnston @ 2022-05-21 23:53 UTC (permalink / raw)
To: Kip Cole <kipcole9@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On Sat, May 21, 2022 at 4:38 PM Kip Cole <kipcole9@gmail.com> wrote:
> I’m trying to cast a string value to a collation name without success. My
> suspicion is this isn’t possible
> based upon the error message.
You cannot cast between a token that interpreted as a literal and one that
is interpreted as a name. For example: SELECT * FROM 'table_name'::???;
you need to supply an identifier in the FROM clause, not a literal.
Identifiers cannot be parameterized, so if you want to interpolate you need
to do so while building a dynamic query string. See the "format()"
function and "EXECUTE" command for ways to do this within the server. Or
use whatever client-side facilities you have at your disposal.
David J.
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Casting a collation in an ORDER BY ... COLLATE
@ 2022-05-21 23:55 Tom Lane <tgl@sss.pgh.pa.us>
parent: Kip Cole <kipcole9@gmail.com>
1 sibling, 0 replies; 3+ messages in thread
From: Tom Lane @ 2022-05-21 23:55 UTC (permalink / raw)
To: Kip Cole <kipcole9@gmail.com>; +Cc: pgsql-general@lists.postgresql.org
Kip Cole <kipcole9@gmail.com> writes:
> cldr_sql=# select * from models order by name collate 'en-x-icu'::regcollation;
> ERROR: syntax error at or near "'en-x-icu'"
> LINE 1: select * from models order by name collate 'en-x-icu'::regco…
You've got the syntax off a bit. The argument of COLLATE is an
identifier, not a string literal (or expression), so you should write
select * from models order by name collate "en-x-icu";
The double quotes are needed because most collation names don't
follow SQL identifier rules.
> Utilmately the objective is to interpolate the collation value into a prepared query so
> this is just the first step to validate that casting a COLLATE name is possible or not.
If you mean that you want to inject a run-time-variable collation
name, you can't, any more than you can inject (say) a run-time-variable
table name. You'd have to construct and execute a dynamic SQL string.
regards, tom lane
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2022-05-21 23:55 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-05-21 23:38 Casting a collation in an ORDER BY ... COLLATE Kip Cole <kipcole9@gmail.com>
2022-05-21 23:53 ` David G. Johnston <david.g.johnston@gmail.com>
2022-05-21 23:55 ` Tom Lane <tgl@sss.pgh.pa.us>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox