📄 ch04.htm
字号:
3.1415 1.7724277 45 6.7082039 5 2.236068 57.667 7.5938791 15 3.8729833 7.2 2.6832816 0 0</FONT></PRE><PRE><FONT COLOR="#0066FF">7 rows selected.</FONT></PRE><H2><FONT COLOR="#000077">Character Functions</FONT></H2><P>Many implementations of SQL provide functions to manipulate characters and stringsof characters. This section covers the most common character functions. The examplesin this section use the table <TT>CHARACTERS</TT>.</P><H5>INPUT/<FONT COLOR="#000000">OUTPUT:</FONT></H5><PRE><FONT COLOR="#0066FF">SQL> <B>SELECT * FROM CHARACTERS;</B></FONT></PRE><PRE><FONT COLOR="#0066FF">LASTNAME FIRSTNAME M CODE--------------- --------------- - ---------PURVIS KELLY A 32TAYLOR CHUCK J 67CHRISTINE LAURA C 65ADAMS FESTER M 87COSTALES ARMANDO A 77KONG MAJOR G 52</FONT></PRE><PRE><FONT COLOR="#0066FF">6 rows selected.</FONT></PRE><H3><FONT COLOR="#000077">CHR</FONT></H3><P><TT>CHR</TT> returns the character equivalent of the number it uses as an argument.The character it returns depends on the character set of the database. For this examplethe database is set to ASCII. The column <TT>CODE</TT> includes numbers.</P><H5>INPUT:</H5><PRE><FONT COLOR="#0066FF">SQL> <B>SELECT CODE, CHR(CODE)</B> 2 <B> FROM CHARACTERS;</B></FONT></PRE><H5><FONT COLOR="#000000">OUTPUT:</FONT></H5><PRE><FONT COLOR="#0066FF"> CODE CH--------- -- 32 67 C 65 A 87 W 77 M 52 4</FONT></PRE><PRE><FONT COLOR="#0066FF">6 rows selected.</FONT></PRE><P>The space opposite the <TT>32</TT> shows that <TT>32</TT> is a space in the ASCIIcharacter set.<H3><FONT COLOR="#000077">CONCAT</FONT></H3><P>You used the equivalent of this function on Day 3, when you learned about operators.The <TT>||</TT> symbol splices two strings together, as does <TT>CONCAT</TT>. Itworks like this:</P><H5>INPUT:</H5><PRE><FONT COLOR="#0066FF">SQL> <B>SELECT CONCAT(FIRSTNAME, LASTNAME) "FIRST AND LAST NAMES" </B> 2<B> FROM CHARACTERS;</B></FONT></PRE><H5><FONT COLOR="#000000">OUTPUT:</FONT></H5><PRE><FONT COLOR="#0066FF">FIRST AND LAST NAMES------------------------KELLY PURVISCHUCK TAYLORLAURA CHRISTINEFESTER ADAMSARMANDO COSTALESMAJOR KONG6 rows selected.</FONT></PRE><H5>ANALYSIS:</H5><P>Quotation marks surround the multiple-word alias <TT>FIRST AND LAST NAMES</TT>.Again, it is safest to check your implementation to see if it allows multiple-wordaliases.</P><P>Also notice that even though the table looks like two separate columns, what youare seeing is one column. The first value you concatenated, <TT>FIRSTNAME</TT>, is15 characters wide. This operation retained all the characters in the field.<H3><FONT COLOR="#000077">INITCAP</FONT></H3><P><TT>INITCAP</TT> capitalizes the first letter of a word and makes all other characterslowercase.</P><H5>INPUT:</H5><PRE><FONT COLOR="#0066FF">SQL> <B>SELECT FIRSTNAME BEFORE, INITCAP(FIRSTNAME) AFTER </B> 2 <B> FROM CHARACTERS;</B></FONT></PRE><H5><FONT COLOR="#000000"><B>OUTPUT:</B></FONT></H5><PRE><FONT COLOR="#0066FF">BEFORE AFTER-------------- ----------KELLY KellyCHUCK ChuckLAURA LauraFESTER FesterARMANDO ArmandoMAJOR Major</FONT></PRE><PRE><FONT COLOR="#0066FF">6 rows selected.</FONT></PRE><H3><FONT COLOR="#000077">LOWER and UPPER</FONT></H3><P>As you might expect, <TT>LOWER</TT> changes all the characters to lowercase; <TT>UPPER</TT>does just the reverse.</P><P>The following example starts by doing a little magic with the <TT>UPDATE</TT>function (you learn more about this next week) to change one of the values to lowercase:</P><H5>INPUT:</H5><PRE><FONT COLOR="#0066FF">SQL> <B>UPDATE CHARACTERS </B> 2<B> SET FIRSTNAME = 'kelly' </B> 3<B> WHERE FIRSTNAME = 'KELLY';</B></FONT></PRE><H5><FONT COLOR="#000000">OUTPUT:</FONT></H5><PRE><FONT COLOR="#0066FF">1 row updated.</FONT></PRE><H5><FONT COLOR="#000000">INPUT:</FONT></H5><PRE><FONT COLOR="#0066FF">SQL> <B>SELECT FIRSTNAME </B> 2 <B> FROM CHARACTERS;</B></FONT></PRE><H5><FONT COLOR="#000000"><B>OUTPUT:</B></FONT></H5><PRE><FONT COLOR="#0066FF">FIRSTNAME---------------kellyCHUCKLAURAFESTERARMANDOMAJOR</FONT></PRE><PRE><FONT COLOR="#0066FF">6 rows selected.</FONT></PRE><P>Then you write</P><H5>INPUT:</H5><PRE><FONT COLOR="#0066FF">SQL> <B>SELECT FIRSTNAME, UPPER(FIRSTNAME), LOWER(FIRSTNAME) </B>2<B> FROM CHARACTERS;</B></FONT></PRE><H5><FONT COLOR="#000000"><B>OUTPUT:</B></FONT></H5><PRE><FONT COLOR="#0066FF">FIRSTNAME UPPER(FIRSTNAME LOWER(FIRSTNAME--------------- --------------- ---------------kelly KELLY kellyCHUCK CHUCK chuckLAURA LAURA lauraFESTER FESTER festerARMANDO ARMANDO armandoMAJOR MAJOR major</FONT></PRE><PRE><FONT COLOR="#0066FF">6 rows selected.</FONT></PRE><P>Now you see the desired behavior.<H3><FONT SIZE="5" COLOR="#000077"><TT>LPAD</TT></FONT><FONT COLOR="#000077"> and</FONT><FONT SIZE="5" COLOR="#000077"><TT>RPAD</TT></FONT></H3><P><TT>LPAD</TT> and <TT>RPAD</TT> take a minimum of two and a maximum of three arguments.The first argument is the character string to be operated on. The second is the numberof characters to pad it with, and the optional third argument is the character topad it with. The third argument defaults to a blank, or it can be a single characteror a character string. The following statement adds five pad characters, assumingthat the field <TT>LASTNAME</TT> is defined as a 15-character field:</P><H5>INPUT:</H5><PRE><FONT COLOR="#0066FF">SQL> <B>SELECT LASTNAME, LPAD(LASTNAME,20,'*') </B> 2<B> FROM CHARACTERS;</B></FONT></PRE><H5><FONT COLOR="#000000"><B>OUTPUT:</B></FONT></H5><PRE><FONT COLOR="#0066FF">LASTNAME LPAD(LASTNAME,20,'*'-------------- --------------------PURVIS *****PURVISTAYLOR *****TAYLORCHRISTINE *****CHRISTINEADAMS *****ADAMSCOSTALES *****COSTALESKONG *****KONG</FONT></PRE><PRE><FONT COLOR="#0066FF">6 rows selected.</FONT></PRE><H5>ANALYSIS:</H5><P>Why were only five pad characters added? Remember that the <TT>LASTNAME</TT> columnis 15 characters wide and that <TT>LASTNAME</TT> includes the blanks to the rightof the characters that make up the name. Some column data types eliminate paddingcharacters if the width of the column value is less than the total width allocatedfor the column. Check your implementation. Now try the right side:</P><H5>INPUT:</H5><PRE><FONT COLOR="#0066FF">SQL> <B>SELECT LASTNAME, RPAD(LASTNAME,20,'*') </B> 2<B> FROM CHARACTERS;</B></FONT></PRE><H5><FONT COLOR="#000000"><B>OUTPUT:</B></FONT></H5><PRE><FONT COLOR="#0066FF">LASTNAME RPAD(LASTNAME,20,'*'--------------- --------------------PURVIS PURVIS *****TAYLOR TAYLOR *****CHRISTINE CHRISTINE *****ADAMS ADAMS *****COSTALES COSTALES *****KONG KONG *****</FONT></PRE><PRE><FONT COLOR="#0066FF">6 rows selected.</FONT></PRE><H5>ANALYSIS:</H5><P>Here you see that the blanks are considered part of the field name for these operations.The next two functions come in handy in this type of situation.<H3><FONT SIZE="6" COLOR="#000077"><TT>LTRIM</TT></FONT><FONT COLOR="#000077"> and</FONT><FONT SIZE="6" COLOR="#000077"><TT>RTRIM</TT></FONT></H3><P><TT>LTRIM</TT> and <TT>RTRIM</TT> take at least one and at most two arguments.The first argument, like <TT>LPAD</TT> and <TT>RPAD</TT>, is a character string.The optional second element is either a character or character string or defaultsto a blank. If you use a second argument that is not a blank, these trim functionswill trim that character the same way they trim the blanks in the following examples.</P><H5>INPUT:</H5><PRE><FONT COLOR="#0066FF">SQL> <B>SELECT LASTNAME, RTRIM(LASTNAME) </B> 2<B> FROM CHARACTERS;</B></FONT></PRE><H5><FONT COLOR="#000000">OUTPUT:</FONT></H5><PRE><FONT COLOR="#0066FF">LASTNAME RTRIM(LASTNAME)--------------- ---------------PURVIS PURVISTAYLOR TAYLORCHRISTINE CHRISTINEADAMS ADAMSCOSTALES COSTALESKONG KONG</FONT></PRE><PRE><FONT COLOR="#0066FF">6 rows selected.</FONT></PRE><P>You can make sure that the characters have been trimmed with the following statement:</P><H5>INPUT:</H5><PRE><FONT COLOR="#0066FF">SQL> <B>SELECT LASTNAME, RPAD(RTRIM(LASTNAME),20,'*') </B> 2<B> FROM CHARACTERS;</B></FONT></PRE><H5><FONT COLOR="#000000">OUTPUT:</FONT></H5><PRE><FONT COLOR="#0066FF">LASTNAME RPAD(RTRIM(LASTNAME)--------------- --------------------PURVIS PURVIS**************TAYLOR TAYLOR**************CHRISTINE CHRISTINE***********ADAMS ADAMS***************COSTALES COSTALES************KONG KONG****************</FONT></PRE><PRE><FONT COLOR="#0066FF">6 rows selected.</FONT></PRE><P>The output proves that trim is working. Now try <TT>LTRIM</TT>:</P><H5>INPUT:</H5><PRE><FONT COLOR="#0066FF">SQL> <B>SELECT LASTNAME, LTRIM(LASTNAME, 'C')</B> 2 <B> FROM CHARACTERS;</B></FONT></PRE><H5><FONT COLOR="#000000">OUTPUT:</FONT></H5><PRE><FONT COLOR="#0066FF">LASTNAME LTRIM(LASTNAME,--------------- ---------------PURVIS PURVISTAYLOR TAYLORCHRISTINE HRISTINEADAMS ADAMSCOSTALES OSTALESKONG KONG</FONT></PRE><PRE><FONT COLOR="#0066FF">6 rows selected.</FONT></PRE><P>Note the missing Cs in the third and fifth rows.<H3><FONT COLOR="#000077">REPLACE</FONT></H3><P><TT>REPLACE</TT> does just that. Of its three arguments, the first is the stringto be searched. The second is the search key. The last is the optional replacementstring. If the third argument is left out or <TT>NULL</TT>, each occurrence of thesearch key on the string to be searched is removed and is not replaced with anything.</P><H5>INPUT:</H5><PRE><FONT COLOR="#0066FF">SQL> <B>SELECT LASTNAME, REPLACE(LASTNAME, 'ST') REPLACEMENT </B> 2 <B> FROM CHARACTERS;</B></FONT></PRE><H5><FONT COLOR="#000000"><B>OUTPUT:</B></FONT></H5><PRE><FONT COLOR="#0066FF">LASTNAME REPLACEMENT--------------- ---------------PURVIS PURVISTAYLOR TAYLORCHRISTINE CHRIINEADAMS ADAMSCOSTALES COALESKONG KONG</FONT></PRE><PRE><FONT COLOR="#0066FF">6 rows selected.</FONT></PRE><P>If you have a third argument, it is substituted for each occurrence of the searchkey in the target string. For example:</P><H5>INPUT:</H5><PRE><FONT COLOR="#0066FF">SQL> <B>SELECT LASTNAME, REPLACE(LASTNAME, 'ST','**') REPLACEMENT</B> 2 <B> FROM CHARACTERS;</B></FONT></PRE><H5><FONT COLOR="#000000">OUTPUT:</FONT></H5><PRE><FONT COLOR="#0066FF">LASTNAME REPLACEMENT--------------- ------------PURVIS PURVISTAYLOR TAYLORCHRISTINE CHRI**INEADAMS ADAMSCOSTALES CO**ALESKONG KONG</FONT></PRE><PRE><FONT COLOR="#0066FF">6 rows selected.</FONT></PRE><P>If the second argument is <TT>NULL</TT>, the target string is returned with nochanges.</P><H5>INPUT:</H5><PRE><FONT COLOR="#0066FF">SQL> <B>SELECT LASTNAME, REPLACE(LASTNAME, NULL) REPLACEMENT</B> 2 <B> FROM CHARACTERS;</B></FONT></PRE><H5><FONT COLOR="#000000">OUTPUT:</FONT></H5><PRE><FONT COLOR="#0066FF">LASTNAME REPLACEMENT--------------- ---------------PURVIS PURVISTAYLOR TAYLORCHRISTINE CHRISTINEADAMS ADAMSCOSTALES COSTALESKONG KONG</FONT></PRE><PRE><FONT COLOR="#0066FF">6 rows selected.</FONT></PRE><H3><FONT COLOR="#000077">SUBSTR</FONT></H3><P>This three-argument function enables you to take a piece out of a target string.The first argument is the target string. The second argument is the position of thefirst character to be output. The third argument is the number of characters to show.</P><H5>INPUT:</H5><PRE><FONT COLOR="#0066FF">SQL> <B>SELECT FIRSTNAME, SUBSTR(FIRSTNAME,2,3)</B> 2<B> FROM CHARACTERS;</B></FONT></PRE><H5><FONT COLOR="#000000"><B>OUTPUT:</B></FONT></H5><PRE><FONT COLOR="#000000"><B></B></FONT></PRE><PRE><FONT COLOR="#0066FF">FIRSTNAME SUB--------------- ---kelly ellCHUCK HUCLAURA AURFESTER ESTARMANDO RMAMAJOR AJO</FONT></PRE><PRE><FONT COLOR="#0066FF">6 rows selected.</FONT></PRE><P>If you use a negative number as the second argument, the starting point is determinedby counting backwards from the end, like this:</P><H5>INPUT:</H5><PRE><FONT COLOR="#0066FF">SQL> <B>SELECT FIRSTNAME, SUBSTR(FIRSTNAME,-13,2) </B> 2<B> FROM CHARACTERS;</B></FONT></PRE><H5><FONT COLOR="#000000">OUTPUT:</FONT></H5><PRE><FONT COLOR="#0066FF">FIRSTNAME SU--------------- --kelly llCHUCK UCLAURA URFESTER STARMANDO MAMAJOR JO</FONT></PRE><PRE><FONT COLOR="#0066FF">6 rows selected.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -