PostgreSQL 文字のバイト数を取得する
- 作成日 2022.02.12
- PostgreSQL
- PostgreSQL
![PostgreSQL 文字のバイト数を取得する](https://mebee.info/wp-content/uploads/2019/09/20160627121830-890x500.jpg)
PostgreSQLで、文字のバイト数を取得する手順を記述してます。
環境
- OS Rocky Linux release 8.4 (Green Obsidian)
- PostgreSQL 14.0
- pgadmin 6.0
文字のバイト数を取得
文字のバイト数を取得する場合は、「octet_length」を使用します。
octet_length(文字列)
実際に、文字のバイト数を取得してみます。
select octet_length('abcde')
実行結果
![](https://mebee.info/wp-content/uploads/2021/10/image-234.png)
取得されていることが確認できます。
ひらがな
全角は3バイトで計算され、取得されます。
select octet_length('あいうえお')
実行結果
![](https://mebee.info/wp-content/uploads/2021/10/image-235.png)
半角カタカナでも同じです。
select octet_length('アイウエオ')
実行結果
![](https://mebee.info/wp-content/uploads/2021/10/image-236.png)
空白の扱い
空白の場合、半角は1バイトとしてカウントされ、全角は3バイトと計算されます。
select octet_length(' ')
実行結果
![](https://mebee.info/wp-content/uploads/2021/10/image-237.png)
全角の場合は3バイト
select octet_length(' ')
実行結果
![](https://mebee.info/wp-content/uploads/2021/10/image-238.png)
サロゲートペア
サロゲートペア文字も、1文字として4バイトでカウントされます。
select octet_length('😂😅😍')
実行結果
![](https://mebee.info/wp-content/uploads/2021/10/image-233.png)
-
前の記事
MySQL 時刻から時のみを取得する 2022.02.12
-
次の記事
firewall 指定したIPアドレスからのみssh接続を許可する 2022.02.13
コメントを書く