Functions Flashcards

1
Q

What is a function?

A

A function is an SQL Server object with a specified name and optional parameters that operates as a single logical unit.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What do you need to include to call a function?

A

Functions are called by providing the name of the function along with a set of parenthesis.

Example: function (arg1, arg2, arg3)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

SELECT DATE_FORMAT(“2017-06-15”, “%Y”)

In the query above, what does the “DATE_FORMAT” represent?

A

The function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

SELECT DATE_FORMAT(“2017-06-15”, “%Y”)

In the query above, what are the values inside the parenthesis called?

A

Arguments

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

DATE

A

The DATE() function extracts the date part from a datetime expression.

SELECT DATE(“2017-06-15”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

TIME

A

The TIME() function extracts the time part from a given time/datetime.

SELECT TIME(“19:30:10”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

DATETIME

A

The DATETIME type is used for values that contain both date and time parts.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

TIMESTAMP

A

The TIMESTAMP data type is used for values that contain both date and time parts. 

SELECT TIMESTAMP(“2017-07-23”, “13:10:11”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

YEAR

A

The YEAR() function returns the year part for a given date (a number from 1000 to 9999).

SELECT YEAR(“2017-06-15”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the format used for date values?

A

YYYY-MM-DD

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the format used for time values?

A

hh:mm:ss

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

SELECT DISTINCT

A

The SELECT DISTINCT statement is used to return only distinct (different) values. It will return only one value for each distinct result.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

COUNT (Aggregate Function)

A

Counts how many rows are in a particular column.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

SUM (Aggregate Function)

A

Adds together all the values in a particular column.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

MIN (Aggregate Function)

A

Returns the lowest value in a particular column.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

MAX (Aggregate Function)

A

Returns the highest value in a particular column.

17
Q

AVG (Aggregate Function)

A

Calculates the average of a group of selected values.

18
Q

What are the 5 Aggregate Functions?

A

Count, Sum, Min, Max, Avg

19
Q

What is a string?

A

A string is a collection of characters that you cannot use in arithmetic calculation. The characters you store in these data types can be uppercase or lowercase letters, numerals, and special characters such as the “at” sign (@), ampersand (&), and exclamation point (!) in any combination. It could be a name, an email address, a telephone number.

20
Q

What case conversion function can you use to convert text to uppercase?

A

UPPER, UCASE

21
Q

What case conversion function can you use to convert text to uppercase?

A

LOWER, LCASE

22
Q

REPLACE

A

Is used to replace a string or part of a string with another string.

Example:
You want to change ‘USA’ for ‘United States’ in the country column of a database.

SELECT REPLACE( country, ‘USA’, ‘United States’ )
FROM customers

23
Q

FORMAT

A

Used to format date/time values and number values.

FORMAT(value, format)

For number values - FORMAT(value, # of decimals)
Example: FORMAT(2034.567, 2)

For dates - FORMAT(value, date format preferred)
Example: FORMAT(getdate(), ‘dd-MM-yy’)

24
Q

ROUND

A

Rounds a number to a specified number of decimal places.

ROUND(value, # of decimals)

Example:
ROUND(2034.567, 2)

It will return 2034.57, the number rounded to only 2 decimals.

25
Q

TRIM

A

Removes the space character OR other specified characters from the start or end of a string.

26
Q

SUBSTR

A

Extracts a substring from a string (starting at any position)

SUBSTR(string, start, length)

27
Q

What is a leading space?

A

A space in front of the value input in the field

28
Q

What is a trailing space?

A

A space left after the value input in the field

29
Q

What function should you use to remove a leading space from a value?

A

ltrim

30
Q

What function should you use to remove a trailing space from a value?

A

rtrim

31
Q

CONCAT

A

Adds two or more strings together.

CONCAT(string1, string2, …., string_n) [Within the paranthesis you include all the string you want to join together]

32
Q

CONCAT_WS

A

The CONCAT_WS() function adds two or more strings together with a separator.

CONCAT_WS(separator, string1, string2, …., string_n)

Here, the first argument is whatever you want to use as a separator between each string, followed by all the strings you want to join together.