0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Oracleで文字数やバイト数をカウント

Posted at

文字数カウント

LENGTH()

バイト数カウント

LENGTHB()

動作確認用SQL

Oracleの文字コードがAL32UTF8の場合の結果。

SELECT 

-- 文字数
--  ↓ 結果3
LENGTH('ABC') AS tes_1,
--  ↓ 結果5
LENGTH('あいうえお') AS tes_2,
--  ↓ 結果5
LENGTH('アイウエオ') AS tes_3,
--  ↓ 結果NULL
LENGTH('') AS tes_4,
--  ↓ 結果NULL
LENGTH(NULL) AS tes_5,

-- バイト数
--  ↓ 結果3
LENGTHB('ABC') AS tes_b_1,
--  ↓ 結果15
LENGTHB('あいうえお') AS tes_b_2,
--  ↓ 結果15
LENGTHB('アイウエオ') AS tes_b_3,
--  ↓ 結果NULL
LENGTHB('') AS tes_b_4,
--  ↓ 結果NULL
LENGTHB(NULL) AS tes_b_5

-- 何かしらテーブルを指定する必要あり
FROM TESTER

-- 1行だけでいい
WHERE ROWNUM < 2

マルチバイトは2~4バイト

例えば…

『あいうえお』は3バイト×5で15バイト。

『アイウエオ』は3バイト×5で15バイト。
半角カタカナにしたからといってバイト数が減るわけではないので注意。

長さ0の文字列はNULL扱い

Oracle触る時は注意。

バージョン

Oracle Database 11g Release 11.2.0.1.0 - 64bit Production

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?