5. Using Single-Row Functions to Customize Output

0.0(0)
studied byStudied by 0 people
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/109

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

110 Terms

1
New cards

Functions: What are the three characteristics of SQL functions?

May take parameters, perform processing with the parameter data, always return one single answer.

2
New cards

Which types of functions are in SQL?

built-in and user-defined

3
New cards

What are built-in functions in SQL?

Are part of the SQL language. They are in every standard implementation of SQL.

4
New cards

Functions: Where can built‑in functions be invoked?

Anywhere an expression is allowed: SELECT list and WHERE, INSERT VALUES, UPDATE SET/WHERE, DELETE WHERE, and more.

5
New cards

What kind of built-in functions are there? Categories.

Character, Number and Date functions.

6
New cards

What does DATE subtraction return?

It returns difference, number of days

7
New cards

DUAL: What is the DUAL table in Oracle?

A one-row, one-column table (DUMMY VARCHAR2(1) with value 'X') used to select expressions or function results.

8
New cards

DUAL: Why select from DUAL?

To evaluate functions or expressions without referencing application tables, e.g., SELECT SYSDATE FROM DUAL. It returns only one row because the dual table has one row.

9
New cards

UPPER: What does UPPER(s1) do?

Converts s1 to uppercase; returns a character string.

s1 is a required character string

10
New cards

UPPER: When searching case-insensitively, how should UPPER be applied?

Standardize both sides: WHERE UPPER(col) = UPPER(:bind) to avoid case mismatch.

11
New cards

LOWER: What does LOWER(s1) do?

Converts s1 to lowercase; returns a character string.

s1 is a required character string

12
New cards

LOWER: Give a safe equality predicate using LOWER for mixed case columns.

WHERE LOWER(last_name) = LOWER(family_name).

13
New cards

INITCAP: What does INITCAP(s1) do?

Capitalizes the first letter of each word and lowercases the rest; returns a character string.

s1 is a required character string

14
New cards

INITCAP: Why can INITCAP produce undesirable results for names like 'McDonald’s'?

It treats punctuation as word separators and lowercases internal letters, e.g., 'McDonald''s' -> 'Mcdonald''S'.

15
New cards

String quotes: How do you include a single quote character in a string literal?

Escape by doubling it: 'O''Brien' or 'McDonald''s'.

16
New cards

String quotes: Why is escaping needed for names like O’Hearn?

Because single quote is the string delimiter; doubling it tells SQL to treat it as data, not a terminator.

17
New cards

CONCAT / ||: What does CONCAT(s1,s2) do and what is its limitation?

Concatenates only two strings into one string; accepts exactly two parameters.

s1 and s2 are both character strings and both are required

18
New cards

CONCAT / ||: How to concatenate more than two strings?

Use the || operator repeatedly, e.g., s1 || s2 || s3.

the result is a single string

19
New cards

LPAD/RPAD: What do LPAD(s1,n,s2) and RPAD(s1,n,s2) do?

Pad s1 on the left/right with s2 so the result length is n.

s1 - character string - required

n - number - required

s2 - character string - optional, defaults to a single blank space if omitted

20
New cards

LPAD/RPAD: Give a formatting example with LPAD/RPAD.

LPAD(‘Ahoj’, 10, ‘.’) => ……Ahoj

21
New cards

LTRIM/RTRIM: What do LTRIM(s1,s2) and RTRIM(s1,s2) do?

Remove occurrences of characters in s2 from the left or right of s1.

s1, s2 - both are character strings

s1 - required

s2 - optional, if omitted, the default is a single blank space

22
New cards

LTRIM/RTRIM: How to strip leading dashes from '---abc'?

SELECT LTRIM('---abc','-') FROM DUAL -> 'abc'.

23
New cards

TRIM: What is the syntax and defaults for TRIM?

TRIM(trim_info trim_char FROM trim_source);

  • trim_info - one of [LEADING|TRAILING|BOTH], defaults to BOTH if omitted

  • trim_char - single char to be trimmed - defaults to blank space if omitted

  • trim_source - string, defaults to return NULL if omitted

  • if either trim_source or trim_character is null then result is null

  • returns varchar2

Works:

  • TRIM(DATE ‘2025-02-02’) → 02-FEB-25

  • TRIM(2) → 2

  • TRIM(1 FROM 123) → 23

  • TRIM(null) → null

  • TRIM(‘ ‘) → null

  • TRIM(‘0’ FROM DATE’2025-02-02’) → 2-FEB-25

Doesn’t work:

  • TRIM(FROM)

  • TRIM(FROM ‘abc’)

  • TRIM(‘abe’ FROM ‘abc’)

  • TRIM(‘a’ FROM)

24
New cards

