`

Single-Row Functions

阅读更多
http://superman-chenzs.itpub.net/post/29327/274808

- figer: function_name [(arg1,arg2,...)]
- Manipulate data items
- Accept arguments and return one value
- Act on each row returned
- Return one result per row
- May modify the data type
- Can be nested
- Accept arguments which can be a column or an expression
-----------------------------------------------------------------------------------------------------------------
Single-Row Functions' Type:
1.Character
2.Number
3.Date
4.Conversion
5.General


-----------------------------------------------------------------------------------------------------------------
Character Functions:
1.Case-manipulation functions
- LOWER : get the sql course's lower shap letters
- UPPER : get the sql course's upper shap letters
- INITCAP : get the sql course's first letter's upper shap and the others letter is lower,init caps Lock
eg:
FUNCTION RESULT
------------------------------
LOWER('CHENZS') chenzs
UPPER('chenzs') CHENZS
INITCAP('CHENZS') Chenzs
INITCAP('chenzs') Chenzs
INITCAP('cHENZS') Chenzs
INITCAP('Chenzs') Chenzs

2.Character-manipulation functions
- CONCAT : CONCAT('string1','string2'),连接了个字符串,相当于符合||
- SUBSTR : SUBSTR('string',A,B)得到某一个字符串的子字符串,A为正表示从string的左边第A个字符从左往右取B个字符;A为负数,表示从string的右边第|A|个字母从左往右取B个字符;B必须为正,若为负,返回空白。注意是字符非字节
- LENGTH : LENGTH('string'),get the length of the string
- INSTR : INSTR('string','c'),get the first char 'c''s location in the string
- LPAD|RPAD : LPAD('string',len,'C'),RPAD('string',len,'C'),在左/右边填充相应的字符(串)'C',使字符串string达到相应的长度len
- TRIM:
- REPLACE
eg:

FUNCTION RESULT
------------------------------------------
CONCAT('Hello','World') HelloWorld
SUBSTR('HelloWorld',1,5) Hello
SUBSTR('HelloWorld',-5,3) Wor
SUBSTR('HelloWorld',-3,-2)
SUBSTR('HelloWorld',3,-2)
SUBSTR('超人,我爱你',2,3) 人,我
LENGTH('HELLOWORLD') 10
INSTR('HELLOWORLD','W') 6
INSTR('CHENZSCHENZS,'C') 1
LPAD(salary,10,'*') *****24000
RPAD(salary,10,'*') 24000*****
LPAD(salary,10,'*!') *!*!*24000
RPAD(salary,10,'*!') 24000*!*!*
TRIM('H' FROM 'HelloWorld') elloWorld
TRIM('e' FROM 'HelloWorld') HelloWorld
TRIM('e' FROM 'eHelloWorld') HelloWorld
TRIM('e' FROM 'HelloWorlde') HelloWorld
TRIM('e' FROM 'eHelloWorlde') HelloWorld


-----------------------------------------------------------------------------------------------------------------
Number Functions:
ROUND : Rounds value to specified decimal,ROUND(numA,numB)
TRUNC : Truncate value to specified decimal,TRUNC(numA,numB)
MOD : Returns remainder of division,MOD(numA,numB)
eg:
FUNCTION RESULT
------------------------------
ROUND(45.926,2) 45.93
ROUND(45.921,2) 45.92
ROUND(45.926,-1) 50
ROUND(45.921,-2) 0
TRUNC(45.926,2) 45.92
TRUNC(45.921,2) 45.92
TRUNC(45.926,-1) 10
TRUNC(45.921,-2) 0
MOD(1600,300) 100
MOD(10.54,0.5) 0.04


-----------------------------------------------------------------------------------------------------------------
Date Functions:
- Oracle database stors dates in an internal numeric format:century,year,month,day,hours,minutes,seconds
- The default date display format is DD-MON-yy
- SYSDATE is a function that returns:Date,Time

Arithmetic with Dates:
- Add or subtract a number to or from a date for a resultant date value
- Subtract two dates to find the number of days between those dates
- Add hours to a date by dividing the number of hours by 24

Function Description
----------------------------------------------------------
MONTHS_BETWEEN Number of months between two dates
ADD_MONTHS Add calendar months to date
NEXT_DAY Next day of the date specified
LAST_DAY Last day of the month
ROUND Round date
TRUNC Truncate date

eg:
MOUTHS_BETWEEN('01-SEP-95','11-JAN-94') -> 19.6774194
ADD_MONTHS('11-JAN-94',6) -> '11-JUL-94'
NEXT_DAY('01-SEP-95','FRIDAY') -> '08-SEP-95'
LAST_DAY('01-FEB-95') -> '28-FEB-95'
Assume SYSDATE = '25-JUL-95':
ROUND(SYSDATE,'MONTH') -> 01-AUG-95
ROUND(SYSDATE,'YEAR') -> 01-JAN-96
TRUNC(SYSDATE,'MONTH') -> 01-JUL-95
TRUNC(SYSDATE,'YEAR') -> 01-JAN-95


-----------------------------------------------------------------------------------------------------------------
Conversion Funtions:
- Data type conversion including: Implicit data type conversion & Explicit data type conversion

Implicit Data Type Conversion:
--------------------------------
FROM TO
--------------------------------
VARCHAR2 OR CHAR NUMBER
VARCHAR2 OR CHAR DATE
NUMBER VRACHAR2
DATE VARCHAR2

Explicit Data Type Conversion:
NUMBER ---TO_CHAR()---> CHARACTER <--TO_CHAR()--- DATE
NUMBER <--TO_NUMBER()-- CHARACTER ---TO_DATE()--> DATE

1.Using the TO_CHAR Function with Dates:
- TO_CHAR(date,'format_model')
- The format model:
* Must be enclosed in single quotation marks and is case sensitive
* Can include any valid date format element
* Has an fm element to remove padded blanks or suppress leading zeros
* Is separated from the date value by a comma
- Elements of the Date Format Model
* YYYY : Full year in numbers
* YEAR : Year spelled out
* YY : Two-digit value for year in the current century.eg:(this year is 2006),so 19-->2019,56-->2056
* RR : Two-digit value for year in the current century.eg:(this year is 2006),so 19-->2019,56-->1956(devived on the 50 year of the century)
* MM : Two-digit value for month
* MONTH: Full name of the month
* MON : Three-letter abbreviation of the month
* DY : Three-letter abbreviation of the day of the week
* DAY : Full name of the day of the week
* DD : Numeric day of the month
* Oracle stores time in 24-hour format--HH:MI:SS
* By default, the time in a date field is 00:00:00 A.M. (midnight) if no time portion is entered.
eg:
Time elements format the time portion of the date:
HH24:MI:SS AM --> 15:45:32 PM
* Add character strings by enclosing them in double quotation marks in the formate
eg:
DD "of" MONTH --> 12 of OCTOBER
Number suffixes spell out numbers
eg:
ddapth --> fourteenth
* the RR year format:
-----------------------------------------------------------------------------------|
| If the specified two-digit year is |
|---------------------|------------------------------|
| 0--49 | 50--99 |
----------------------------------------------------|------------------------------|
| |The return date is in|The return date in the century|
the year of the | RR |the current century |before the current one |
|-------|---------------------|------------------------------|
date's format | |The return date is in|The return date is in the |
| YY |the current century |current century |
------------------------------------------------------------------------------------

2.Using the TO_CHAR Function with Numbers:
- TO_CHAR(number,'format_model')
- the format model:
* 9 : Represents a number
* 0 : Forces a zero to be displayed
* $ : Places a floating dollar sign
* L : Uses the floating local currency symbol
* . : Prints a decimal point
* , : Prints a thousand indicator

3.Using the TO_NUMBER & TO_DATE Functions:
- Convert a character string to a number format using the TO_NUMBER function:TO_NUMBER(char1[,'format_model']);
- Convert a character string to a date format using the TO_DATE function:TO_DATE[,'format_model']);
This functions have an fx modifier. This modifier specifies the exact matching for the character argument and date format model of a TO_DATE function.


-----------------------------------------------------------------------------------------------------------------
Nesting Functions:
- Single-row functions can be nested to any level
- Nested functions are evaluated from deepest level to the least deep level


-----------------------------------------------------------------------------------------------------------------
General Functions:
- these functions work with any data type and pertain to using nulls
1.NVL(expr1,expr2)
- if expr1 is null show expr2,else show expr1
- Data types that can be used are date,character and number.
- Data types must match.
2.NVL2(expr1,expr2,expr3) :
- if expr1 is null show expr2,else show expr3
3.NULLIF(expr1,expr2) :
- if expr1 equals expr2 show null,else show expr1
4.COALESCE(expr1,expr2,...,exprn) :
- return the first null value between exprns.
- If the first expression is not null,it returns that expression;otherwise,it does a COALESCE of the remaining expressions.
- The advantage of the COALESCE function over the NVL function is that the COALESCE function can take multiple alternate values.


-----------------------------------------------------------------------------------------------------------------
Conditional Expressions:
- Provide the use of IF=THEN-ELSE logic within a SQL statement.
- Use two methods:
- CASE expression
- DECODE function

1.The CASE Expression
Facilitates conditional inquiries by doing the work of an IF-THEN-ELSE statement.
Syntax:
--------------------------------------------------
CASE expr WHEN comparison_expr1 THEN return_expr1
[WHEN comparison_expr2 THEN return_expr2
WHEN comparison_exprn THEN return_exprn
ELSE else_expr]
END
--------------------------------------------------

2.The DECODE Function
Facilitates conditional inquiries by doing the work of an IF-THEN-ELSE statement:
Syntax:
-------------------------------------------
DECODE (col|expression,search1,result1
[,search2,result2,...]
[,default value])
分享到:
评论

相关推荐

    oracle函数介绍(1) 著名函数之单值函数.doc

    第一篇 著名函数之单值函数 (一).数值型函数(Number Functions) (二).字符型函数返回字符值(Character Functions Returning Character Values) ...其它辅助函数(Miscellaneous Single-Row Functions)

    oracle函数介绍(2) 非著名函数之单值函数.doc

    第二篇 非著名函数之单值函数 (一).数值型函数(Number Functions) (二).字符型函数返回字符值(Character Functions Returning Character Values) ...其它辅助函数(Miscellaneous Single-Row Functions)

    oracle ocp007 笔记

    3. **Single-Row Functions** - 包括字符、数字、日期转换等五大类函数。 - 字符函数如`LOWER`, `UPPER`, `INITCAP`分别将文本转换为小写、大写或首字母大写。 - 其他字符函数包括`CONCAT`(连接字符串),`...

    oracle操作文档

    4. 单行函数(Single-Row Functions): Oracle提供了一系列内置的单行函数,用于处理单个值。这些函数包括字符串函数(如SUBSTR、INSTR)、日期函数(如SYSDATE、ADD_MONTHS)、数学函数(如ROUND、MOD)和转换函数...

    MLDN魔乐科技JAVA培训_Oracle课堂6_排序、单行函数.rar

    标题中的“MLDN魔乐科技JAVA培训_Oracle课堂6_排序、单行函数.rar”表明这是一个关于Java编程和Oracle数据库的教程,特别是涉及到排序(Sorting)和单行函数(Single-row functions)的主题。这个压缩包可能包含了一...

    Oracle 10g培训经典_英文版

    2. **Les03Single-Row Functions.ppt** - 单行函数是处理单个数据行的函数,例如转换数据类型、提取字符串子串、计算数学运算等。常见的有TO_CHAR、SUBSTR、UPPER/LOWER等。 3. **Les04Displaying Data from ...

    oracle官方ppt

    3. **03 Single-Row Functions.ppt**:这部分介绍了单行函数,如CONVERT、UPPER/LOWER、TRIM等,这些函数用于处理单行数据,进行类型转换、大小写转换和字符串操作。 4. **04 Displaying Data from Multiple Tables...

    Absolute Database for D7

    64,000 (also limited by page size) Bytes per row 65,400 (also limited by page size) 65,400 (also limited by page size) Columns per index 10,000 10,000 Columns per table 65,000 65,000 UNIQUE indexes or...

    EhLib 8.0 Build 8.0.023 Pro Edition FullSource for D7-XE8

    Allows automatic broken of a single line long title and data row to a multiline. Title can act as button and, optionally show a sort marker. Automatically sortmarking. Ability to truncate long ...

    EhLib 6.3 Build 6.3.176 Russian version. Full source included.

    Allows automatic broken of a single line long title and data row to a multiline. Title can act as button and, optionally show a sort marker. Automatically sortmarking. Ability to truncate long ...

    The Art of Assembly Language Programming

    Arrays 5.6.1.1 - Declaring Arrays in Your Data Segment 5.6.1.2 - Accessing Elements of a Single Dimension Array 5.6.2 - Multidimensional Arrays 5.6.2.1 - Row Major Ordering 5.6.2.2 - ...

    R for beginner

    Functions like `par(mfrow=c(2,2))` can be used to specify the layout of multiple plots in a single device. ##### Graphical Functions R includes numerous built-in functions for generating graphs, ...

    oracle sql教程

    - **Lesson 3 - Single Row Functions**:了解单行函数的使用,这些函数通常用于处理单个值。 - **Lesson 4 - Displaying Data from Multiple Tables**:学习如何使用JOIN语句从多个表中检索数据。 #### 第二天 - ...

    EhLib5.0.13 最新的ehlib源码

    Allows automatic broken of a single line long title and data row to a multiline. Title can act as button and, optionally show a sort marker. Automatically sortmarking. Ability to truncate long ...

    Advanced Engineering Mathematics

    of a Single Real Variable 636 11.3 Directional Derivatives and the Gradient Operator 644 11.4 Conservative Fields and Potential Functions 650 11.5 Divergence and Curl of a Vector 659 11.6 Orthogonal ...

    ehlib_vcl_src_9_3.26

    Allows automatic broken of a single line long title and data row to a multiline. Title can act as button and, optionally show a sort marker. Automatically sortmarking. Ability to truncate long ...

    EhLib 9.1.024

    Allows automatic broken of a single line long title and data row to a multiline. Title can act as button and, optionally show a sort marker. Automatically sortmarking. Ability to truncate long ...

    Killtest1Z0-051.pdf

    4. **单行函数(Single Row Functions)** - 单行函数在Oracle中用于处理一行数据的每一列,返回单个值。选项D(They always return a single result row for every row of a queried table)和E(They can return ...

    Foundations for Analytics with Python O-Reilly-2016-Clinton W. Brownley

    It also introduces another new technique: keeping track of the previous row you processed and the row you’re currently processing, in order to calculate a statistic based on values in the two rows....

    数据库mysql8版本免费分享

    2. **窗口函数(Window Functions)**: MySQL 8.0引入了窗口函数,这是一类可以在结果集的行之间进行计算的功能,例如RANK()、ROW_NUMBER()、LEAD()和LAG()等,极大地增强了SQL查询的能力,特别是对数据分组和排序...

Global site tag (gtag.js) - Google Analytics