Table 8.62 Concat Results CONCAT_STR MANAGER-CLARK PRESIDENT-KING CLERK-MILLER Greatest The greatest function returns the alphanumerically greatest string in its parameter list.. Table 8
Trang 1The chr function returns the character associated with the number in the database’s character set If USING NCHAR_CS is specified, it would use the database’s national character set
SELECT chr(65) SIXTY_FIVE ,
chr(65 USING NCHAR_CS) SIXTY_FIVE_NATIONAL FROM DUAL
Table 8.61 lists the results
Table 8.61 Chr Results
SIXTY_FIVE SIXTY_FIVE_NATIONAL
Concat
The concat function is equivalent to the || operator It merges two strings
SELECT concat(job,concat(‘-’,ename)) AS concat_str
FROM emp
WHERE deptno=10
Table 8.62 lists the results
Table 8.62 Concat Results
CONCAT_STR
MANAGER-CLARK
PRESIDENT-KING
CLERK-MILLER
Greatest
The greatest function returns the alphanumerically greatest string in its parameter list Numbers and dates can be intermixed in the parameter list, but dates use the default date mask It’s best to convert dates to strings first with the to_char function SELECT greatest(‘MAZE1’,ename,job,dname,’MAZE2’) AS greatest_str
FROM emp,dept
WHERE emp.deptno=dept.deptno
Trang 2Table 8.63 lists the results.
Table 8.63 Greatest Result
GREATEST_STR
MAZE2
PRESIDENT
MILLER
Initcap
The initcap function capitalizes the first letter and lowercases all other letters
SELECT INITCAP(‘mIXeD CaSE’) AS initcap_str FROM DUAL
Table 8.64 lists the results
Table 8.64 Initcap Results
INITCAP_STR
Mixed case
Instr
The instr function, which can be translated to mean “in string,” searches the first string for the second string and returns the position You can specify where to start searching in the string and which occurrence to find
SELECT instr(‘tripper’,’rip’) AS rip_pos,
instr(‘tripper’,’trip’) AS trip_pos,
instr(‘tripper’,’per’) AS rep_pos,
instr(‘tripper’,’xxx’) AS xxx_pos
FROM dual
Table 8.65 lists the results
Table 8.65 Instr Simple Results
RIP_POS TRIP_POS REP_POS XXX_POS
Trang 3SELECT instr(‘00-11-00-11-00-11’,’00’,2) AS start_1,
instr(‘00-11-00-11-00-11’,’00’,2,2) AS start_2_get_2 FROM dual
Table 8.66 lists the results
Table 8.66 Instr Position and Instance Results
START_1 START_2_GET_2
Instrb
The instrb function provides the same functionality of instr but returns the byte position This is advantageous when multibyte character sets are used It searches the first string for the second string and returns the position You can specify where to start searching in the string and which occurrence to find
SELECT instr(‘tripper’,’rip’) AS rip_pos,
instr(‘tripper’,’trip’) AS trip_pos, instr(‘tripper’,’per’) AS rep_pos, instr(‘tripper’,’xxx’) AS xxx_pos FROM dual
Table 8.67 lists the results
Table 8.67 Instr Simple Results
RIP_POS TRIP_POS REP_POS XXX_POS
SELECT instr(‘00-11-00-11-00-11’,’00’,2) AS start_1,
instr(‘00-11-00-11-00-11’,’00’,2,2) AS start_2_get_2 FROM dual
Table 8.68 lists the results
Table 8.68 Instr Position and Instance Results
START_1 START_2_GET_2
Trang 4The least function returns the alphanumerically least string in its parameter list Numbers and dates can be intermixed in the parameter list, but dates use the default date mask It’s best to convert dates to strings first with the to_char function
SELECT least(‘DOH1’,ename,job,’DOH2’) AS least_str
FROM emp,dept
WHERE emp.deptno=dept.deptno
AND emp.deptno=10
Table 8.69 lists the results
Table 8.69 Least Results
LEAST_STR
CLARK
DOH1
CLERK
Length
The length function returns the number of characters in the string
SELECT dname,length(dname) AS length FROM dept
Table 8.70 lists the results
Table 8.70 Length Results
Trang 5The lengthb function returns the number of bytes in the string This function is advantageous when multibyte character sets are used
SELECT dname,length(dname) AS length FROM dept
Table 8.71 lists the results
Table 8.71 Length Results
Lower
The lower function changes all letters to lowercase
SELECT lower(‘mIXeD CaSE’) AS lower_str FROM DUAL
Table 8.72 lists the results
Table 8.72 Initcap Results
INITCAP_STR
mixed case
Lpad
The lpad function guarantees the length of the string by prefixing characters or trun-cating By default, spaces are the pad character, but this can be specified
SELECT dname,
lpad(dname,8) AS space_pad, lpad(dname,8,’-’) AS dash_pad FROM dept
Table 8.73 lists the results
Trang 6Table 8.73 Lpad Results
Ltrim
The ltrim trims characters from the left side By default, spaces are trimmed, but the strings to trim can be specified
SELECT ltrim(‘ string’) AS trim_str,
ltrim(‘####string’,’#’) AS single_char_trim,
ltrim(‘/**string’,’*/’) AS multi_char_trim
FROM dual
Table 8.74 lists the results
Table 8.74 Ltrim Results
TRIM_STR SINGLE_CHAR_TRIM MULTI_CHAR_TRIM
Nls_initcap
The nls_initcap function capitalizes the first letter and lowercases all other letters, based on the national character set
SELECT nls_initcap(‘mIXeD CaSE’) AS initcap_str FROM DUAL
Table 8.75 lists the results
Table 8.75 Initcap Results
INITCAP_STR
Trang 7The nls_lower function changes all letters to lowercase, based on the national char-acter set
SELECT nls_lower(‘mIXeD CaSE’) AS lower_str FROM DUAL
Table 8.76 lists the results
Table 8.76 Initcap Results
INITCAP_STR
mixed case
Nls_upper
The nls_upper function changes all letters to lowercase
SELECT nls_upper(‘mIXeD CaSE’) AS upper_str FROM DUAL
Table 8.77 lists the results
Table 8.77 Upper Results
UPPER_STR
MIXED CASE
Nls_sort
The nls_sort function returns the byte sequence used to sort a string
Replace
The replace function replaces one substring with another in your string
SELECT replace(‘good or bad’,’or’,’and’) replace_str FROM dual
Trang 8Table 8.78 lists the results.
Table 8.78 Replace Results
REPLACE_STR
good and bad
Rpad
The rpad function guarantees the length of the string by suffixing characters or trun-cating By default, spaces are the pad character, but this character can be specified
SELECT dname,
rpad(dname,8) AS space_pad,
rpad(dname,8,’-’) AS dash_pad
FROM dept
Table 8.79 lists the results
Table 8.79 Rpad Results
Rtrim
The rtrim function trims characters from the right side By default, spaces are trimmed, but the characters to trim can be specified
SELECT ltrim(‘string ‘) AS trim_str,
ltrim(‘string####’,’#’) AS single_char_trim,
ltrim(‘string**/’,’/*’) AS multi_char_trim
FROM dual
Trang 9Table 8.80 lists the results.
Table 8.80 Ltrim Results
TRIM_STR SINGLE_CHAR_TRIM MULTI_CHAR_TRIM
Soundex
Soundexis used to derive a phonetic pronunciation of the input string It is used to help in cases where a string may not be spelled exactly right The first letter is retained,
all vowels h and y are dropped, and the remaining letters are encoded.
SELECT soundex(dname) AS soundex_dname FROM dept
Table 8.81 lists the result
Table 8.81 Soundex Results
SOUNDEX_DNAME
A253
R262
S420
O163
Substr
The substr function returns a substring of the given string The second parameter, if positive, is the character position where your desired substring starts If negative, the position is an offset from the end of the string By default, the string ends at the end of the string If the third parameter is passed, it will specify the length of the string to extract
SELECT substr(dname,4) AS dname_4,
substr(dname,-4) AS dname_neg4, substr(dname,2,3) AS dname_2_3, substr(dname,-3,2) AS dname_neg3_2 FROM dept
Table 8.82 lists the results
Trang 10Table 8.82 Substr Results
DNAME_4 DNAME_NEG4 DNAME_2_3
DNAME_NEG3_2
Substrb
The substrb function returns a substring of the given string where the parameters refer to a byte position It differs from substr only when a multibyte character set is used The second parameter, if positive, is the byte position where your desired sub-string starts If negative, the position is an offset from the end of the sub-string By default, the string ends at the end of the string If the third parameter is passed, it will specify the length of the string in bytes to extract
SELECT substrb(dname,4) AS dname_4,
substrb(dname,-4) AS dname_neg4,
substrb(dname,2,3) AS dname_2_3,
substrb(dname,-3,2) AS dname_neg3_2
FROM dept
Table 8.83 lists the results
Table 8.83 Substrb Results
DNAME_4 DNAME_NEG4 DNAME_2_3 DNAME_NEG3_2
Translate
The translate function changes one set of characters to another in the given string SELECT translate(dname,’AEIOU’,’12345’) AS trans_str
FROM dept
Trang 11Table 8.84 lists the results.
Table 8.84 Translate Results
TRANS_STR
1CC45NT3NG
R2S21RCH
S1L2S
4P2R1T34NS
Trim
The trimfunction trims characters from the left side By default, spaces are trimmed, but the strings to trim can be specified
SELECT trim(‘ string ‘) AS space_trim,
trim(‘*’ FROM ‘****string****’) AS trim_char trim(LEADING ‘*’ FROM ‘****string****’) AS lead_trim, trim(TRAILING ‘*’ FROM ‘****string****’) AS trail_trim FROM dual
Table 8.85 lists the results
Table 8.85 Ltrim Results
SPACE_TRIM LEAD_TRIM TRAIL_TRIM TRIM_CHAR
string string string**** ****string
Upper
The upper function changes all letters to lowercase
SELECT upper(‘mIXeD CaSE’) AS upper_str FROM DUAL
Table 8.86 lists the results
Trang 12Table 8.86 Upper Results
UPPER_STR
MIXED CASE
Date Functions
Date functions allow you to manipulate dates in SQL They are some of the most widely used functions in SQL With these functions you can get the current time, do arithmetic on dates, and convert between time zones The date formats used in this sec-tion were covered in the “Date Format Elements” secsec-tion
Add_months
The add_months function adds months to a particular date The date is kept unless it is too large for the new month In that case, the last date of the last month is used instead SELECT
to_date(‘01-31’,’MM-DD’) AS jan_str,
add_months(to_date(‘01-31’,’MM-DD’),1) AS feb_str,
add_months(to_date(‘01-31’,’MM-DD’),2) AS mar_str,
add_months(to_date(‘01-31’,’MM-DD’),3) AS apr_str
FROM dual
Table 8.87 lists the results
Table 8.87 Add_months Results
JAN_STR FEB_STR MAR_STR APR_STR
31-JAN-02 28-FEB-02 31-MAR-02 30-APR-02
Current_timestamp
The current_timestamp function returns the current timestamp from the table with
a TIMESTAMP WITH A TIMEZONE data type
SELECT to_char(current_timestamp,’YYYY-MM-DD HH24:MM:SS.FF’) AS date_str
FROM dual
Trang 13Table 8.88 lists the results.
Table 8.88 Sysdate Results
DATE_STR
2002-05-19 01:05:00.000001
Dbtimezone
The dbtimezone function returns the timezone of the database:
SELECT dbtimezone AS tz FROM dual
Table 8.89 lists the results
Table 8.89 Dbtimezone Results
TZ
-5:00
Extract
The extract function extracts the specified part (year, month, day, hour, minute, second, timezone_hour, timezone_minute, timezone_region, and timezone_abbr) from the date
SELECT hiredate, extract(YEAR FROM hiredate) AS year
FROM emp
WHERE deptno=10
Table 8.90 lists the results
Table 8.90 Extract Results
Trang 14The greatest function returns the greatest number in its parameter list Numbers and dates can be intermixed in the parameter list, but dates use the default date mask It’s best to convert dates to strings first with the to_char function
SELECT
greatest(hiredate,to_date(‘16-NOV-1981’,’DD-MON-YYYY’)) AS
greatest_date
FROM emp WHERE deptno=10;
Table 8.91 lists the results
Table 8.91 Greatest Results
GREATEST_DATE
16-NOV-81
17-NOV-81
23-JAN-82
Last_day
The last_day function returns the last day of the month in which the specified date falls
SELECT hiredate,last_day(hiredate) AS last
FROM emp
WHERE deptno=10
Table 8.92 lists the results
Table 8.92 Last_day Results
Trang 15The greatest function returns the least number in its parameter list Numbers and dates can be intermixed in the parameter list, but dates use the default date mask It’s best to convert dates to strings first with the to_char function
SELECT
least(hiredate,to_date(‘16-NOV-1981’,’DD-MON-YYYY’)) AS least_date FROM emp WHERE deptno=10;
Table 8.93 lists the results
Table 8.93 Least Results
LEAST_DATE
09-JUN-81
16-NOV-81
16-NOV-81
Local_timestamp
The local_timestamp function returns the timestamp in the local timezone as a timestamp data type This function is sometimes preferable to current_timestamp, because the latter returns the timestamp with the timezonedata type
SELECT to_char(localtimestamp,’YYYY-MM-DD HH24:MI:SSXFF’) AS time
FROM dual
Table 8.94 lists the results
Table 8.94 Local_timestamp Results
TIME
2002-05-19 01:34:21.000001
Months_between
The months_between function returns the number of months between two dates Integers are only returned if both dates have the same day of the month or if both dates fall on the last day of their respective months A negative number is returned if the first argument precedes the second argument You can use the abs function to always get a positive number
Trang 16SELECT months_between(to_date(‘01-16’,’MM-DD’),to_date(‘08.16’,’MM-DD’))
AS btwn_1,
months_between(to_date(‘08.16’,’MM-DD’),to_date(‘01-16’,’MM-DD’))
AS btwn_2,
months_between(to_date(‘01-16’,’MM-DD’),to_date(‘08.31’,’MM-DD’))
AS btwn_3,
months_between(to_date(‘02-28’,’MM-DD’),to_date(‘08.31’,’MM-DD’))
AS btwn_4
FROM dual;
Table 8.95 lists the results
Table 8.95 Months_between Results
New_time
The new_time function converts a time from one timezone to another
SELECT to_char(
new_time(to_date(‘08:00’,’HH24:MI’),’EST’,’GMT’),
‘HH24:MI’) AS new_time_str
FROM dual
Table 8.96 lists the results
Table 8.96 New_time Results
NEW_TIME_STR
13:00
Next_day
The next_day function returns the date after a given date that falls on the given day
of the week If the given date falls on the given day of the week, the date one week hence is returned This can be fixed by always subtracting 1 from the date
SELECT ename,
deptno,
to_char(hiredate,’DY, MON-DD’) AS hire_date,
next_day(hiredate,’Fri’) AS next_fri_1,
Trang 17FROM emp
WHERE deptno=30
Table 8.97 lists the results
Table 8.97 Next_day Results
HIRE_DATE NEXT_FRI_1 NEXT_FRI_2
ALLEN FRI, FEB-20 27-FEB-81 20-FEB-81
WARD SUN, FEB-22 27-FEB-81 27-FEB-81
MARTIN MON, SEP-28 02-OCT-81 02-OCT-81
BLAKE FRI, MAY-01 08-MAY-81 01-MAY-81
TURNER TUE, SEP-08 11-SEP-81 11-SEP-81
JAMES THU, DEC-03 04-DEC-81 04-DEC-81
Numtodsinterval
The numtodsinterval function converts the parameter to an interval day to second interval The string argument specifies the unit and can be one of SECOND, MINUTE, HOUR, or DAY
SELECT hiredate,
hiredate+numtodsinterval(100,’day’) AS days, hiredate+numtodsinterval(1000,’hour’) AS hours, hiredate+numtodsinterval(10000,’minute’) AS minutes, hiredate+numtodsinterval(100000,’second’) AS seconds FROM emp
WHERE deptno=10
Table 8.98 lists the results
Table 8.98 Numtodsinterval Results
09-JUN-81 17-SEP-81 20-JUL-81 15-JUN-81 10-JUN-81
17-NOV-81 25-FEB-82 28-DEC-81 23-NOV-81 18-NOV-81
23-JAN-82 03-MAY-82 5-MAR-82 29-JAN-82 24-JAN-82
Trang 18The numtoyminterval function converts the parameter and interval year to month interval The string argument specifies the unit and can be either MONTH or YEAR
SELECT hiredate,
hiredate+numtoyminterval(1,’year’) AS years,
hiredate+numtoyminterval(10,’month’) AS months
FROM emp
WHERE deptno=10
Table 8.99 lists the results
Table 8.99 Numtoyminterval Results
09-JUN-81 09-JUN-82 09-APR-82
17-NOV-81 17-NOV-82 17-SEP-82
23-JAN-82 23-JAN-83 23-NOV-82
Round
The round function rounds a datetime value to the nearest unit specified by the for-mat By default, the date is rounded to the precision of the default forfor-mat
SELECT to_char(round(sysdate,’HH’),’YYYY-MM-DD HH24:MI’) AS hour,
to_char(round(sysdate,’MM’), ‘YYYY-MM-DD HH24:MI’) AS month,
to_char(round(sysdate,’YYYY’), ‘YYYY-MM-DD HH24:MI’) AS year,
to_char(round(sysdate,’CC’), ‘YYYY-MM-DD HH24:MI’) AS century
FROM dual
Table 8.100 lists the results
Table 8.100 Round Results
2002-05-18 2002-06-01 2002-01-01 2001-01-01