当前位置:数据库 > Oracle >>

oracle知识点小节2(字符串中包含单引号)

oracle知识点小节2(字符串中包含单引号)
 
1.关于在字符串中包含单引号
字符串是引用字符串的,如果字符串本身就包含单引号怎么办?
用2个单引号表示或者以q或Q开头,然后后面是字符串,字符串的第1个字符和最后结束的字符是分割符号,中间的引号随便写,如果开始是[,<,(,{结束一定要是],>,),}。但是开始如果是],>,),}那么结束要和开始相同,其他的符号,+,|,或字母a,A,等,开始和结束相同,包括大小写。
例子如下:
SQL> select 'you are'' beautiful' from dual;

'YOUARE''BEAUTIFUL
------------------
you are' beautiful

select q'[you are' beautiful]' from dual;


Q'[YOUARE'BEAUTIFU
----------------------
you are' beautiful

SQL> select q'+it's a cat+' from dual
  2  /

Q'+IT'SACAT+'
-------------
it's a cat


SQL> select Q'Ait's a cata' from dual
  2  /

select Q'Ait's a cata' from dual

 

 
ORA-01756: 引号内的字符串没有正确结束
 
2.where子句中不能使用别名
    在where字句中是不能使用别名的,但在order by 字句中是可以使用的,如下
SQL> select employee_id,last_name,job_id,department_id
  2  from employees
  3  where department_id=90;


EMPLOYEE_ID     LAST_NAME    JOB_ID     DEPARTMENT_ID
----------------------      ------------------    ----------            ------------
        100                 King                   AD_PRES                 90
        101                Kochhar              AD_VP                      90
        102                De Haan              AD_VP                    90


SQL> select employee_id,last_name,job_id,department_id dd
  2  from employees where  dd=90;
from employees where  dd=90
                      *
ERROR at line 2:
ORA-00904: "DD": invalid identifier


order by 自己可以用别名
SQL> select employee_id,last_name,salary*12 annsal from employees
  2  order by annsal;


EMPLOYEE_ID LAST_NAME           ANNSAL
--------------      ----------------------              ----------
        132            Olson                             25200
        136            Philtanker                     26400
        128             Markle                         26400
        127             Landry                         28800
        135             Gee                             28800
        191             Perkins                       30000
        119             Colmenares               30000
        140             Patel                           30000
        144             Vargas                        30000
        182             Sullivan                       30000
        131             Marlow                        30000

 

 
为什么呢?因为select语句有个执行顺序,where子句是在select之前进行处理的,而order by字句是在select之后处理。列的别名是在select时生成,所以where字句根本看不到别名,故无法引用,order by 字句则可以。
 
3.关于NULL值
    NULL is a value that is unavaiable,unassigned,unknown or inapplicable.在算术表达式中有NULL值,则结果为NULL值;在字符串的表达式中,则保持原有的字符串。
NULL值在排序中:在升序中NULL值放在最后,在降序中放在开始,即放在最大的位置,可以改变位置,在语句末尾加上nulls first或者nulls last。
select语句默认为升序排列,以department_id排序NULL在最后位置
SQL> select  last_name, department_id from employees order by department_id;
LAST_NAME                 DEPARTMENT_ID
------------------                     -----------------
Greenberg                            100
Sciarra                                  100
Urman                                   100
Popp                                     100
Faviet                                    100
Gietz                                      110
Higgins                                  110
Grant

 

 
以降序排列,NULL在第一个位置
SQL> select  last_name, department_id from employees order by department_id desc;

LAST_NAME                 DEPARTMENT_ID
-------------------------               -------------
Grant
Higgins                                  110
Gietz                                    110
Urman                                  100
Faviet                                  100
Chen                                   100
Popp                                    100
Greenberg                           100
Sciarra                                 100
De Haan                               90
Kochhar                               90

 

 
在末尾加上nulls last改变了NULL值的位置
SQL> select  last_name, department_id from employees order by department_id desc  nulls last;
LAST_NAME                 DEPARTMENT_ID
------------------------- -------------
Himuro                               30
Tobias                               30
Raphaely                             30
Baida                                30
Fay                                  20
Hartstein                            20
Whalen                               10
Grant

 


Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,