Home Database Mysql Tutorial oracle心得2

oracle心得2

Jun 07, 2016 pm 03:38 PM
oracle sql function Multi-line Experience operate number

Sql 有两种函数,单行函数和多行函数 1. 单行函数 单行函数: 操作数据对象、接受参数返回一个结果、只对一行进行变换、每行返回一个结果、可以转换数据类型、可以嵌套、参数可以是一列或一个 DUAL 是一个‘ 伪表’,可以用来测试函数和表达式 2. 字符函数 大

Sql有两种函数,单行函数和多行函数

1.单行函数

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

oracle心得2

DUAL是一个‘伪表’,可以用来测试函数和表达式

2.字符函数

oracle心得2

大小写控制函数:这类函数改变字符的大小写。

oracle心得2

例子:

<pre class="brush:php;toolbar:false">select lower(ename) from emp;

LOWER(ENAM                                                                                          
----------                                                                                          
smith                                                                                               
allen                                                                                               
ward                                                                                

select upper(ename) from emp;

UPPER(ENAM                                                                                          
----------                                                                                          
SMITH                                                                                               
ALLEN                                                                                               
WARD        

select initcap(ename) from emp;

INITCAP(EN                                                                                          
----------                                                                                          
Smith                                                                                               
Allen                                                                                               
Ward     
Copy after login

Copy after login

字符控制函数:

oracle心得2

select concat('hello','word')from dual;

CONCAT('H                                                                                           
---------                                                                                           
helloword  

select substr('helloword',1,3) from dual;

SUB                                                                                                 
---                                                                                                 
hel                                                                           

select length('helloword') from dual;

LENGTH('HELLOWORD')                                                                                 
-------------------                                                                                 
                  9  

select instr('helloworld','w') from dual;

INSTR('HELLOWORLD','W')                                                                             
-----------------------                                                                             
                      6  

select lpad('hello',10,'*')from dual;

LPAD('HELL                                                                                          
----------                                                                                          
*****hello

select rpad('hello',10,'#')from dual;

RPAD('HELL                                                                                          
----------                                                                                          
hello#####  

select trim('  hello  ') from dual;

TRIM(                                                                                               
-----                                                                                               
hello             
Copy after login


 

3.数字函数

ROUND:四舍五入

TRUNC: 截断

MOD:求余

Round 函数 :语法为ROUND(number,num_digits);其中Number是需要进行四舍五入的数字;Num_digits为指定的位数,按此位数进行四舍五入,如果 num_digits 大于 0,则四舍五入到指定的小数位; Num_digits值为多少就到相应的小数点位置四舍五入,如果 num_digits等于 0,则四舍五入到最接近的整数,如果 num_digits 小于 0,则在小数点左侧进行四舍五入;Num_digits值多少就到小数点左侧的整数相应的位置四舍五入。

例如:

ROUND(2.149, 0) 将 2.149 四舍五入到一个整数结果为2。

ROUND(2.15, 1) 将 2.15 四舍五入到一个小数位,结果为2.2。

ROUND(2.149, 1) 将 2.149 四舍五入到一个小数位结果为2.1。

ROUND(-1.475, 2) 将 -1.475 四舍五入到两小数位结果为-1.48)。

ROUND(21.5, -1) 将 21.5 四舍五入到小数点左侧一位结果为20。

例子:


 

SQL> select round(45.926,2) from dual;

ROUND(45.926,2)                                                                                     
---------------                                                                                     
          45.93                                                                                     

SQL> select round(45.926,-2) from dual;

ROUND(45.926,-2)                                                                                    
----------------                                                                                    
               0                                                                                    

SQL> select round(55.926,-2) from dual;

ROUND(55.926,-2)                                                                                    
----------------                                                                                    
             100                                                                                    

SQL> select round(50.926,-2) from dual;

ROUND(50.926,-2)                                                                                    
----------------                                                                                    
             100                                                                                    

SQL> select round(150.926,-2) from dual;

ROUND(150.926,-2)                                                                                   
-----------------                                                                                   
              200                                                                                   

SQL> select round(50.326,-2) from dual;

ROUND(50.326,-2)                                                                                    
----------------                                                                                    
             100                                                                                    

SQL> select round(550.326,-2) from dual;

ROUND(550.326,-2)                                                                                   
-----------------                                                                                   
              600                                                                                   

SQL> select trunc(45.926,2) from dual;

TRUNC(45.926,2)                                                                                     
---------------                                                                                     
          45.92                                                                                     

SQL> select trunc(45.926,-2) from dual;

TRUNC(45.926,-2)                                                                                    
----------------                                                                                    
               0                                                                                    

SQL> select trunc(55.926,-2) from dual;

TRUNC(55.926,-2)                                                                                    
----------------                                                                                    
               0                                                                                    

SQL> select trunc(155.926,-2) from dual;

TRUNC(155.926,-2)                                                                                   
-----------------                                                                                   
              100                                                                                   

SQL> select mod(1600,300) from dual;

MOD(1600,300)                                                                                       
-------------                                                                                       
          100                                                                                       

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

 MOD(13,3)                                                                                          
----------                                                                                          
         1                                                                                          

SQL> select round(45.926,-1) from dual;

ROUND(45.926,-1)                                                                                    
----------------                                                                                    
              50             
Copy after login


 

4.日期

Oracle中的日期型数据实际含有两个值: 日期和时间。

默认的日期格式是 DD-MON-RR.函数SYSDATE 返回:日期、时间

在日期上加上或减去一个数字结果仍为日期。两个日期相减返回日期之间相差的天数。可以用数字除24来向日期中加上或减去小时。

日期函数

oracle心得2

  注:日期转换格式不支持转换中文格式的日期

例子:

SQL> select to_char(sysdate,'yyyy-mm-dd') from dual;

 

TO_CHAR(SY                                                                                                              

----------                                                                                                              

2013-04-04                                                                                                              

 

 

SQL> select to_char(sysdate,'yyyy/mm/dd') from dual;

 

TO_CHAR(SY                                                                                                              

----------                                                                                                              

2013/04/04            

 

select to_char(sysdate,'YEAR-MONTH-DAY') from dual;

 

TO_CHAR(SYSDATE,'YEAR-MONTH-DAY')                                                                                       

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

TWENTY THIRTEEN-4月 -星期四

 

select to_date('1212-12-12','yyyy/mm/dd') from dual;

 

TO_DATE('1212-                                                                                                          

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

12-12月-12

 

SQL> select to_date('1212-12-12','yyyy-mm-dd') from dual;

 

TO_DATE('1212-                                                                                                          

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

12-12月-12                                                                                                              

 

SQL> select to_char(sysdate,'dd month year') from dual;

 

TO_CHAR(SYSDATE,'DDMONTHYEAR')                                                                                          

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

04 4月  twenty thirteen                                                                                                 

 

SQL> select to_char(sysdate,'dd month yyyy') from dual;

 

TO_CHAR(SYSDAT                                                                                                          

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

04 4月  2013        
Copy after login


                                                                    

                                                                  

5.转换函数

oracle心得2

隐式数据类型转换:Oracle自动完成下列转换:

oracle心得2

oracle心得2

TO_CHAR 函数对日期的转换

格式:必须包含在单引号中而且大小写敏感。可以包含任意的有效的日期格式。日期之间用逗号隔开。

oracle心得2

oracle心得2

oracle心得2

例子:

select ename,to_char(sal,'$999,999.00') from emp;

ENAME      TO_CHAR(SAL,                                                                                                 
---------- ------------                                                                                                 
SMITH           $800.00                                                                                                 
ALLEN          $1,600.00                                                                                                 
WARD           $1,250.00     

select ename,to_char(sal,'l999,999.00') from emp;

ENAME      TO_CHAR(SAL,'L999,999                                                                                        
---------- ---------------------                                                                                        
SMITH                   ¥800.00                                                                                        
ALLEN                 ¥1,600.00                                                                                        
WARD                  ¥1,250.00      
Copy after login


 

<span><span><span>6. </span><span>通用函数</span></span></span>
Copy after login

这些函数适用于任何数据类型,同时也适用于空值:

NVL (expr1, expr2):将空值转换成一个已知的值:可以使用的数据类型有日期、字符、数字。

函数的一般形式:

NVL(commission_pct,0)

NVL(hire_date,'01-JAN-97')

NVL(job_id,'No Job Yet')

NVL2 (expr1, expr2, expr3) : expr1不为NULL,返回expr2;为NULL,返回expr3。相当于java中的三目运算符

NULLIF (expr1, expr2) : 相等返回NULL,不等返回expr1

COALESCE (expr1, expr2, ..., exprn):COALESCE 与 NVL 相比的优点在于 COALESCE 可以同时处理交替的多个值。如果第一个表达式为空,则返回下一个表达式,对其他的参数进行COALESCE 。

例子:

SQL> select ename,nvl(comm,0) from emp;

 

ENAME      NVL(COMM,0)                                                                                                  

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

SMITH                0                                                                                                  

ALLEN              300                                                                                                  

WARD               500   

 

错误写法,条件comm与0位置混乱

SQL> select ename,nvl2(comm,0,comm) from emp;

 

ENAME      NVL2(COMM,0,COMM)                                                                                            

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

SMITH                                                                                                                   

ALLEN                      0                                                                                            

WARD                       0                                                                                            

JONES                         

 

正确写法:

SQL> select ename,nvl2(comm,comm,0) from emp;

 

ENAME      NVL2(COMM,COMM,0)                                                                                            

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

SMITH                      0                                                                                            

ALLEN                    300                                                                                            

WARD                     500    

 

 

SQL> select nullif(2,2) from dual;

 

NULLIF(2,2)                                                                                                             

-----------                                                                                                             

                                                                                                                        

 

SQL> select nullif(2,1) from dual;

 

NULLIF(2,1)                                                                                                             

-----------                                                                                                             

          2                                                                                                             

 

SQL> select nullif(1,2) from dual;

 

NULLIF(1,2)                                                                                                             

-----------                                                                                                             

          1        

 

SQL> select ename,

  2  coalesce(sal,comm) from emp;

 

ENAME      COALESCE(SAL,COMM)                                                                                           

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

SMITH                     800                                                                                           

ALLEN                    1600                                                                                           

WARD                     1250           



COALESCE(COMM,SAL)                                                                                                      

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

               800                                                                                                      

               300                                                                                                      

               500                                                                                                      

              2975      

Copy after login

7条件表达式

在 SQL 语句中使用IF-THEN-ELSE 逻辑;使用两种方法:

CASE 表达式

DECODE 函数

例子:

SQL> select ename,job,sal,case

  2  job when 'SALESMAN' then sal*1.2

  3  when 'MANAGER' then sal*1.8

  4  else sal end  "revised_sal" from emp;

 

ENAME      JOB         SAL revised_sal                                                                                  

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

SMITH      CLERK       800         800                                                                                  

ALLEN      SALESMAN   1600        1920                                                                                  

WARD       SALESMAN   1250        1500                                                                                  

JONES      MANAGER    2975        5355  

 

 

SQL> select ename,job,sal,decode(

  2  job,'SALESMAN',1.2*sal,

  3  'MANAGER',1.8*sal,sal)

  4  revised_sal from emp;

 

ENAME      JOB         SAL REVISED_SAL                                                                                  

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

SMITH      CLERK       800         800                                                                                  

ALLEN      SALESMAN   1600        1920                                                                                  

WARD       SALESMAN   1250        1500   
Copy after login


8.嵌套函数

单行函数可以嵌套。嵌套函数的执行顺序是由内到外。

例子:

SQL> select ename,job,nvl2(to_char(job),'manager','NO MANAGER') from emp;

 

ENAME      JOB       NVL2(TO_CH                                                                                         

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

SMITH      CLERK     manager                                                                                            

ALLEN      SALESMAN  manager                                                                                            

WARD       SALESMAN  manager 
Copy after login


 

 

 

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the problem of closing oracle cursor How to solve the problem of closing oracle cursor Apr 11, 2025 pm 10:18 PM

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

How to create cursors in oracle loop How to create cursors in oracle loop Apr 12, 2025 am 06:18 AM

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

How to create oracle dynamic sql How to create oracle dynamic sql Apr 12, 2025 am 06:06 AM

SQL statements can be created and executed based on runtime input by using Oracle's dynamic SQL. The steps include: preparing an empty string variable to store dynamically generated SQL statements. Use the EXECUTE IMMEDIATE or PREPARE statement to compile and execute dynamic SQL statements. Use bind variable to pass user input or other dynamic values ​​to dynamic SQL. Use EXECUTE IMMEDIATE or EXECUTE to execute dynamic SQL statements.

How to paginate oracle database How to paginate oracle database Apr 11, 2025 pm 08:42 PM

Oracle database paging uses ROWNUM pseudo-columns or FETCH statements to implement: ROWNUM pseudo-columns are used to filter results by row numbers and are suitable for complex queries. The FETCH statement is used to get the specified number of first rows and is suitable for simple queries.

What to do if the oracle can't be opened What to do if the oracle can't be opened Apr 11, 2025 pm 10:06 PM

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

How to read the oracle awr report How to read the oracle awr report Apr 11, 2025 pm 09:45 PM

An AWR report is a report that displays database performance and activity snapshots. The interpretation steps include: identifying the date and time of the activity snapshot. View an overview of activities and resource consumption. Analyze session activities to find session types, resource consumption, and waiting events. Find potential performance bottlenecks such as slow SQL statements, resource contention, and I/O issues. View waiting events, identify and resolve them for performance. Analyze latch and memory usage patterns to identify memory issues that are causing performance issues.

How to open a database in oracle How to open a database in oracle Apr 11, 2025 pm 10:51 PM

The steps to open an Oracle database are as follows: Open the Oracle database client and connect to the database server: connect username/password@servername Use the SQLPLUS command to open the database: SQLPLUS

How to use triggers for oracle How to use triggers for oracle Apr 11, 2025 pm 11:57 PM

Triggers in Oracle are stored procedures used to automatically perform operations after a specific event (insert, update, or delete). They are used in a variety of scenarios, including data verification, auditing, and data maintenance. When creating a trigger, you need to specify the trigger name, association table, trigger event, and trigger time. There are two types of triggers: the BEFORE trigger is fired before the operation, and the AFTER trigger is fired after the operation. For example, the BEFORE INSERT trigger ensures that the age column of the inserted row is not negative.

See all articles