版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
Oracle中的varchar2類型及其字符串截取1、varchar2類型介紹1.1Oracle數(shù)據(jù)庫常?的數(shù)據(jù)類型介紹varchar2類型的同時(shí),我們順便了解下Oracle數(shù)據(jù)庫?持的其他數(shù)據(jù)類型。如下摘?Oracle官??檔Built-inDataTypeSummary,簡(jiǎn)單介紹了Oracle中常?的數(shù)據(jù)類型。CDatoaDescriptionMemodTypeevarchar2中指定的長(zhǎng)度參數(shù),指的是最多可以存儲(chǔ)的長(zhǎng)度,實(shí)際存儲(chǔ)超過這個(gè)長(zhǎng)度的內(nèi)容會(huì)報(bào)VARCHAR2(size[BYTE|Variable-lengthcharacterstringhavingmaximumlengthsizebytesorcharacters.Maximumsizeis4000bytesorcharacters,andminimumis1byteor1character.YoumustspecifysizeforVARCHAR2.BYTEindicatesthatthecolumnwillhavebytelengthsemantics.CHARindicatesthatthecolumnwillhavecharactersemantics.錯(cuò)。類型中var的意思是說,存儲(chǔ)?于該長(zhǎng)度的內(nèi)容時(shí),實(shí)際占?的存儲(chǔ)根據(jù)具體存儲(chǔ)內(nèi)容長(zhǎng)度?定。?如定義varchar2(10)實(shí)際存儲(chǔ)內(nèi)容為3就占?長(zhǎng)度為3的存儲(chǔ)1CHAR])空間。相?于定長(zhǎng)的char,可以節(jié)省存儲(chǔ)空間。Variable-lengthUnicodecharacterstringhavingmaximumlengthsizecharacters.ThenumberofbytescanbeuptotwotimessizeforNVARCHAAL16UTF16encodingandthreetimessizeforUTF8encoding.R2(sizMaximumsizeisdeterminedbythenationalcharactersetdefinition,1主要是?來存儲(chǔ)Unicode字符的e)withanupperlimitof4000bytes.YoumustspecifysizeforNVARCHAR2.NUMNumberhavingprecisionpandscales.TheprecisionpcanrangeBER[from1to38.Thescalescanrangefrom-84to127.Bothprecision2(p[,s])]andscaleareindecimaldigits.ANUMBERvaluerequiresfrom1to22bytes.AsubtypeoftheNUMBERdatatypehavingprecisionp.AFLOATFLOAvalueisrepresentedinternallyasNUMBER.Theprecisionpcan28T[(p)]rangefrom1to126binarydigits.AFLOATvaluerequiresfrom1to22bytes.LONGCharacterdataofvariablelengthupto2gigabytes,or231-1bytes.Providedforbackwardcompatibility.表?的?期范圍可以是公元前4712年1?1??公元9999年12?31?。YoucanaddandsubtractconstantstoandfromaDATEvalue,andthesenumberswillbeinterpretedasnumbersofdays.Forexample,SYSDATE+1willbetomorrow.YoucannotValiddaterangefromJanuary1,4712BC,toDecember31,9999AD.Thedefaultformatisdeterminedexplicitlybythe12DATNLS_DATE_FORMATparameterorimplicitlybytheNLS_TERRITORYEparameter.Thesizeisfixedat7bytes.ThisdatatypecontainsthedatetimefieldsYEAR,MONTH,DAY,HOUR,MINUTE,andSECOND.Itdoesnothavefractionalsecondsoratimezone.multiplyordivideDATEvalues.100BINARY_FLOAT32-bitfloatingpointnumber.Thisdatatyperequires4bytes.BINA1RY_DCDat64-bitfloatingpointnumber.Thisdatatyperequires8bytes.Description0oOUBLa1EdTypMemoeeYear,month,anddayvaluesofdate,aswellashour,minute,andsecondvaluesoftime,wherefractional_seconds_precisionisthenumberofdigitsinthefractionalpartoftheSECONDdatetimefield.Acceptedvaluesoffractional_seconds_precisionare0to9.Thedefaultis6.ThedefaultformatisdeterminedexplicitlybytheNLS_TIMESTAMP_FORMATparameterorimplicitlybytheNLS_TERRITORYparameter.Thesizeis7or11bytes,dependingontheprecision.ThisdatatypecontainsthedatetimefieldsYEAR,MONTH,DAY,HOUR,MINUTE,andSECOND.Itcontainsfractionalsecondsbutdoesnothaveatimezone.TIMESTAMP180[(fractional_seconds_precision)]TIMESTAMPAllvaluesofTIMESTAMPaswellastimezonedisplacementvalue,wherefractional_seconds_precisionisthenumberofdigitsinthefractionalpartoftheSECONDdatetimefield.Acceptedvaluesare0to9.Thedefaultis6.Thedefaultformatisdeterminedexplicitlyby[(fractional_secon181ds_prtheNLS_TIMESTAMP_FORMATparameterorimplicitlybytheecision)]NLS_TERRITORYparameter.Thesizeisfixedat13bytes.ThisdatatypecontainsthedatetimefieldsYEAR,MONTH,DAY,HOUR,WITHMINUTE,SECOND,TIMEZONE_HOUR,andTIMEZONE_MINUTE.IthasTIMEZONEfractionalsecondsandanexplicittimezone.TIMESTAMP[(fractional_AllvaluesofTIMESTAMPWITHTIMEZONE,withthefollowingseconds_prexceptions:Dataisnormalizedtothedatabasetimezonewhenitis231storedinthedatabase.Whenthedataisretrieved,usersseetheecisiodatainthesessiontimezone.Thedefaultformatisdeterminedn)]explicitlybytheNLS_TIMESTAMP_FORMATparameterorimplicitlyWITHbytheNLS_TERRITORYparameter.Thesizeis7or11bytes,LOCAdependingontheprecision.LTIMEZONEINTERVALYEAR182[(yearStoresaperiodoftimeinyearsandmonths,whereyear_precisionis_precithenumberofdigitsintheYEARdatetimefield.Acceptedvaluesaresion)]0to9.Thedefaultis2.Thesizeisfixedat5bytes.TOMONTHINTERVALDAY[(day_precisStoresaperiodoftimeindays,hours,minutes,andseconds,whereion)]day_precisionisthemaximumnumberofdigitsintheDAYdatetimeTO183field.Acceptedvaluesare0to9.Thedefaultis2.SECOfractional_seconds_precisionisthenumberofdigitsinthefractionalNDpartoftheSECONDfield.Acceptedvaluesare0to9.Thedefaultis[(fracti6.Thesizeisfixedat11bytes.onal_seconCDatds_praoecisiodTypn)]DescriptionMemoee23RAW(size)Rawbinarydataoflengthsizebytes.Maximumsizeis2000bytes.YoumustspecifysizeforaRAWvalue.LONG24Rawbinarydataofvariablelengthupto2gigabytes.RAWBase64stringrepresentingtheuniqueaddressofarowinitstable.ThisdatatypeisprimarilyforvaluesreturnedbytheROWIDpseudocolumn.69ROWIDUROWID[(size)]208Base64stringrepresentingthelogicaladdressofarowofanindex-organizedtable.TheoptionalsizeisthesizeofacolumnoftypeUROWID.Themaximumsizeanddefaultis4000bytes.CHARFixed-lengthcharacterdataoflengthsizebytesorcharacters.Maximumsizeis2000bytesorcharacters.Defaultandminimumsizeis1byte.BYTEandCHARhavethesamesemanticsasforVARCHAR2.[(size[BYTE|96CHAR])]Fixed-lengthcharacterdataoflengthsizecharacters.ThenumberofNCHbytescanbeuptotwotimessizeforAL16UTF16encodingandAR[(sithreetimessizeforUTF8encoding.Maximumsizeisdeterminedby96ze)]thenationalcharactersetdefinition,withanupperlimitof2000bytes.Defaultandminimumsizeis1character.Acharacterlargeobjectcontainingsingle-byteormultibyte112CLOcharacters.Bothfixed-widthandvariable-widthcharactersetsareBsupported,bothusingthedatabasecharacterset.Maximumsizeis(4gigabytes-1)*(databaseblocksize).AcharacterlargeobjectcontainingUnicodecharacters.Bothfixed-widthandvariable-widthcharactersetsaresupported,bothusingthedatabasenationalcharacterset.Maximumsizeis(4gigabytes-1)*(databaseblocksize).Storesnationalcharactersetdata.112NCLOB113BLOBAbinarylargeobject.Maximumsizeis(4gigabytes-1)*(databaseblocksize).114Containsalocatortoalargebinaryfilestoredoutsidethedatabase.EnablesbytestreamI/OaccesstoexternalLOBsresidingonthedatabaseserver.Maximumsizeis4gigabytes.BFILE需要注意的是,如果在聲明varchar2類型長(zhǎng)度的時(shí)候,沒有顯式指定是BYTE還是CHAR,缺省的情況取決于數(shù)據(jù)庫參數(shù)NLS_LENGTH_SEMANTICS。具體可以通過如下sql查詢,如下是?個(gè)例?:selectname,valuefromv$parameterwherename='nls_length_semantics';結(jié)果:NAMEVALUEnls_length_semanticsBYTENAMEVALUE在這個(gè)例?中,缺省的情況,就是按照字節(jié)長(zhǎng)度來計(jì)算。1.2Oracle中varchar2和varchar的區(qū)別截?現(xiàn)在,varchar和varchar2是沒有區(qū)別的。按照ANSI的標(biāo)準(zhǔn),varchar在Oracle中是被保留的,?來后續(xù)?持區(qū)分NULL和空字符串。VARCHAR2現(xiàn)在不區(qū)分NULL和空字符串,以后也不會(huì)區(qū)分。如果程序?qū)τ贜ULL和空字符串等同有依賴,就應(yīng)該使?varchar2類型。2、Oracle中截取字符串2.1substr函數(shù)官?說明如下:{SUBSTR|SUBSTRB|SUBSTRC|SUBSTR2|SUBSTR4}(char,position[,substring_length])TheSUBSTRfunctionsreturnaportionofchar,beginningatcharacterposition,substring_lengthcharacterslong.SUBSTRcalculateslengthsusingcharactersasdefinedbytheinputcharacterset.SUBSTRBusesbytesinsteadofcharacters.SUBSTRCusesUnicodecompletecharacters.SUBSTR2usesUCS2codepoints.SUBSTR4usesUCS4codepoints.Ifpositionis0,thenitistreatedas1.Ifpositionispositive,thenOracleDatabasecountsfromthebeginningofchartofindthefirstcharacter.Ifpositionisnegative,thenOraclecountsbackwardfromtheendofchar.Ifsubstring_lengthisomitted,thenOraclereturnsallcharacterstotheendofchar.Ifsubstring_lengthislessthan1,thenOraclereturnsnull.charcanbeanyofthedatatypesCHAR,VARCHAR2,NCHAR,NVARCHAR2,CLOB,orNCLOB.Bothpositionandsubstring_lengthmustbeofdatatypeNUMBER,oranydatatypethatcanbeimplicitlyconvertedtoNUMBER,andmustresolvetoaninteger.Thereturnvalueisthesamedatatypeaschar.Floating-pointnumberspassedasargumentstoSUBSTRareautomaticallyconvertedtointegers.?法介紹:substr函數(shù):截取字符串語法:SUBSTR(string,start,[length])string:表?源字符串,即要截取的字符串。start:開始位置,從1開始查找。如果start是負(fù)數(shù),則從string字符串末尾開始算起。length:可選項(xiàng),表?截取字符串長(zhǎng)度。下?是?個(gè)例?:SELECT'截取所有字符串'func,'SUBSTR(''HelloSQL!'',1)'expr,SUBSTR('HelloSQL!',1)expr_valFROMdualunionallSELECT'從第2個(gè)字符開始,截取到末尾'func,'SUBSTR(''HelloSQL!'',2)'expr,SUBSTR('HelloSQL!',2)expr_valFROMdualunionallSELECT'從倒數(shù)第4個(gè)字符開始,截取到末尾'func,'SUBSTR(''HelloSQL!'',-4)'expr,SUBSTR('HelloSQL!',-4)expr_valFROMdualunionallSELECT'從第3個(gè)字符開始,截取6個(gè)字符'func,'SUBSTR(''HelloSQL!'',3,6)'expr,SUBSTR('HelloSQL!',3,6)expr_valFROMdualunionallSELECT'從倒數(shù)第4個(gè)字符開始,截取3個(gè)字符'func,'SUBSTR(''HelloSQL!'',-4,3)'expr,SUBSTR('HelloSQL!',-4,3)expr_valFROMdualunionallSELECT'從截取開始位置超過字符串長(zhǎng)度'func,'SUBSTR(''HelloSQL!'',20,3)'expr,SUBSTR('HelloSQL!',20,3)expr_valFROMdual結(jié)果如下:substr例?注:上圖中的-表?空。2.2根據(jù)指定字符出現(xiàn)的位置截取字符串這?需要?到oracle中的instr函數(shù),?法如下。instr函數(shù):返回?字符串在源字符串中的位置語法:INSTR(string,child_string,[start],[show_time])string:表?源字符串。child_string:?字符串,即要查找的字符串。start:可選項(xiàng),開始位置,默認(rèn)從1開始。如果為負(fù)數(shù),則從右向左檢索。show_time:可選項(xiàng),表??字符串第?次出現(xiàn)在源字符串當(dāng)中,默認(rèn)第1次,負(fù)數(shù)則報(bào)錯(cuò)。返回指定字符出現(xiàn)的位置從1開始,如果找不到返回0下?是?個(gè)例?:SELECT'查找字符串中l(wèi)出現(xiàn)的位置'func,'INSTR(''HelloSQL!'',''l'')'expr,INSTR('HelloSQL!','l')expr_valFROMdualunionallSELECT'查找字符串中H出現(xiàn)的位置'func,'INSTR(''HelloSQL!'',''H'')'expr,INSTR('HelloSQL!','H')expr_valFROMdualunionallSELECT'查找字符串中l(wèi)出現(xiàn)的位置'func,'INSTR(''HelloSQL!'',''l'',1)'expr,INSTR('HelloSQL!','l',1)expr_valFROMdualunionallSELECT'查找字符串中l(wèi)l出現(xiàn)的位置'func,'INSTR(''HelloSQL!'',''ll'')'expr,INSTR('HelloSQL!','ll')expr_valFROMdualunionallSELECT'查找字符串中aa出現(xiàn)的位置'func,'INSTR(''HelloSQL!'',''aa'')'expr,INSTR('HelloSQL!','aa')expr_valFROMdualunionallSELECT'查找字符串中l(wèi)第2次出現(xiàn)的位置'func,'INSTR(''HelloSQL!'',''l'',1,2)'expr,INSTR('HelloSQL!','l',1,2)expr_valFROMdual結(jié)果如下:instr例?結(jié)合instr?如我們想截取某個(gè)字符出現(xiàn)位置之前的字符,下?是?個(gè)例?。--截取空格前的內(nèi)容SELECTSUBSTR('HelloSQL!',1,INSTR('HelloSQL!','')-1)FROMdual--Hello--截取空格后的內(nèi)容SELECTSUBSTR('HelloSQL!',INSTR('HelloSQL!','')+1)FROMdual--SQL!2.3substrb按字節(jié)截取字符串2.3.1Oracle數(shù)據(jù)中?占?個(gè)字節(jié)可以通過查看如下參數(shù)(查詢當(dāng)前數(shù)據(jù)庫的字符集)來了解當(dāng)前的oracle環(huán)境中?個(gè)漢字占?個(gè)字節(jié):SELECT*FROMv$nls_parametersWHEREPARAMETER='NLS_CHARACTERSET';如果value=ZHS16GBK,那么?個(gè)漢字占?2個(gè)字節(jié);如果value=AL32UTF8,那么?個(gè)漢字占?3個(gè)字節(jié)。另外?個(gè)參數(shù)也可以:selectuserenv('language')fromdual;結(jié)果如下:列USERENV('LANGUAGE')值A(chǔ)MERICAN_AMERICA.AL32UTF8這?AL32UTF8說明當(dāng)前環(huán)境下,?個(gè)漢字占3個(gè)字節(jié)。實(shí)際上,也可以通過語句進(jìn)?測(cè)試,如下:select'length(''美喵泡泡'')'expr,length('美喵泡泡')expr_valfromdualunionallselect'lengthb(''美喵泡泡'')'expr,lengthb('美喵泡泡')expr_valfromdual結(jié)果如下:exprexpr_vallength('美喵泡泡')4lengthb('美喵泡泡')12exprexpr_val2.3.2Oracle多字節(jié)數(shù)據(jù)截取如下是?個(gè)跨字節(jié)截取的例?:select'dump(''美喵泡泡'')'expr,dump('美喵泡泡')expr_valfromdualunionallselect'substrb(''美喵泡泡'',1,3)'expr,substr
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年教科新版必修3生物下冊(cè)月考試卷含答案
- 2025年新科版選擇性必修3生物下冊(cè)階段測(cè)試試卷含答案
- 2025年湘師大新版選擇性必修1化學(xué)上冊(cè)月考試卷含答案
- 2025年滬科新版高三歷史上冊(cè)月考試卷含答案
- 美容院二零二五年度美容儀器研發(fā)與創(chuàng)新基金投資合同4篇
- 2025年度綠色生態(tài)門面房購置與生態(tài)旅游開發(fā)合同4篇
- 二零二五年度美容院?jiǎn)T工績(jī)效考核與激勵(lì)合同4篇
- 承攬合同的范本(2篇)
- 2025年度新能源汽車出租車股權(quán)轉(zhuǎn)讓合同范本4篇
- 二零二五版泥漿外運(yùn)與固體廢棄物處理合同4篇
- 課題申報(bào)書:GenAI賦能新質(zhì)人才培養(yǎng)的生成式學(xué)習(xí)設(shè)計(jì)研究
- 駱駝祥子-(一)-劇本
- 全國醫(yī)院數(shù)量統(tǒng)計(jì)
- 《中國香文化》課件
- 2024年醫(yī)美行業(yè)社媒平臺(tái)人群趨勢(shì)洞察報(bào)告-醫(yī)美行業(yè)觀察星秀傳媒
- 第六次全國幽門螺桿菌感染處理共識(shí)報(bào)告-
- 天津市2023-2024學(xué)年七年級(jí)上學(xué)期期末考試數(shù)學(xué)試題(含答案)
- 經(jīng)濟(jì)學(xué)的思維方式(第13版)
- 盤錦市重點(diǎn)中學(xué)2024年中考英語全真模擬試卷含答案
- 湖北教育出版社四年級(jí)下冊(cè)信息技術(shù)教案
- 背景調(diào)查報(bào)告
評(píng)論
0/150
提交評(píng)論