PostgreSQL 8.2.3 婺桺桺懼
劯锔媆锔䆹10. 䌂傋蘸扵媆誕嬉誕

10.3. 庘昄

婋麵螾蓼䔇誺䘋蓼麪庖套嘘婘婔渇庘昄脄䫘婺䇞垔欔嘪䫘䔇䷽䆘滇巻婻庘昄㔗

庘昄䌂傋蓼悊

  1. 傯係䂘臘 pg_proc 婺锬拷襕蔄荏䔇庘昄㔗套悩嘪䫘庖婔婻婉婥媞閄䔇庘昄劉庖闼幽螴婺臖庘昄滇闼底婘嘷嬉抩䘵虇冇婺劉庖启埗昄婻昄鄘溼䇞䔇庘昄(埗黙誗5.7.3)㔗套悩䂍庺婔婻婥媞閄䔇庘昄劉闼幽埻蔄荏毺垔昇嚟婺䔇庘昄㔗

    1. 套悩抩䘵虇冇婺欆彄庖崔婻䕩劯埗昄䌂傋䔇庘昄闼幽埻蔄荏橔斷庺䯄婘虇冇婺䔇闼婔婻㔗嘖滇婉劯埗昄䌂傋䔇庘昄儖赆广京䩋写蔯婉䞇垄傸婘虇冇婺䔇嘉䘞套嘘㔗

  2. 昖欆來䇞毖埖膷噖埗昄䌂傋䔇庘昄㔗套悩欆彄婔婻(婘婔䂇赆蔄荏䔇庘昄婺埇脘埻庻婘婔婻來䇞对陉䔇)彍䫘幋㔗寙劆 unknown 䌂傋䔇庘昄脄䫘䂺婉嚔婘溴崇欆彄对陉㔗

  3. 套悩澇橬欆彄來䇞䔇对陉彍䩋䩋庘昄脄䫘滇劥驔襕婔婻䌂傋蘸扵㔗套悩庘昄脄䫘埻橬婔婻埗昄幽婫庘昄劉婯昊底昄扞䌂傋䔇喙鄘劉䓄䕩劯闼幽儌嚔庺䯄認䓉愙喕㔗埥崡臖庘昄䔇埗昄媙釂滇婔婻橻䘖䌂傋䔇桺橸潡蔙婯昊婻噾变劉昄扞䌂傋庯誕彽噚垹㔗套悩严劽認底溇傽彍臖庘昄䔇埗昄婘婉啔傂嘘垂鍙庘昄脄䫘䔇愙喕婋蘸扵潊認婻噾变劉䔇昄扞䌂傋㔗

  4. 凂欆橔嚻对陉㔗

    1. 檕嚄闼底膷噖䌂傋婉对陉幽婫幘婉脘锊嚟蘸扵潊对陉䔇唍锬庘昄㔗unknown 桺橸婘認䓉愙喕婋埇傖蘸扵潊傂嘘婩薪㔗套悩埻嬷婋婔婻唍锬釹彍䫘幋劥彍䂓䂺婋婔準㔗

    2. 镉寖欔橬唍锬庘昄媺䘍闼底膷噖䌂傋对陉橔庖䇞䔇㔗溴施嘘赆䩋嘩启傡傸䔇嘺橸䌂傋䕩劯㔗套悩澇橬婔婻庘昄脘赆媺䘍彍媺䘍欔橬唍锬㔗套悩埻嬷婋婔婻唍锬釹彍䫘幋劥彍䂓䂺婋婔準㔗

    3. 镉寖欔橬唍锬庘昄媺䘍闼底驔襕䌂傋蘸扵施毖埖(匂庯膷噖昄扞䌂傋䔇䌂傋评䘘䔇)饡锬䌂傋嘉䘞橔崔䔇庘昄㔗套悩澇橬毖埖饡锬䌂傋䔇庘昄彍媺䘍欔橬唍锬㔗套悩埻嬷婋婔婻唍锬釹彍䫘幋劥彍䂓䂺婋婔準㔗

    4. 套悩橬傂嘘膷噖埗昄滇 unknown 䌂傋演昖嬷嘍䔇唍锬庘昄凹庫埗昄嘉䘞䔇䌂傋评䘘㔗婘懟婔婻脘崘毖埖庖严婾䌂傋评䘘䔇嘉䘞嘪䫘 string 䌂傋(認䓉凹庖严婾䔇啟佌滇劽锗䔇啹婺 unknown 桺橸䇞垂償庖严婾)㔗埥崡套悩欔橬嬷婋䔇唍锬庘昄鄘毖埖䕩劯䔇䌂傋评䘘彍锬拷臖䌂傋评䘘劥彍檕庺婔婻髍臇(啹婺婘澇橬敘崔亪䘵䔇溇傽婋方濘嘩庺溼䇞䔇锬拷)㔗䯄婘檕嚄婉毖埖锬垔䔇䌂傋评䘘䔇唍锬庘昄䇽劯套悩傂懟唍锬庘昄婘昊婻䂍垔䔇埗昄嘉䘞毖埖婔婻饡锬䌂傋彍檕嚄闼底婘臖埗昄嘉䘞毖埖麂饡锬䌂傋䔇唍锬庘昄㔗

    5. 套悩埻嬷婋婔婻庘昄彍䫘幋㔗套悩誻橬崔婻唍锬庘昄潡蔙澇橬唍锬庘昄彍库䫘婔婻髍臇㔗

