单行函数·上

SQL> -- 大写 小写 首字母大写
SQL> select upper(‘hello world‘) 大写, lower(‘Hello World‘) 小写,initcap(‘hello world‘) 首字母大写 from dual;

大写                   小写                   首字母大写                                                                                                                                               
---------------------- ---------------------- ----------------------                                                                                                                                   
HELLO WORLD            hello world            Hello World

SQL> -- 字节和字符的统计
SQL> select length(‘中国‘) 字符,lengthb(‘中国‘) 字节 from dual;

字符       字节                                                                                                                                                                                  
---------- ----------                                                                                                                                                                                  
         2          6

SQL> -- 替换字符串里面的o
SQL> select replace(‘hello‘,‘o‘,‘a‘) from dual;

REPLACE(‘H                                                                                                                                                                                             
----------                                                                                                                                                                                             
hella

SQL> select replace(‘hello‘,‘o‘) from dual;

REPLACE(                                                                                                                                                                                               
--------                                                                                                                                                                                               
hell

SQL> select lengthb(replace(‘hello‘,‘o‘)) from dual;

LENGTHB(REPLACE(‘HELLO‘,‘O‘))                                                                                                                                                                          
-----------------------------                                                                                                                                                                          
                            4

SQL> -- 截取字符串的子串
SQL> select substr(‘hello world‘,3) from dual;

SUBSTR(‘HELLOWORLD                                                                                                                                                                                     
------------------                                                                                                                                                                                     
llo world

SQL> select substr(‘hello world‘,3,4) from dual;

SUBSTR(‘                                                                                                                                                                                               
--------                                                                                                                                                                                               
llo

SQL> /*
SQL> substr(‘‘,a) 从第a为开始截取到最后
SQL> substr(‘‘,a,b) 从第a位开始截取截取b个长度
SQL> */
SQL> -- 在一个字符串中查找某一个字符或者字符串
SQL> select instr(‘hello world‘,‘o‘) from dual;

INSTR(‘HELLOWORLD‘,‘O‘)                                                                                                                                                                                
-----------------------                                                                                                                                                                                
                      5

SQL> select instr(‘hello world‘,‘hello‘) from dual;

INSTR(‘HELLOWORLD‘,‘HELLO‘)                                                                                                                                                                            
---------------------------                                                                                                                                                                            
                          1

SQL> -- instr 查询第一次出现某一个字符或者字符串的位置
SQL> -- 左补齐和右补齐
SQL> select lpad(‘hello world‘,15,‘*‘) 左补齐,rpad(‘hello world‘,15,‘@‘) 右补齐 from dual;

左补齐                         右补齐                                                                                                                                                                  
------------------------------ ------------------------------                                                                                                                                          
****hello world                hello [email protected]@@@

SQL> -- 去掉字符串里面的某一个字符或者子串
SQL> select trim(‘hello world‘,‘hel‘) from dual;
select trim(‘hello world‘,‘hel‘) from dual
                         *
第 1 行出现错误:
ORA-00907: 缺失右括号

SQL> select trim(‘hel‘ from ‘hello world‘) from dual;
select trim(‘hel‘ from ‘hello world‘) from dual
       *
第 1 行出现错误:
ORA-30001: 截取集仅能有一个字符

SQL> select trim(‘h‘ from ‘hello world‘) from dual;

TRIM(‘H‘FROM‘HELLOWO                                                                                                                                                                                   
--------------------                                                                                                                                                                                   
ello world

SQL> -- trim只能去除一个字符,不能去除字符串
SQL> -- 数值函数
SQL> -- round 四舍五入
SQL> select round(42.965,2) 一,round(42.965,1) 二,round(42.965,0) 三,
  2  round(42.965,-1) 四,round(42.965,-2) 五 from dual;

一         二         三         四         五                                                                                                                                                 
---------- ---------- ---------- ---------- ----------                                                                                                                                                 
     42.97         43         43         40          0

SQL>  select trunc(42.965,2) 一,trunc(42.965,1) 二,trunc(42.965,0) 三,
  2   trunc(42.965,-1) 四,trunc(42.965,-2) 五 from dual;

一         二         三         四         五                                                                                                                                                 
---------- ---------- ---------- ---------- ----------                                                                                                                                                 
     42.96       42.9         42         40          0

SQL> select mod(10,3) from dual;

MOD(10,3)                                                                                                                                                                                             
----------                                                                                                                                                                                             
         1

SQL> select mod(3,10) from dual;

MOD(3,10)                                                                                                                                                                                             
----------                                                                                                                                                                                             
         3

SQL> spool off

时间: 2024-10-19 18:08:09

单行函数·上的相关文章

sql的基础语句-单行函数,dual,数字函数,日期函数,表连接,集合运算,分组报表,单行子查询,多行子查询

3. 单行函数 3.1 转换函数 select ascii('A'),chr(65) from dual; select to_char(1243123),1231451 from dual;靠左边的就是字符串,靠右边的就是数字 select to_char(123512a121) from dual;   --错误的写法,没有引号表示数字,但是数字里面包含了字母,不合法的输入值 select to_number('123141211') from dual; select to_number(

Oracle单行函数笔记

Oracle中单行函数的分类:1.字符函数substr():字符串截取select substr('我爱你,你知道么?',0,4) from dual执行结果:我爱你,length函数:字符串长度select length('我爱你,你知道么?') from dual执行结果:9Replace()函数:替换指定字符select replace('我爱你,你知道么?','你','的是他') from dual执行结果:我爱的是他,的是他知道么?upper函数:转字母大写select upper('

Using Single-Row Functions to Customize Output使用单行函数自定义输出

DUAL is a public table that you can use to view results from functions and calculations. SQL> select * from DUAL; D - X SQL> desc DUAL; Name                                      Null?    Type ----------------------------------------- -------- ------

数据库 day60,61 Oracle入门,单行函数,多表查询,子查询,事物处理,约束,rownum分页,视图,序列,索引

1.    oracle介绍 ORACLE数据库系统是美国ORACLE公司(甲骨文)提供的以分布式数据库为核心的一组软件产品,是目前最流行的客户/服务器(CLIENT/SERVER)或B/S体系结构的数据库之一.比如SilverStream就是基于数据库的一种中间件.ORACLE数据库是目前世界上使用最为广泛的数据库管理系统,作为一个通用的数据库系统,它具有完整的数据管理功能:作为一个关系数据库,它是一个完备关系的产品:作为分布式数据库它实现了分布式处理功能.但它的所有知识,只要在一种机型上学习

Oracle函数-单行函数-数字、日期、日期处理函数

函数的分类 单行函数:一个input对应一个output,input和output存在一一对应的关系 如lower 组函数:多个input,但是只对应一个output.如 sum() ============================================================== 单行函数 特点: 每行返回一个结果,输入输出存在一一对应的关系 能嵌套使用 ,一个函数的输出能做另外一个函数的输入 如:select lowner(upper('aa')) from dua

Oracle中的单行函数

Oracle中的单行函数 1 字符函数 UPPER()--将字符串转换为大写 SELECT UPPER('abc') FROM dual; LOWER()-将字符串转换为小写 SELECT LOWER('ABC') FROM dual; INITCAP()-首字母大写,其它字母小写. SELECT INITCAP('hello') FROM dual; REPLACE()-将指定的字符/字符串替换 SELECT REPLACE('abcd efg','cd','XXX') FROM dual;

Oracle系列:(8)单行函数

单行函数:只有一个参数输入,只有一个结果输出 多行函数或分组函数:可有多个参数输入,只有一个结果输出 测试lower/upper/initcap函数,使用dual哑表 select lower('www.BAIdu.COM') from dual; select upper('www.BAIdu.COM') from dual; select initcap('www.BAIdu.COM') from dual; 测试concat/substr函数,从1开始,表示字符,不论中英文 select

Oracle学习(3):单行函数

单行函数 单行函数: 操作数据对象 接受参数返回一个结果 只对一行进行变换 每行返回一个结果 可以转换数据类型 可以嵌套 参数可以是一列或一个值 ************************************************************************************************************************************************* 字符函数 **************************

oracle学习笔记单行函数

单行函数 只对一行进行变换  每行返回一个结果 单行函数分 字符.数值.日期.转换.通用 字符函数:大小写控制函数.字符控制函数 大小写控制函数:lower, upper, initcap 字符控制函数:concat,substr,length,instr,lpad|rpad,trim,replace lower,upper,initcap select lower('SQL') from dual; --结果 sql select upper('sql') from dual; --结果 SQ