Introduction Database Analyst - duties include teaching class Relational Databases since 1987, mostly DB2 and Oracle
Cheryl’s email: What the hell are scalar functions? You probably have used some of these, even if you’re not familiar with the name. Operate on a single value May be column or expression, including the result of another scalar or columnar function, can be stacked
For each function, the first line shows the syntax. When the string is fixed length, or when you supply a length argument, the substring returned is a fixed length. If no length is specified and the string is variable, the result returned is a variable length string. You need to consider this when you define a host variable.
Length of a character string is more useful when using variable length columns. I didn’t find any varchar columns in our mainframe sasi db, but we would get the actual length of the data. In a cobol program, the length of a variable length column is automatically returned in a host variable anyway, but it can be useful in spufi or qmf. For example, if you have a comments column of 254 bytes, you may not have room to print the longest comments in a report. By querying that column for comments over say 50 or 100 characters, you could get an idea how many rows have lengthy comments and how effective it would be to truncate them.
Here is a case where we have used a function in the where clause – it will cost us some performance.
Removes leading and/or trailing characteres. Blanks are assumed unless specified otherwise. Useful combined with length function, as you will now get the length of string actually used in a fixed length character column.
Although we probably won’t ever use the HEX function in a COBOL program, it can be a useful debugging tool in spufi or qmf.
If you want to insure that the result is not null, the last expression should be either a literal or a column or expression that you know will never be null. This function is especially valuable in a program that accesses columns of numeric data that may contain nulls. If an arithmetic expression refers to a column containing a null value, the result of the expression will be null. You can insure against this by using COALESE to substitute zero for any null value found. It is also useful when selecting columns that may be null into host variables in a COBOL program. When I forgot to check the null indicator for the column “leavedate”, I was getting the leavedate of the last student who had one. Tried to correct by selecting coalesce(leavedate, ‘ ‘). This gave an SQL error saying wrong data type. Data types of arguments have to match. I could coalesce leavedate with a hard-coded date but I didn’t want that. Coalesce(char(leavedate),’ ‘) gave me the result I wanted.
This function allows the conversion of an unwanted value to a null. Not useful in COBOL, since it doesn’t recognize null values, but in another language, such as C++ or Java, you may want to convert zero values to null. Might use it if your database defaulted character columns to spaces when there is no data, but your coding language handled nulls.
First argument names the item Second argument identifies the desired format. DB2 generally defaults to the ISO format, unless your shop has customized the general default.) What is our default?? CHAR can also convert numbers to character strings. Similar to DIGITS, only it includes the decimal place and sign. ISO = International Standards Organization USA = IBM American Standard EUR = IBM European Standard JIS = Japanese Industrial Standard LOCAL = site specific
Keep in mind that if you are fetching dates into a COBOL program, and later want to compare them with other dates, you definitely want the ISO format. For LOCAL to work, the site must have an exit routine for date and time installed on the system, to return the value in the desired format. ---------+---------+---------+---------+---------+---------+---------+--------- select char(enterdate,local) from ssasipit.astu_student where ccsd_id = '357000'; ---------+---------+---------+---------+---------+---------+---------+--------- DSNT408I SQLCODE = -185, ERROR: THE LOCAL FORMAT OPTION HAS BEEN USED WITH A DATE OR TIME AND NO LOCAL EXIT HAS BEEN INSTALLED
A positive number could be used when comparing dates. Will see an example of this with the DAYS function.
Note – microseconds are set to zero
Can determine the days between two dates. First example - difference between two dates. Second example - age of an invoice. This is a lot easier than calculating in a cobol program, taking into consideration month lengths and leap year! You might call a date routine, but using this function in the select stmt could avoid calling another program.
Note in the example that digits to the right of the decimal point are truncated, not rounded. See the Integer example for a method of rounding. (Oracle has a round function.)
I think this would be used for double-byte character languages such as Arabic or Japanese characters.
Our book gives a slick example of using substr to get last name, first initial. It assumes that lastname is a variable-length column (VARCHAR). In SASI, and probably most of our databases, names are fixed length, which will give the second result. See the example of the STRIP function (available in v. 5) for the desired result.
The length function could be used to determine how many characters of a variable length column are actually used. Say you are formatting a report and you’d like to truncate the teacher_comment column, varchar(256). You could find the longest comment:
select max(length(teacher_comment)) as LONGEST_COMMENT
If you only have room for 150 characters, you could find out how many students have comments that would be truncated: