ORACLE ÖеÄDECODEº¯Êý
×òÌìÃæÊÔ±»Îʵ½ÁË·Ö¶Îͳ¼Æ,ÎÒÖªµÀdecode¿ÉÒÔ,ÃæÊÔ¹Ù½Ó×ÅÒªÎÒ˵decodeµÄÓï·¨,û»Ø´ðÉÏÀ´.´ÓÍøÉÏÕÒµÄ×ÊÁÏÉÔ΢¸ÄÁËÒ»ÏÂ.
DECODEº¯ÊýÊÇORACLE PL/SQLÊǹ¦ÄÜÇ¿´óµÄº¯ÊýÖ®Ò»£¬Ä¿Ç°»¹Ö»ÓÐORACLE¹«Ë¾µÄSQLÌṩÁ˴˺¯Êý£¬ÆäËûÊý¾Ý¿â³§É̵ÄSQLʵÏÖ»¹Ã»Óд˹¦ÄÜ¡£
DECODEÓÐʲôÓÃ;ÄØ£¿ Ïȹ¹ÔìÒ»¸öÀý×Ó£¬¼ÙÉèÎÒÃÇÏë¸øÖ°Ô±¼Ó¹¤×Ê£¬Æä±ê×¼ÊÇ£º¹¤×ÊÔÚ8000ÔªÒÔϵĽ«¼Ó20£¥£»¹¤×ÊÔÚ8000ÔªÒÔÉϵļÓ15£¥£¬8000ÔªµÄ²»±ä.Èç¹ûÏëÏÈÔ¤ÀÀһϼӹ¤×ʺóµÄ½á¹û¿ÉÒÔÓÃdecodeº¯Êý.ÈçÏÂ:
select decode(sign(salary - 8000),1,salary*1.15,-1,salary*1.2,salary) from employee
DECODEµÄÓï·¨£ºDECODE(value,if1,then1,if2,then2,if3,then3,...,else)£¬±íʾÈç¹ûvalueµÈÓÚif1ʱ£¬DECODEº¯ÊýµÄ½á¹û·µ»Øthen1,...,Èç¹û²»µÈÓÚÈκÎÒ»¸öifÖµ£¬Ôò·µ»Øelse¡£³õ¿´Ò»Ï£¬DECODE Ö»ÄÜ×öµÈÓÚ²âÊÔ£¬µ«¸Õ²ÅÒ²¿´µ½ÁË£¬ÎÒÃÇͨ¹ýһЩº¯Êý»ò¼ÆËãÌæ´úvalue£¬ÊÇ¿ÉÒÔʹDECODEº¯Êý¾ß±¸´óÓÚ¡¢Ð¡ÓÚ»òµÈÓÚ¹¦ÄÜ¡£
decode()º¯ÊýʹÓü¼ÇÉ
·Èí¼þ»·¾³:
1¡¢Windows NT4.0+ORACLE 8.0.4
·º¬Òå½âÊÍ:
decode(Ìõ¼þ,Öµ1,·ÒëÖµ1,Öµ2,·ÒëÖµ2,...Öµn,·ÒëÖµn,ȱʡֵ)
¸Ãº¯ÊýµÄº¬ÒåÈçÏÂ:
IF Ìõ¼þ=Öµ1 THEN
RETURN(·ÒëÖµ1)
ELSIF Ìõ¼þ=Öµ2 THEN
RETURN(·ÒëÖµ2)
......
ELSIF Ìõ¼þ=Öµn THEN
RETURN(·ÒëÖµn)
ELSE
RETURN(ȱʡֵ)
END IF
· ʹÓ÷½·¨:
1¡¢±È½Ï´óС
select decode(sign(±äÁ¿1-±äÁ¿2),-1,±äÁ¿1,±äÁ¿2) from dual; --È¡½ÏСֵ
sign()º¯Êý¸ù¾Ýij¸öÖµÊÇ0¡¢ÕýÊý»¹ÊǸºÊý£¬·Ö±ð·µ»Ø0¡¢1¡¢-1
ÀýÈç:
±äÁ¿1=10£¬±äÁ¿2=20
Ôòsign(±äÁ¿1-±äÁ¿2)·µ»Ø-1£¬decode½âÂë½á¹ûΪ“±äÁ¿1”£¬´ïµ½ÁËÈ¡½ÏСֵµÄÄ¿µÄ¡£
2¡¢±í¡¢ÊÓͼ½á¹¹×ª»¯
ÏÖÓÐÒ»¸öÉÌÆ·ÏúÊÛ±ísale£¬±í½á¹¹Îª:
month char(6) --Ô·Ý
sell number(10,2) --ÔÂÏúÊÛ½ð¶î
ÏÖÓÐÊý¾ÝΪ:
200001 1000
200002 1100
200003 1200
200004 1300
&
Ïà¹ØÎĵµ£º
oracle±í¿Õ¼ä²Ù×÷Ïê½â
1
2
3×÷Õߣº À´Ô´£º ¸üÐÂÈÕÆÚ£º2006-01-04
5
6
7½¨Á¢±í¿Õ¼ä
8
9CREATE TABLESPACE data01
10DATAFILE '/ora ......
1.±¸·Ýscott
£¨1£©ÔÚCÅ̵ĸùĿ¼Ï´´½¨Ò»¸öÎļþ¼Ðtemp
£¨2£©ÔÚcmdÔÚ½øÈëÎļþ¼Ð£¬del *.* £¬É¾³ýËùÓÐÎļþ¡£
£¨3£©ÊäÈëexp
£¨4£©ÊäÈëÓû§ÃûºÍÃÜÂë:scott/tiger
£¨5£©¾Í»áÏÔʾ“ÊäÈëÊý×éÌáÈ¡»º³åÇø´óС”£¬ÔÚÕâÒ»²½Ö±½Ó»Ø³µ¾ÍÐÐÁË
£¨6£©ÏÔʾ“µ¼³öÎļþ£ºDXPDAT.DMP>”£¬ÕâÒ»²½Ò²Êǻسµ
£¨ ......
http://cheneyph.javaeye.com/blog/453492
ÓÃoracle sql¶ÔÊý×Ö½øÐвÙ×÷: È¡ÉÏÈ¡Õû¡¢ÏòÏÂÈ¡Õû¡¢±£ÁôNλСÊý¡¢ËÄÉáÎåÈë¡¢Êý×Ö¸ñʽ»¯
È¡Õû£¨ÏòÏÂÈ¡Õû£©£º
select floor(5.534) from dual;
select trunc(5.534) from dual;
ÉÏÃæÁ½ÖÖÓ÷¨¶¼¿ÉÒÔ¶ÔÊý×Ö5.534ÏòÏÂÈ¡Õû£¬½á¹ûΪ5.
Èç¹ûÒªÏòÉÏÈ¡Õû £¬µÃµ½½á¹ûÎ ......
//¼ÆËãºÁÃë²î(Á½¸ödateÀàÐ͵ÄÏà¼õΪÌìÊý²î±ð£¬È»ºóת»»ÎªºÁÃë)
select ceil(to_date('209-11-17 13:00:12','yyyy-mm-dd hh24:mi-ss')-to_date(2009-11-18 14:00:12','yyyy-mm-dd hh24:mi-ss') )from dual;
//¼ÆËãÏà²îÔ·Ý
select (EXTRACT(year from to_date('209-11-17','yyyy-mm-dd'))-EXTRACT(year from  ......
ÎÊ£ºÊ²Ã´ÊÇNULL£¿
´ð£ºÔÚÎÒÃDz»ÖªµÀ¾ßÌåÓÐʲôÊý¾ÝµÄʱºò£¬Ò²¼´Î´Öª£¬¿ÉÒÔÓÃNULL£¬ÎÒÃdzÆËüΪ¿Õ£¬ORACLEÖУ¬º¬ÓпÕÖµµÄ±íÁ㤶ÈΪÁã¡£
ORACLEÔÊÐíÈκÎÒ»ÖÖÊý¾ÝÀàÐ͵Ä×Ö¶ÎΪ¿Õ£¬³ýÁËÒÔÏÂÁ½ÖÖÇé¿ö£º
1¡¢Ö÷¼ü×ֶΣ¨primary key£©£¬
2¡¢¶¨ÒåʱÒѾ¼ÓÁËNOT NULLÏÞÖÆÌõ¼þµÄ×Ö¶Î
˵Ã÷£º
1¡¢µÈ¼ÛÓÚûÓÐÈκÎÖµ¡¢ÊÇδ֪Êý¡£
2 ......