Oracle study notes 007-1 (one-way function: character functions, numerical functions, conversion functions, date functions)

Function Introduction
  • Oracle SQL provides specific functions for performing a particular operation, these functions greatly enhance the function of the SQL language, the use of most programming languages, the process returns to the received parameter data items result, the function can accept zero or more input parameters, and returns an output.
  • In Oracle function it is mainly divided into two categories (This paper describes a single line in which four common function)
  1. One-way function: a function for every record in the table in the application, a single line of a result, returns a result.
  2. Aggregation function: function while the polymerization may operate on multiple rows of data, and returns a result. Such as the SUM (x)
    returns the result set sum column x.
Conventional one-way function
  • Character functions: for string manipulation.

  • Digital functions: a digital calculation, a return number.

  • Transfer function: can convert one data type to another data type.

  • Date Functions: Date and time of processing.

  • Null correlation function: NULL-related functions of

  • Decoding and encoding related functions

  • Nested functions: a function as a parameter of another ambiguous return value

Commonly used characters function
function Explanation Show result
ASCII(parameter) Returns the ASCII code of the character parameter SELECT ASCII(‘X’) FROM DUAL; 88
CONCAT(parameter1,parameter2) Connection string parameter1 and parameter2 SELECT CONCAT(‘HELLO’,‘WORLD’) FROM DUAL; HELLOWORLD
LENGTH(parameter) Returns the length of the parameter SELECT LENGTH ( 'Oracle Study Notes') FROM DUAL; 10
LOWER(parameter) parameter converted to lowercase SELECT LOWER(‘JavaSE’) FROM DUAL; javase
UPPER(parameter) parameter to uppercase SELECT UPPER(‘JavaSE’) FROM DUAL; JAVASE
LTRIM(parameter[,X]) X amputated the left of the parameter of the string, the default amputated spaces SELECT LTRIM(’###Hello–World###’,’###’) FROM DUAL; JAVASE
RTRIM(parameter[,X]) The right of the parameter string of truncated X, the default amputated spaces: SELECT RTRIM(’###Hello–World###’,’###’) FROM DUAL; JAVASE
TRIM([X FROM]parameter) The truncated sides parameter string X, truncated default space SELECT TRIM(’#‘FROM’#Hello–World#’) FROM DUAL; Hello–World
REPLACE(parameter,old,new) Find old in the parameter, and replaced with new SELECT REPLACE(‘Hello–World’,‘World’,‘nihao’) FROM DUAL; Hello–nihao
SUBSTR(parameter,start[,length]) Back substring parameter, starting from the start, the interception of length characters, the default length, to the end of the default SELECT SUBSTR(‘Hello–World’,8,5) FROM DUAL; World
Common numerical function
function Explanation Show result
ABS(X) The absolute value of X SELECT ABS(-9) FROM DUAL; 9
ACOS(X) X inverse cosine SELECT ACOS(1) FROM DUAL; 0
COS(X) Cosine SELECT COS(1) FROM DUAL; 0.5403023058
CEIL(X) Greater than or equal to the minimum X SELECT CEIL(9.9) FROM DUAL 10
FLOOR(X) Less than or equal to the maximum value of X SELECT FLOOR(9.9) FROM DUAL; 9
LOG(X,Y) X-Y is the number of bottom SELECT LOG(100,10) FROM DUAL; 0.5
V (X, Y) X divided by Y remainder SELECT MOD(10,3) FROM DUAL; 1
POWER(X,Y) X的Y次幂 SELECT POWER(2,5) FROM DUAL; 32
ROUND(X[,Y]) X在第Y位四舍五入 SELECT ROUND(3.14155926,2) FROM DUAL; 3.14
SQRT(X) X的平方根 SELECT SQRT(9) FROM DUAL; 3
TRUNC(X[,Y]) X在第Y位截断 SELECT TRUNC(3.1415926,3) FROM DUAL; 3.141
常用转换函数
--自动类型转换
SELECT 1+'99' FROM DUAL;

--将字符串转化为时间日期格式
SELECT add_months('15-3月-2020',2) FROM DUAL;

--TO_NUMBER   TO_CHAR   TO_DATE
SELECT SYSDATE FROM DUAL;

--把日期函数转换成字符串:TO_CHAR()
SELECT SYSDATE,TO_CHAR(SYSDATE) FROM DUAL;

--将数字转化为字符串
SELECT 1433223,TO_CHAR(1433223) FROM DUAL;

-- 将字符串转换成日期类型,必须要指定格式TO_DATE
SELECT MONTHS_BETWEEN(SYSDATE,TO_DATE('2019-03-15','YYYY-MM-DD')) FROM DUAL;

--将字符串转化为数字:字符串中必须只有数字
SELECT '123' ,TO_NUMBER('3.1415926') FROM DULA;

日期函数
  • ADD_MONTHS(d,n),在某一个日期 d 上,加上指定的月数 n,返回计算后的新日期,d 表示日期,n 表示要加的月数

  • LAST_DAY(d),返回指定日期当月的最后一天

  • ROUND(d[,format]),返回一个以 format 为格式的四舍五入日期值, d 是日期, format 是格式

    ① 如果 format 为“YEAR”则舍入到某年的 1 月 1 日,即前半年舍去,后半年作为下一年

    ② 如果 format 为“MONTH”则舍入到某月的 1 日,即前月舍去,后半月作为下一月

    ③ 如果 format 为“DAY”则舍入到最近的周的周日,即上半周舍去,下半周作为下一周周日

多种日期格式
  1. YYYY:四位表示的年份
  2. YYY,YY,Y:年份的最后三位、两位或一位,缺省为当前世纪
  3. IYYY:ISO标准的四位年份
  4. MM:01~12的月份编号
  5. MONTH:全拼字符集表示的月份,右边用空格填补
  6. D:当周第几天
  7. DD:当月第几天
  8. DDD:当年第几天
  9. HH,HH12:一天中的第几个小时,12进制表示法
  10. HH24:一天中的第几个小时,取值为00~23
  11. MI:一小时中的分钟
  12. SS:一分钟中的秒
常用日期查询
SELECT TRUNC(SYSDATE,'YEAR') FROM DUAL;

SELECT TRUNC(SYSDATE,'MONTH') FROM DUAL;

SELECT TRUNC(SYSDATE,'DAY') FROM DUAL;

--查询当天日期
SELECT TRUNC(SYSDATE) FROM DUAL;

--查询当月的第一天
SELECT TRUNC(SYSDATE, 'MM') FROM DUAL;

--查询当年第一天
SELECT TRUNC(SYSDATE,'YY') FROM DUAL;

--返回当前时间
SELECT TRUNC(SYSDATE, 'HH') FROM DUAL;


边学习边记录,若有不足之处欢迎留言指点…

发布了63 篇原创文章 · 获赞 1 · 访问量 2022

Guess you like

Origin blog.csdn.net/qq_45061361/article/details/104875399