postgresql 检查字符是否有效Postgres

zzlelutf  于 2023-08-04  发布在  PostgreSQL
关注(0)|答案(1)|浏览(139)

我正在将SQL服务器代码转换为Postgres,并试图检查字符串中任何位置的字符是否有效。
到目前为止的代码是(有部分围绕这个)

SELECT
string_agg(txml.substr_text,'') as stringagg
FROM (
SELECT
CASE
WHEN (substring('ÄZYasdXA', n, 1) similar to ('"[^a-zA-Z0-9_{}""""() *&%$#@!?/\;:,.—<>–+ =`|~]-"')) 
AND (substring('ÄZYasdXA', n, 1) <> '[') AND (substring('ÄZYasdXA', n, 1) <> ']')
AND (substring('ÄZYasdXA', n, 1) <> '^') 
THEN substring('ÄZYasdXA', n, 1)
ELSE '|'
END as substr_text
FROM (SELECT
* from generate_series(1,1000)) AS nums (n)
WHERE n <= LENGTH('ÄZYasdXA')) AS txml

字符串
我已经输入了一个in来检查,因为在SQL Server中做同样的事情会使其无效,但在这里没有这样做。我是否遗漏了什么,或者我是否需要尝试更改“[^a-zA-Z 0 -9_'{}"()*&%$#@!?/;:,.-<>-+ =`|~]-“一部分?

qmb5sa22

qmb5sa221#

请调试您的工作并检查结果:

select 
   substring('ÄZYasdXA', 1, 1), 
   substring('ÄZYasdXA', 1, 1) similar to ('"[^a-zA-Z0-9_{}""""() *&%$#@!?/\;:,.—<>–+ =`|~]-"') test1,
   (substring('ÄZYasdXA', 1, 1) <> '[') AND (substring('ÄZYasdXA', 1, 1) <> ']') as test2,
   (substring('ÄZYasdXA', 1, 1) <> '^') as test3,
   substring('ÄZYasdXA', 1, 1) as substring2;

字符串
结果:
| 测试1|测试2|测试3| substring2| substring2 |
| --|--|--|--| ------------ |
| 假的|真的|真的|Ä| Ä |
test失败,因此result将等于|
您可能需要将第一个测试更改为:

select substring('ÄZYasdXA',1,1) similar to ('[^a-zA-Z0-9_{}""""() *&%$#@!?/\;:,.—<>–+ =`|~]');


(返回true

相关问题