Manipulating Letters and NumbersStringsThis section takes us into the area of strings, which are simply sequences of things, like the letters in this sentence or your name . In the course of your work you'll have to play with strings, and fortunately SQL comes with several good editing tools to help you:
Here's the same SELECT statement, but using a small a : SELECT L_NAME INSTR(L_NAME, 'a') from STUDENTS; The results look like this:
The INSTR command is extremely useful when you're searching the database for particular patterns, names, invalid strings, and so forth. You'll probably be using it a lot. For our final discussion of strings, we're going to take a look at four more tools that you can use to manipulate a string or set: LPAD , RPAD , LTRIM , and RTRIM . (You already know that SUBSTR lets you find a piece of a string.) The PAD twins ” R and L ”let you add characters to the (guess where?) right and left ends of a string. By default the characters are spaces, but you can use anything. Common uses of RPAD and LPAD are to add periods or dashes to help the user read across a line. Here's an example: SELECT RPAD (L_NAME,40,'.'), F_NAME from STUDENTS; This command produces the following report:
Now let's look at the TRIM twins: LTRIM and RTRIM . These commands are used to remove characters at the ends of strings, such as quotation marks. Here are a few examples:
More Functions for Working with StringsTRANSLATESuppose you wanted to modify last names by changing certain characters. You would use the Oracle TRANSLATE command as in this example: SQL> 1* select translate(L_NAME, 'R', 'x') from students Here are the results:
You can use this technique to eliminate characters by putting blanks in the TRANSLATE command: SQL> SELECT TRANSLATE(L_NAME, 'R', ' ') from STUDENTS
The results look like this:
DECODEDECODE is an interesting function. I use it to reverse financial transactions where a duplicate journal entry has to be made but the sign has to be reversed . Here's the general syntax: DECODE (value, if it is this1(a), then change it to this1(b), if it is this2(a), then change it to this2(b), . . . else change it to this) Here's an example:
DECODE can be a very powerful tool when used with other functions. Such programming gets rather complex, and I refer you to more detailed documentation on DECODE. What I have shown you here will more than suffice for most of your normal work, but for those who are curious and have taken a liking to such functions, go and look deeper. Number FunctionsIn this short section I'm going to introduce you to some of the more common mathematical functions that are used in normal business environments. For the serious mathematicians out there, be aware that there is much, much more horsepower in Oracle math functions than we're going to cover here, and more in-depth references are available when you need them. We're going to take a look at AVG , COUNT , MAX , MIN , SUM , GREATEST , and LEAST . Before we start, keep in mind these two things:
NULLLet's digress a bit and talk about NULL . In Oracle, as I have mentioned, NULL means "empty"; it does not mean zeros or spaces. It is an intrinsic feature, and when you run the DESC command on a table, one of the headings will be "NULL?". For example, this command: SQL> desc temp_person_table; yields the following results:
Here none of the columns are set to NULL , which is standard. However, there are some important considerations with NULL values. First, any mathematical computation that uses a NULL value ends up staying null. For example, the following code inserts data for a particular person into COURSE_TABLE with a NULL value for HOURS , and then increases the hours for that person: SQL> RUN 1 INSERT into COURSE_TABLE values ( 2* 987654321, 16, '12-APR-02', 'Satellite', 65.25, null ) 1 row created. SQL> RUN 1* UPDATE COURSE_TABLE set hours = (hours +12) WHERE PERSON_ID = 987654321 1 row updated.
Now take a look at the HOURS value for person 987654321. You would expect 12 hours, but it is still NULL . Run a simple SQL statement to select everything from the COURSE_TABLE : SQL> select * from course_table; The results look like this:
Now that you know how NULL values work, think about other implications. Other math activities, such as AVG , SUM , MIN , MAX , and COUNT will skip any NULL columns, possibly making the results meaningless. You could do a count on all the rows in a table, then do a count on just one column, and get different results. The reason is that COUNT will count all rows but will skip NULL columns! You can test for NULL values, but you must use the correct syntax. Instead of the equal sign and so forth, you must use the following general syntax: "If Person_ID IS NULL . . . (do something)", or "If Person_ID IS NOT NULL . . . (do something)". Now let's take a look at the test scores to date for all students:
Suppose that the director of training comes to you and wants a quick synopsis of the scores ”no details, just a summary. Here's where you can combine math functions and deliver a neat, quick, easily understood overview. Just use the following SELECT command: SQL> SELECT AVG(SCORE), COUNT(SCORE), MAX(SCORE), MIN(SCORE), SUM(SCORE) 2 from TEST_HISTORY; And here are the results:
Note the use of the various math commands, and when the director comes to you and says she would like a breakdown so that she can see which exam is getting the lowest score, you refine the SELECT command like this: SELECT TEST_ID, AVG(SCORE) from TEST_HISTORY GROUP BY TEST_ID;
The results look like this:
Now that the director can see the average scores, she asks you to show details for Test 2. Just keep refining your SELECT statement, as here: SQL> SELECT STUDENT_ID, SCORE from TEST_HISTORY WHERE TEST_ID = 2 Now we get these results:
Since you've done such a good job getting the information so quickly, the director now wants to see how student 111111111 has done on all tests. By now I'm sure you can figure out the SELECT command for that! But what I want you to realize is that you have really created a group of queries that should become standard reports that the director can run when-ever she wants. So go ahead and create the scripts, using standard coding, as well as prompting the user for input, such as which test or which student. In going through these rather simple exercises, you will have discovered what is valuable to your users. |