We have some encoding issues and I need to check whether a BOM is already present in a PostgreSQL text column. I used

select convert(varbinary, columnXY) from tableXY where id = 1;

for MS SQL successfully, but don't find equivalent conversions for PostgreSQL. I found this documentation and tried with decode(columnXY, 'hex'), but that is not working.

有帮助吗?

解决方案

You may consider the binary representation of the TEXT column by converting it to BYTEA (edit: not by a direct cast, better use convert_to(text,'UTF-8') instead) and searching the BOM sequence in it as a series of bytes.

as an SQL expression:

position('\xefbbbf'::bytea IN convert_to(your_text_column,'UTF-8'))=1

0 as the result of position(...) would mean the BOM is not in the string.
1 means it's at the beginning of the string.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top