Home > Database > Mysql Tutorial > 5.单行函数,多行函数,字符函数,数字函数,日期函数,数据类型

5.单行函数,多行函数,字符函数,数字函数,日期函数,数据类型

WBOY
Release: 2016-06-07 16:04:48
Original
1394 people have browsed it

1 多行函数(理解:有多个输入,但只输出1个结果) SQLselect count(*) from emp; COUNT(*) ------------- 14 B 字符函数Lower select Lower(Hello) 转小写, upper(hello) 转大写, initcap(hello woRld) 首字母大写 from dual; 结果: 转小 转大 首字母大写

1 多行函数(理解:有多个输入,但只输出1个结果)

SQL>select count(*) from emp;

COUNT(*)

-------------

14

B 字符函数Lower

select Lower('Hello') 转小写,

upper('hello') 转大写,

initcap('hello woRld') 首字母大写

from dual;

结果:

转小 转大 首字母大写

---------- -----------

helloHELLO Hello World

C字符函数 concat:

select concat('hello','world') from dual

concat函数嵌套:

select concat (concat('hello', 'world'),'cccc') 函数嵌套 from dual;

函数嵌套

--------------

helloworldcccc

D字符函数 substr()

SUBSTR(a,b) --- 从a中第b位去字符串

substr(a, b, c) 从a中第b位, c个字符....

select substr('abcdefg1111', 2) 从第二位取子串,

substr('abcdefg1111', 2, 4) 取4个字符

from dual;

从第二位取取4

----------------- ------

bcdefg1111 bcde

E length字符数 lengthb字节数

注意:一个中文字符,两个字节

select length('中国abc') 字符数,

lengthb('中国abc') 字节数

from dual;

运行结果:

字符数 字节数

---------- ----------

5

F instr(),lpad(),rpad(),trim()函数

select instr('abcdefg', 'efg') 求子串位置 from dual;

求子串位置

----------------

5

selectlpad('abcd', 10, '*') lpad, rpad('abcd', 10, '*') rpad from dual;

LPAD RPAD

---------- ----------

******abcd abcd******

--trim 去掉前后指定的字符,字符可以是空格,也可以不是空格 .

selecttrim('A' from 'ABCDEFg') from dual;

