Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
:sectnums:
:sectnumlevels: 5

= ASCII 函数

== 功能说明

`ASCII` 返回输入值转换为字符串后第一个字节的无符号整数值,返回类型为 `integer`。对于以 ASCII 字符开头的字符串,该值是首字符的 ASCII 码;后续字符不影响结果。

== 语法

[source,sql]
----
ASCII(value)
----

支持 Oracle 兼容的 `VARCHAR2`、`NUMBER`、`BINARY_FLOAT`、`BINARY_DOUBLE`、`DATE`、`TIMESTAMP` 和 `TIMESTAMP WITH TIME ZONE` 输入。数值先转换为字符串;日期和时间类型分别使用会话的 `nls_date_format`、`nls_timestamp_format` 和 `nls_timestamp_tz_format` 格式化,因此结果可能随格式设置改变。输入为 `NULL` 或转换后的字符串为空时返回 `NULL`。

== 示例

[source,sql]
----
SELECT ascii('abc') FROM dual; -- 97,即小写字母 a
SELECT ascii('xyz') FROM dual; -- 120,即小写字母 x
SELECT ascii(321) FROM dual; -- 51,即数字字符 3
SELECT ascii('') IS NULL FROM dual; -- true
SELECT ascii(to_char('2026-01-01'::date, 'DD-MON-YYYY HH24:MI:SS')) FROM dual; -- 48
----

== 注意事项

对于上述 Oracle 兼容类型的实现,非 ASCII 字符返回其在数据库编码中第一个字节的值,而不是 Unicode 码点或完整多字节字符的数值。例如,在 UTF8 数据库中,`中` 的首字节为十六进制 `E4`,对应十进制 228。结果取决于编码及调用时选择的函数重载;不要将其用于取得非 ASCII 字符的 Unicode 码点。
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
:sectnums:
:sectnumlevels: 5

= ASCII function

== Description

`ASCII` returns the unsigned integer value of the first byte of the input converted to a string, as an `integer`. For a string beginning with an ASCII character, this is its ASCII code; subsequent characters do not affect the result.

== Syntax

[source,sql]
----
ASCII(value)
----

The Oracle-compatible overloads accept `VARCHAR2`, `NUMBER`, `BINARY_FLOAT`, `BINARY_DOUBLE`, `DATE`, `TIMESTAMP`, and `TIMESTAMP WITH TIME ZONE`. Numbers are converted to strings first. Date and time values are formatted using the session's `nls_date_format`, `nls_timestamp_format`, and `nls_timestamp_tz_format`, respectively, so the result can depend on those settings. A NULL input or an empty converted string returns NULL.

== Examples

[source,sql]
----
SELECT ascii('abc') FROM dual; -- 97, lowercase a
SELECT ascii('xyz') FROM dual; -- 120, lowercase x
SELECT ascii(321) FROM dual; -- 51, digit character 3
SELECT ascii('') IS NULL FROM dual; -- true
SELECT ascii(to_char('2026-01-01'::date, 'DD-MON-YYYY HH24:MI:SS')) FROM dual; -- 48
----

== Non-ASCII characters

For the Oracle-compatible overloads, a non-ASCII character yields the value of its first byte in the database encoding, not its Unicode code point or the value of the complete multibyte character. For example, in a UTF8 database, the first byte of `中` is hexadecimal `E4`, or decimal 228. The result depends on the encoding and the overload selected for the call; do not use it to obtain Unicode code points for non-ASCII characters.
Loading