TRIM: How to remove trailing dashes from 'Seven thousand--------'?

SELECT TRIM(TRAILING '-' FROM 'Seven thousand--------') FROM DUAL -> 'Seven thousand'.

25
New cards

LENGTH: What does LENGTH(s) return and what is the type?

Returns the length of s as a number.

s - string, required

26
New cards

LENGTH: Example of LENGTH with a long word.

SELECT LENGTH('antidisestablishmentarianism') FROM DUAL -> 28

27
New cards

INSTR: What does INSTR(s1, s2, pos, n) do?

Returns the position of the nth occurrence of s2 in s1 starting at pos

  • s1 - source string, required

  • s2 - substring, required

  • pos - starting position in s1, optional, default 1, if negative the search in s1 for occurrences of s2 starts at the end of the string and moves backward

  • n - nth occurrence of s2 to locate, optional, default 1

28
New cards

What’s the result of this query?

select INSTR('Hellow World, Bordie', 'or', -13, 1)

from dual;

0

29
New cards

What’s the result of this query?

select INSTR('Hellow World, Bordie', 'or', -12, 1)

from dual;

9

30
New cards

What’s the result of this query?

select INSTR('Hellow World, Bordie', 'or', -5, 1)

from dual;

16

31
New cards

SUBSTR: What does SUBSTR(s, pos, len) do?

Returns substring of s starting at pos for len chars; if len omitted, to end; negative pos counts from end.

  • s - string, required

  • pos - number, required

  • len - number, optional

32
New cards

SUBSTR: Extract the name from 'Name: MARK KENNEDY' starting at 7.

SELECT SUBSTR('Name: MARK KENNEDY',7) FROM DUAL -> 'MARK KENNEDY'.

33
New cards

What does this query return?

select SUBSTR('Hellow World, Bordie', -2, 2)

from dual;

ie

34
New cards

SOUNDEX: What does SOUNDEX(s) return and why use it?

A 4-character phonetic code to match similar-sounding words. It translates English word (a source string) into its SOUNDEX code. Useful for sounding alike sounds tend to generate the same soundex pattern.

  • s - source string, required

35
New cards

How does SOUNDEX work?

  • First letter remains same

  • Next letters are translated into a numeric code according to the rules until three digits are generated. If any letters exist beyond that, they are ignored. The result is pad with 0 until it’s a 4 char code.

36
New cards

Which letters get translated into soundex code 1?

B, F, P, V

37
New cards

Which letters get translated into soundex code 2?

C, G, J, K, Q, S, X, Z

38
New cards

Which letters get translated into soundex code 3?

D, T

39
New cards

Which letters get translated into soundex code 4?

L

40
New cards

Which letters get translated into soundex code 5?

M, N

41
New cards

Which letters get translated into soundex code 6?

R

42
New cards

Which letters are ignored when translating them into soundex code?

A, E, H, I, O, U, W, Y

43
New cards

What is the soundex code for “dog”?

D200

44
New cards

SOUNDEX: Why apply SOUNDEX on both sides of a comparison?

To compare codes, e.g., WHERE SOUNDEX(lastname)=SOUNDEX('Franklin'); using it on one side compares text to a code.

45
New cards

What is the SOUNDEX code for: “0“, null, ‘-1’, ‘‘, ‘-’

null

46
New cards

What is the SOUNDEX code for: DATE’2025-01-01’

J500 - because it gets converted to 2025-JAN-01, so JAN is translated to J500

47
New cards

ABS: What does ABS(n) do?

Returns the absolute value of n.

n - is required, numeric

48
New cards

CEIL: What does CEIL(n) return?

Smallest integer ≥ n.

  • n is required, numeric

49
New cards

CEIL: Example: CEIL(10.01), CEIL(-2.1), CEIL(-2.9).

11, -2, and -2.

50
New cards

FLOOR: What does FLOOR(n) return?

Largest integer ≤ n.

n - numeric, required

51
New cards

FLOOR: Example: FLOOR(10.99), FLOOR(-2.1), FLOOR(-2.9).

10,-3, and -3.

52
New cards

ROUND (number): What is the behavior of ROUND(n,i)?

Rounds n to i decimal places (i optional, default 0); i<0 rounds to left of decimal; .5 rounds away from zero.

  • n - required, numeric

  • i - integer, optional, if omitted then 0

53
New cards

ROUND (number): Example: ROUND(12.355143,2) and ROUND(259.99,-1), ROUND(9.23, -1), ROUND(9.23, 0), ROUND(9.23, -2)

12.36, 260, 10, 9, 0

54
New cards

TRUNC (number): What is the behavior of TRUNC(n,i)?

Truncates n toward zero at i decimal places (i optional, default 0).

  • n - required, number, can be decimal

  • i - integer, optional, if omitted then 0

