My table is as below:
a | b
-----
1 | a
2 | b
In my query, I want to change the order by clause depending on the type of the column.
So something like
get_data($order_by_column) {
....
ORDER BY
CASE
WHEN is_numeric($order_by_column) THEN $order_by_column
ELSE CHAR_LENGTH($order_by_column)
END
}
I’ve checked and it seems it’s quite difficult to determine the column type dynamically like in the above query. What would be an alternative (or alternatives) to achieve this?
You might be interested in
pg_typeof():Note that the legs of a
CASEstatement need to return matching types, which happens to be the case here, since$order_by_columnandlength($order_by_column)both returninteger.More about object identifier types like
regtypein the manual.Find all registered types:
Again, more about
pg_typein the manual.