Home > Database > Oracle > body text

How to query the length of a field in Oracle

WBOY
Release: 2022-02-18 16:28:49
Original
41255 people have browsed it

In Oracle, you can use the select statement with the length() method to query the length of the field. length represents the character length of the string. The select statement is used for simple data query. The syntax is "select length( field name) from table name".

How to query the length of a field in Oracle

The operating environment of this tutorial: Windows 10 system, Oracle 11g version, Dell G3 computer.

How to query the length of a field in Oracle

To query the length of a field in Oracle, you can use

select length(字段名) from 表名;
Copy after login

This sentence is to look at the length of all fields in the table

If it is

select length(字段名) from 表名
where 要查找那个记录;
Copy after login

A simple query uses the SELECT command to extract data from the table. The SELECT command structure is as follows:

select command structure:

select *|列名|表达式 from 表名 where 条件 order by 列名
Copy after login

In oracle, compare Common ones may be length and substr,

length represents the character length of the string,

lengthb represents the byte length of the string;

substr represents the Get the substring by character length,

substrb means get the string based on the byte length.

Let’s look directly at the example to illustrate:

SELECT length('叶德华abc') -- length按字符计,汉字、英文、数字都是1个字符,故这里返回6
  FROM dual;
SELECT lengthb('叶德华abc') -- length按字节计,我这里是UTF-8编码,汉字3个字节,英文一个字节,故这里返回12
  FROM dual;
SELECT substr('叶德华abc', -- substr按字符截取,截取到a,返回:叶德华a
              1,
              4)
  FROM dual;
SELECT substrb('叶德华abc',
               1,
               2) -- substrb按字节截取,2不足一个汉字长度,返回:两个空格
  FROM dual;
SELECT substrb('叶德华abc',
               1,
               3) -- substrb按字节截取,3刚好是一个汉字长度,返回:叶
  FROM dual;
SELECT substrb('叶德华abc',
               1,
               4) -- substrb按字节截取,4多余一个汉字少于两个汉字,返回:叶 加一个空格
  FROM dual;
Copy after login

Recommended tutorial: "Oracle Video Tutorial"

The above is the detailed content of How to query the length of a field in Oracle. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template