55
New cards

TRUNC (number): Example: TRUNC(12.355143,2) and TRUNC(259.99,-1), TRUNC(815623.23, -5), TRUNC(815623.23, 0)

12.35, 250, 800000, 815623

56
New cards

REMAINDER: What does REMAINDER(n1, n2) compute?

Returns n1 minus the nearest multiple of n2; result can be negative if the nearest multiple is above n1.

  • n1, n2 - both numeric and required

57
New cards

REMAINDER: Example: REMAINDER(9,3), REMAINDER(10,3), REMAINDER(11,3), REMAINDER(5, -2), REMAINDER(5, -5), REMAINDER(-5, -2), REMAINDER(-5, 2)

0, 1, -1, 1, 0, -1, -1

58
New cards

MOD: what’s the syntax of MOD function?

MOD(n1,n2): n1, n2 are both numeric and required

Returns n1 minus the nearest multiple of n2 but that the difference is a plus number

59
New cards

MOD: How does MOD(n1,n2) differ from REMAINDER?

MOD uses FLOOR instead of ROUND; returns a nonnegative remainder for positive n2.

60
New cards

MOD: Example comparison for (5,3).

MOD(5,3)=2; REMAINDER(5,3)=-1.

61
New cards

SIGN: What does SIGN(n) return?

n is numeric and required

1 if n>0, 0 if n=0, -1 if n<0.

62
New cards

SIGN: Use SIGN to test positivity without CASE.

WHERE SIGN(n)=1 filters positive values.

63
New cards

SQRT: What does SQRT(n) return?

n is numeric and required. The square root of n.

64
New cards

SQRT: Give a simple example of SQRT.

SELECT SQRT(25) FROM DUAL -> 5.

65
New cards

Define function POWER

POWER(n2, n1) - n2 raised to the n1 power.

n1 and n2 are both numeric and required.

if n2 is negative then n1 must be integer

66
New cards

What does function USER do?

Returns the session user (schema)

67
New cards

What does function UID do?

Returns a unique integer that identifies the session user

68
New cards

What does CURRENT_DATE function do?

Returns the current date in the session time zone. Date data type.

69
New cards

What does CURRENT_TIMESTAMP function do?

Returns the current date and time in the session time zone. Timestamp with time zone data type.

70
New cards

What does DBTIMEZONE function do?

Returns the value of the database time zone. Return type is a time zone offset. Example result: +00:00 (but this is based on the database settings of specifying the db time zone display settings)

71
New cards

What does EXTRACT function do?

Extracts the returns the value of a specified datetime field from a datetime or interval expression.

EXTRACT(datetime_field FROM expr)

  • datetime_field is required and one of these: YEAR|MONTH|DAY|HOUR|MINUTE|SECOND|TIMEZONE_HOUR|TIMEZONE_MINUTE|TIMEZONE_REGION|TIMEZONE_ABBR

  • FROM and expr is also required

expr must be:

  • for YEAR or MONTH => DATE, TIMESTAMP, TS WITH TZ, TS WITH LOCAL TZ or INTERVAL YEAR TO MONTH

  • for DAY => DATE, TIMESTAMP, TS W TZ, TS W L TZ, INTERVAL DAY TO SECOND

  • for HOUR, MINUTE, or SECOND, then expr must be evaluate to timestamp, TS with TZ, TS with local TZ, or interval day to second. Date is not valid here.

  • for TIMEZONE_HOUR, TIMEZONE_MINUTE, TIMEZONE_ABBR, TIMEZONE_REGION, or TIMEZONE_OFFSET then expr must evaluate to TS WITH TZ or TS WITH LOCAL TZ

72
New cards

SYSDATE: What does SYSDATE return and where is it sourced?

Current date and time from the database server OS; time exists but may not display per NLS_DATE_FORMAT.

No parameters.

73
New cards

SYSDATE: How to display the time portion of SYSDATE?

Use TO_CHAR with a format model, e.g., TO_CHAR(SYSDATE,'YYYY-MM-DD HH24:MI:SS').

74
New cards

ROUND (d, i): What does do for dates?

Rounds d to the nearest unit per i; default rounds to nearest whole day; 12:00 rounds up to next day.

d - required date

i - format model, optional

75
New cards

ROUND (date, i): Example showing bias at noon.

ROUND(TO_DATE('2020-08-01 12:00','YYYY-MM-DD HH24:MI'), 'DD') -> next day (2020‑08‑02).

76
New cards

TRUNC (date, fmt): What does it do for dates?

Always rounds down to the boundary of fmt; default is day (time set to 00:00).

  • d is a date, required

  • fmt is a format model, optional

77
New cards

