You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PostgreSQL中生成不含I和O的字符序列?

Solution to Generate Character Sequence Excluding I and O

Got it, let's fix this for you. Your original query generates characters from A to J, but you want to exclude both I and O (even though O isn't in your current range, it's smart to build for future flexibility).

Here are two straightforward, practical ways to achieve this:

1. Filter by Character Value (Easy to Read)

Add a WHERE clause to directly exclude the unwanted characters. This approach is intuitive and works seamlessly if you expand your character range later:

SELECT chr(a)
FROM generate_series(ascii('A'), ascii('A') + 10 - 1, 1) a
WHERE chr(a) NOT IN ('I', 'O');

It converts each generated ASCII number back to a character, then checks if it's in the excluded list. For your original A-J range, this will remove I and leave you with A-H, J. If you later extend the range to include O (like going up to Z), the condition will automatically exclude it too.

2. Filter by ASCII Code (Slightly More Efficient)

Skip the character conversion step and filter directly on the numeric ASCII values. This saves a tiny bit of processing, which adds up if you're generating a large sequence:

SELECT chr(a)
FROM generate_series(ascii('A'), ascii('A') + 10 - 1, 1) a
WHERE a NOT IN (ascii('I'), ascii('O'));

Here, we use ascii('I') and ascii('O') to get their numeric equivalents (73 and 79) and exclude those numbers from the generated series. The result is exactly the same as the first method, but it avoids an extra chr() conversion for each row.

内容的提问来源于stack exchange,提问作者smiss

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:53:55