本篇内容主要讲解“数据库中TO_CHAR格式转换的方法是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“数据库中TO_CHAR格式转换的方法是什么”吧!
我的经理叫我写一个函数,这个函数接受一个正整数并返回相应的序数(比如,2->2nd, 145->145th), 这个序数是一个字符串。这个函数只接受1和50,000之间的整数。
下列的哪些选项实现了函数plch_n_to_nth, 从而使得它满足上述需求,在下列块被执行之后:
BEGIN
DBMS_OUTPUT.put_line (plch_n_to_nth (1));
DBMS_OUTPUT.put_line (plch_n_to_nth (8));
DBMS_OUTPUT.put_line (plch_n_to_nth (256));
DBMS_OUTPUT.put_line (plch_n_to_nth (25763));
END;
/
我能见到如下输出:
1st
8th
256th
25763rd
(A)
CREATE OR REPLACE FUNCTION plch_n_to_nth (n_in IN INTEGER)
RETURN VARCHAR2
IS
BEGIN
RETURN TO_CHAR (n_in, '9999th');
END;
/
SQL> BEGIN
2 DBMS_OUTPUT.put_line (plch_n_to_nth (1));
3 DBMS_OUTPUT.put_line (plch_n_to_nth (8));
4 DBMS_OUTPUT.put_line (plch_n_to_nth (256));
5 DBMS_OUTPUT.put_line (plch_n_to_nth (25763));
6 END;
7 /
BEGIN
DBMS_OUTPUT.put_line (plch_n_to_nth (1));
DBMS_OUTPUT.put_line (plch_n_to_nth (8));
DBMS_OUTPUT.put_line (plch_n_to_nth (256));
DBMS_OUTPUT.put_line (plch_n_to_nth (25763));
END;
ORA-01481: 无效的数字格式模型
ORA-06512: 在 "YOGA.PLCH_N_TO_NTH", line 5
ORA-06512: 在 line 2
SQL>
(B)
CREATE OR REPLACE FUNCTION plch_n_to_nth (n_in IN INTEGER)
RETURN VARCHAR2
IS
BEGIN
RETURN LOWER (
TO_CHAR (
TO_DATE ('1-1-' || n_in, 'dd-mm-yyyy')
, 'FMYYYYth'));
END;
/
SQL> BEGIN
2 DBMS_OUTPUT.put_line (plch_n_to_nth (1));
3 DBMS_OUTPUT.put_line (plch_n_to_nth (8));
4 DBMS_OUTPUT.put_line (plch_n_to_nth (256));
5 DBMS_OUTPUT.put_line (plch_n_to_nth (25763));
6 END;
7 /
1st
8th
256th
BEGIN
DBMS_OUTPUT.put_line (plch_n_to_nth (1));
DBMS_OUTPUT.put_line (plch_n_to_nth (8));
DBMS_OUTPUT.put_line (plch_n_to_nth (256));
DBMS_OUTPUT.put_line (plch_n_to_nth (25763));
END;
ORA-01830: 日期格式图片在转换整个输入字符串之前结束
ORA-06512: 在 "YOGA.PLCH_N_TO_NTH", line 5
ORA-06512: 在 line 5
SQL>
(C)
CREATE OR REPLACE FUNCTION plch_n_to_nth (n_in IN INTEGER)
RETURN VARCHAR2
IS
BEGIN
RETURN LOWER (
TO_CHAR (
TO_DATE ('1-1-2011 ' || n_in
, 'dd-mm-yyyy SSSSS')
, 'FMSSSSSth'));
END;
/
SQL> BEGIN
2 DBMS_OUTPUT.put_line (plch_n_to_nth (1));
3 DBMS_OUTPUT.put_line (plch_n_to_nth (8));
4 DBMS_OUTPUT.put_line (plch_n_to_nth (256));
5 DBMS_OUTPUT.put_line (plch_n_to_nth (25763));
6 END;
7 /
1st
8th
256th
25763rd
PL/SQL procedure successfully completed
SQL>
(D)
CREATE OR REPLACE FUNCTION plch_n_to_nth (
n_in IN INTEGER)
RETURN VARCHAR2
IS
c_last_digit CONSTANT PLS_INTEGER
:= MOD (n_in, 10) ;
c_tens_digit CONSTANT CHAR (1)
:= SUBSTR ('0' || TO_CHAR (n_in), -2, 1) ;
BEGIN
RETURN TO_CHAR (n_in)
|| CASE
WHEN c_tens_digit = '1' THEN 'th'
WHEN c_last_digit = 1 THEN 'st'
WHEN c_last_digit = 2 THEN 'nd'
WHEN c_last_digit = 3 THEN 'rd'
ELSE 'th'
END;
END plch_n_to_nth;
/
SQL> BEGIN
2 DBMS_OUTPUT.put_line (plch_n_to_nth (1));
3 DBMS_OUTPUT.put_line (plch_n_to_nth (8));
4 DBMS_OUTPUT.put_line (plch_n_to_nth (256));
5 DBMS_OUTPUT.put_line (plch_n_to_nth (25763));
6 END;
7 /
1st
8th
256th
25763rd
PL/SQL procedure successfully completed
SQL>
CD.
(A)你不能在TO_CHAR(NUMBER)中使用TH格式,只能在TO_CHAR (datetime)中使用。
(B)差点就对了,这个答案在年份使用了th格式, 但ORACLE不支持高达50,000的年份,会出现如下错误:
ORA-01830: date format picture ends before converting entire input string
(C)这个正确使用了TO_CHAR (datetime)的th格式,一天中有86400秒,足够使用。
(D)这个自定义函数正确实现了转换规则。
到此,相信大家对“数据库中TO_CHAR格式转换的方法是什么”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!