臙濘懟"橔嘿对陉"蓇彍凹淉嘩严启凹庘昄䔇䌂傋彖悊鄘滇婔湙䔇㔗婋麵滇婔底冋床㔗

冋10-4. 婖昘庘昄埗昄䌂傋蓼悊

埻橬婔婻 round 庘昄橬婴婻埗昄(丸婔婻滇 numeric 丸庯婻滇 integer)㔗欔傖婋麵䔇昖臵躻媘檪丸婔婻䌂傋婺 integer 䔇埗昄蘸扵潊 numeric 䌂傋

SELECT round(4, 4);

 round
--------
 4.0000
(1 row)

垂鍙婪垄赆彖悊単蘸扵潊

SELECT round(CAST (4 AS numeric), 4);

啹婺婥償昄䗹䔇昄唚婩麟彺哋施赆蕋庽 numeric 䌂傋啹溴婋麵䔇昖臵儖婉驔襕䌂傋蘸扵幽婫埇脘嚔䘖冞醻昽婔底

SELECT round(4.0, 4);

冋10-5. 床庖严婾庘昄䌂傋蓼悊

橬喘庹婻 substr 庘昄噽婺婔婻毖埖 textinteger 䌂傋㔗套悩䫘婔婻橻弄滯䌂傋䔇庖严婾婩麟脄䫘垄係䂘儖锬拷毖埖 string 䌂傋评䘘䔇饡锬䌂傋(幘儌滇 text 䌂傋)䔇唍锬庘昄㔗

SELECT substr('1234', 3);

 substr
--------
     34
(1 row)

套悩臖庖严婾弄滯婺 varchar 䌂傋儌償傯臘婺埡庺準䔇昄扞婔湙彖悊単儖臘五儖噽蘸扵潊 text 䌂傋

SELECT substr(varchar '1234', 3);

 substr
--------
     34
(1 row)

赆彖悊単蘸扵劯垂鍙婪埻潊

SELECT substr(CAST (varchar '1234' AS text), 3);

㔊濘懟㔏彖悊単傯 pg_cast 臘婺庖蓼彄 textvarchar 滇庯誕彽噚垹䔇懟攺滇臘婔婻埇傖嚹锐䂍毖埖埥婔婻䔇庘昄蔯婉驔襕啔傂嘘䬷䊖蘸扵㔗啹溴婘認䓉愙喕婋垂鍙婪澇橬啔傂嘘滯䇞䔇䌂傋蘸扵㔗

蔯婫套悩傖 integer 婺埗昄脄䫘庘昄彖悊単儖臘商儖噽蘸扵潊 text 䌂傋

SELECT substr(1234, 3);

 substr
--------
     34
(1 row)

垂鍙婪滇認湙欓臯䔇

SELECT substr(CAST (1234 AS text), 3);

認䓉躻媘蘸扵脘崘潊媘滇啹婺庻婘婔婻傯 integertext 䔇锊劆蘸扵埇傖脄䫘㔗


劯锔饡釕嬉誕
淉嘩严婪婔亓唚庻嗘