TRUNC (date, i): Example of month truncation.

TRUNC(DATE '2020-08-18','MM') -> 2020‑08‑01 00:00.

78
New cards

NEXT_DAY: What does NEXT_DAY(d,c) return?

a date of the first occurrence of weekday c strictly after date d; c is a text day name per session NLS settings.

  • d is date, required

  • c is a text reference to a day of the week, required

79
New cards

NEXT_DAY: Example: next Saturday after 2019-05-31.

NEXT_DAY(DATE '2019-05-31','SATURDAY') -> 2019‑06‑01.

80
New cards

LAST_DAY: What does LAST_DAY(d) return?

The last day of the month that contains d.

  • d is required, date

81
New cards

LAST_DAY: Example for leap and common years.

LAST_DAY(DATE '2020-02-10')=2020‑02‑29; LAST_DAY(DATE '2021-02-10')=2021‑02‑28.

82
New cards

ADD_MONTHS: What does ADD_MONTHS(d,n) do?

Returns a date that adds n months to d (n can be negative to subtract).

  • d is a date, required

  • n is a whole number, required

83
New cards

ADD_MONTHS: Example with end-of-month behavior.

ADD_MONTHS(DATE '2017-01-31',1) -> 2017‑02‑28 (adjusts to valid date).

84
New cards

MONTHS_BETWEEN: What does MONTHS_BETWEEN(d1,d2) return?

Number of months between d1 and d2, possibly fractional; positive if d1>d2 else negative.

  • d1 and d2 are dates, required

85
New cards

MONTHS_BETWEEN: Example with sign and whole months.

MONTHS_BETWEEN(DATE '2014-06-12', DATE '2013-10-03') -> positive value; reversing args yields negative.

86
New cards

NUMTOYMINTERVAL: What does NUMTOYMINTERVAL(n,interval_unit) return?

date type of INTERVAL YEAR TO MONTH representing n 'YEAR' or 'MONTH'.

  • n is number, required

  • interval_unit is YEAR or MONTH

87
New cards

NUMTOYMINTERVAL: Example of 27 months.

NUMTOYMINTERVAL(27,'MONTH') -> +02-03 → (2 years, 3 months).

88
New cards

NUMTODSINTERVAL: What does NUMTODSINTERVAL(n,interval_unit) return?

A value of the data type INTERVAL DAY TO SECOND representing n 'DAY'|'HOUR'|'MINUTE'|'SECOND'.

  • n is number, required

  • interval_unit = one of DAY, HOUR, MINUTE, or SECOND

89
New cards

NUMTODSINTERVAL: Example of 36 hours.

NUMTODSINTERVAL(36,'HOUR') -> +01 12:00:00.000000.

90
New cards

DATE arithmetic: What numeric literal equals one day in DATE arithmetic?

1 equals one day; 1/24 equals one hour; 1/1440 equals one minute.

91
New cards

DATE arithmetic: Add minutes to a DATE without functions. How to add one day to SYSDATE?

SYSDATE + (12/1440) → adds 12 minutes or SYSTIMESTAMP + (12/1440) → adds 12 minutes

SYSDATE + 1 → adds 1 day, or SYSTIMESTAMP + 1 → adds 1 day

92
New cards

What can analytical functions do?

Analytical functions in Oracle SQL compute values across a set of rows that are related to the current row, without collapsing the rows into a single result as aggregate functions do. They operate over a "window" of rows defined by a clause (like OVER with PARTITION BY and ORDER BY), and return a value for each row in that window. This allows row-by-row analysis with context from surrounding rows, such as rankings, running totals, or moving averages

93
New cards

Analytic placement: Where can analytic functions appear in a query?

SELECT list or ORDER BY only; not allowed in WHERE, GROUP BY, or HAVING.

94
New cards

Analytic order: When are analytic functions evaluated?

As the last step before ORDER BY of the query.

95
New cards

OVER: What does the OVER clause do?

Defines the analytic window and ordering for a function to operate over rows.

96
New cards

OVER: How does OVER enable running totals?

SUM(col) OVER(ORDER BY key ROWS UNBOUNDED PRECEDING) computes a cumulative total per order.

97
New cards

PARTITION BY: What does PARTITION BY specify in analytics?

Divides rows into groups; analytics reset per partition.

98
New cards

PARTITION BY: Effect of PARTITION BY on running totals?

Running totals restart at the first row of each partition.

99
New cards

ORDER BY in OVER: Is ORDER BY in OVER tied to the query ORDER BY?

No; it defines analytic computation order independent of the SELECT's ORDER BY.

100
New cards

ORDER BY in OVER: What happens if SELECT ORDER BY changes but OVER stays the same?

Computed analytic values per row remain the same; only output order changes.