TRIM('

------

BCDEFg

2 数字函数

第二个参数

含义

2> 0

保留2位

= 0

保留到个位

-1

保留到10位

-2

保留到百位(要看十位四舍五入)

selectround(45.926, 2) AA,

round(45.926, 1) BB,

round(45.926, 0) CC,

round(45.926) DD,

round(45.926, -1) EE,

round(45.926, -2) FF

from dual;

结果:

AA BB CC DD EE FF

---------- ---------- ---------- ---------- ---------- ----------

45.93 45.9 46 46 50 0

3 日期函数

时间的计算.......

oracle数据库中 date包含日期和时间

mysql 3中数据类型 date 、time 、times.

A selectto_char(sysdate, 'yyyy-mm-dd hh24:mi:ss') from dual ;

TO_CHAR(SYSDATE,'YY

-------------------

2014-10-0716:34:17

B ----昨天今天明天

select sysdate-1 昨天,

sysdate 今天,

sysdate+1 明天

from dual;

selectto_char(sysdate-1, 'yyyy-mm-dd hh24:mi:ss') 昨天,

sysdate 今天,

sysdate+1 明天

from dual;

C ----查询员工的入职时间,按照周 月 年方式显示.....

select(sysdate-hiredate)/7 周,

(sysdate-hiredate)/30 月,

(sysdate-hiredate)/365 年

from emp;

 

select (sysdate-hiredate)/7 周,

(sysdate-hiredate)/30 月,

(sysdate-hiredate)/365

from emp;

周 月 年

---------- ---------- ----------

1763.95625 411.589791 33.8292979

1754.67053 409.423125 33.6512157

1754.38482 409.356458 33.6457363

D 查询员工的入职时间, 入职月数

selectename,

(sysdate-hiredate)/30 估计月,

MONTHS_BETWEEN(sysdate, hiredate) 函数计算月

from emp

select ename,

(sysdate-hiredate)/30 估计月,

MONTHS_BETWEEN(sysdate, hiredate) 函数计算月

from emp;

ENAME 估计月函数计算月

---------- ---------- ----------

SMITH 411.589872 405.699876

ALLEN 409.423205 403.603102

WARD 409.356539 403.538586

JONES 408.056539 402.183747

MARTIN 402.089872 396.345037

selectnext_day(sysdate, '星期六') from dual ;

NEXT_DAY(SYSDA

--------------

11-10月-14

4 数据类型转换

select * from emp where hiredate >'01-1月 -81'

A 日期相关类型转换

比这个日期都要大的08-9月 -81 所有员工信息按照三种方式实现

(1):隐式类型转换

select*

from emp

where hiredate > '01-1月 -81';

(2):字符串转成日期..显示类型转换

select*

from emp

where hiredate > to_date('1981-01-0102:03:04', 'yyyy-mm-dd hh24:mi:ss');

(3):日期转换成为日期类型的字符串:

select*

from emp

where to_char(hiredate, 'yyyy-mm-ddhh24:mi:ss') > '1981-01-01 02:03:04'

5 数字和字符串转换

A --查询员工的薪水:两位小数本地货币代码千位符

Y1,250.00

select empno, ename, to_char(sal, 'L9,999.99')

fromemp

结果:

EMPNO ENAME TO_CHAR(SAL,'L9,999

---------- ---------- -------------------

7369SMITH ¥800.00

7499ALLEN ¥1,600.00

7521WARD ¥1,250.00

7566JONES ¥2,975.00

7654MARTIN ¥1,250.00

7698BLAKE ¥2,850.00

7782CLARK ¥2,450.00

B 把这个字符¥1,250.00,转成数字.....

select to_number('¥1,250.00','L9,999.99') from dual ;

TO_NUMBER('¥1,250.00','L9,999.99')

-----------------------------------

1250

6 通用函数

A 给员工涨工资:总裁 1000 经理:800 其他涨500,

===============================================

分析:前后工资给列出来

if (job == 'PRESIDENT')

SAL+1000

else if (job == 'MANAGER')

SAL+800

else

SAL+500

================================================

使用的知识点:

CASE expr WHEN comparison_expr1 THEN return_expr1

[WHEN comparison_expr2 THENreturn_expr2

WHEN comparison_exprn THEN return_exprn

ELSE else_expr]

END

===============================================

转化:

CASE job when 'PRESIDENT' then sal+1000

when'MANAGER' then sal+800

elsesal + 500

END

=============================================

select ename, job, sal 涨前工资,

(

CASE job when 'PRESIDENT' then sal+1000

when'MANAGER' then sal+800

elsesal + 500

END

)

涨后工资

from emp

-----------------------------------------------------------------------------------------------------

结果:

ENAME JOB 涨前工资 涨后工资

------------------- ---------- ----------

SMITH CLERK 800 1300

ALLEN SALESMAN 1600 2100

WARD SALESMAN 1250 1750

JONES MANAGER 2975 3775

MARTIN SALESMAN 1250 1750

BLAKE MANAGER 2850 3650

CLARK MANAGER 2450 3250

SCOTT ANALYST 3000 3500

KING PRESIDENT 5000 6000

TURNER SALESMAN 1500 2000

ADAMS CLERK 1100 1600

JAMES CLERK 950 1450

FORD ANALYST 3000 3500

MILLER CLERK 1300 1800

已选择14行。

B 第二种方法

===================================================

语法:

DECODE(col|expression, search1, result1

[, search2, result2,...,]

[, default])

==================================================

转化:

decode(job, 'PRESIDENT', sal+10000, 'MANAGER', sal+800, sal+500)

===================================================

select ename, job, sal 涨前工资,

(

decode(job, 'PRESIDENT', sal+10000,'MANAGER', sal+800, sal+500)

)

涨后工资

from emp;

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