Skill 1.3 Implement functions and aggregate data Flashcards

(47 cards)

1
Q

What is the output of a ‘TRY_XXX’ conversion function when conversion fails?

A

NULL

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

What does the function GETDATE do?

A

Returns the current date and time in the SQL instance you are querying

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

What is the SQL standard equivalent of GETDATE?

A

CURRENT_TIMESTAMP

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

What is the difference between GETDATE and SYSDATETIME?

A

SYSDATETIME returns a DATETIME2 value instead of a DATETIME value

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

How are DATETIME and DATETIME2 values different?

A

DATETIME2 values have an accuracy of 100ns, DATETIME values are Rounded to increments of .000, .003, or .007 seconds

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

What is the output of the SYSDATETIMEOFFSET function?

A

SYSDATETIMEOFFSET returns a DATETIMEOFFSET value, which is a DATETIME2 date value plus the offset in hours from UTC

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

How do you get the date or time in T-SQL?

A

Cast the DATETIME, DATETIME2, or DATETIMEOFFSET value as DATE or TIME: CAST(GETDATE() as DATE), CAST(SYSDATETIME() as TIME)

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

What do the GETUTCDATE and SYSUTCDATETIME functions do?

A

The GETUTCDATE and SYSUTCDATETIME functions return DATETIME and DATETIME2 values converted to UTC time

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

What do you use the DATEPART function for?

A

The DATEPART function is used to extract part of a date (year, month, day, hour, minute, nanosecond) as an integer

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

What does the DATENAME function do?

A

DATENAME returns a character string that represents the specified datepart of the specified date

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

What functions does SQL provide for constructing DATETIME values from parts?

A
DATEFROMPARTS
DATETIME2FROMPARTS
DATETIMEFROMPARTS
DATETIMEOFFSETFROMPARTS
SMALLDATETIMEFROMPARTS
TIMEFROMPARTS
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What function gets you the end of month date for the given date and time value?

A

EOMONTH (It supports a second argument indicating how many months to add to the result)

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

What is the DATEADD function used for?

A

DATEADD takes a date, number of units, and a date part, and gives you a second date with that number of units of that part added. Negative units allows for subtraction.

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

What does the DATEDIFF function do?

A

DATEDIFF gives you the difference in units between two dates, expressed as an integer. Use DATEDIFF_BIG, if the difference doesn’t fit in a 4-byte integer.

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

What does SWITCHOFFSET do?

A

SWITCHOFFSET returns an input DATETIMEOFFSET value adjusted to a requested target offset. You can use it to convert a datetime in the one timezone to a datetime in another timezone

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

How is TODATETIMEOFFSET different from SWITCHOFFSET?

A

TODATETIMEOFFSET accepts a DATETIME value (not a DATETIMEOFFSET value) as input. You can use it to convert a datetime in the server’s current timezone into a datetime in another timezone

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

How is the function AT TIME ZONE different from SWITCHOFFSET?

A

AT TIME ZONE expects a time zone name (‘Pacific Standard Time’) instead of an offset value (‘-07:00’)

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

What two methods of concatenating strings does SQL Server support?

A

the plus (+) operator and the CONCAT function

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

What is the output of a string concatenated using the plus operator when any one of the parts is NULL?

20
Q

What does CONCAT replace NULLs with?

A

an empty string

21
Q

What are the three arguments to the substring function?

A

string, starting position, length

22
Q

What are the two arguments to the LEFT and RIGHT functions

A

string, length

23
Q

What does the CHARINDEX function do?

A

CHARINDEX returns the position of the first occurrence of a given string within another string, beginning at a certain position

24
Q

How is PATINDEX different from CHARINDEX?

A

PATINDEX looks for a pattern (like the LIKE clause) in a string

25
What are the two functions you can use to measure the length of a string?
LEN and DATALENGTH
26
What is the difference between LEN and DATALENGTH?
LEN returns the number of characters in a string, minus any trailing spaces (NOT leading spaces), DATALENGTH returns the number of bytes in the input string (2 bytes per character for unicode strings)
27
What does the REPLACE function do?
REPLACE takes an input string, and replaces all occurrences of one string with another string
28
What does the REPLICATE function do?
REPLICATE takes an input string and replicates it a requested number of times
29
What does the STUFF function do?
STUFF replaces a given number of characters at a given position in a string with another string
30
What does the format FUNCTION do?
FORMAT lets you format an input value based on a .NET format string
31
What does the STRING_SPLIT function do?
STRING_SPLIT takes an input string and a string to split it by and returns a list of strings
32
What is the general format of the simple form of the CASE function?
``` case [columname] when some_value then output_value when some_value2 then output_value2 ... else some_default value END as alias_name ```
33
What is the general format of the searched form of the CASE function?
case when [columname] = some_value then output_value when [columname] = some_value2 then output_value2 ... else some_default value END as alias_name
34
What functions are considered abbreviates of the CASE function?
COALESCE, NULLIF (standard) | ISNULL, IIF, CHOOSE (nonstandard)
35
What does the COALESCE function do?
COALESCE returns the first non-NULL value from a list of values
36
What does the NULLIF function do?
NULLIF compares two input expressions and returns NULL if they are equal and the first input if they are not
37
What does the IIF function do?
IIF returns one value if a predicate is true, and another value if a predicate is false or unknown
38
What does the CHOOSE function do?
CHOOSE lets you give a position and a list of expressions, and returns the expression in the given position (NULL if there is no expression in that position)
39
What do the @@ROWCOUNT and ROWCOUNT_BIG functions do?
@@ROWCOUNT and ROWCOUNT_BIG return the number of rows affected (inserted, updated, deleted) by the last SQL statement you executed
40
What compression algorithm does the COMPRESS function use?
GZIP
41
How do you turn a COMPRESSed string back into a string you can read?
CAST the string as the original type that was compressed (nvarchar(max), etc)
42
T-SQL triggers are niladic. What does niladic mean?
niladic means that they don't support parameters
43
What is CONTEXT_INFO?
CONTEXT_INFO is a session-scoped binary string that can hold up to 128 bytes
44
How do you set the value of CONTEXT_INFO
you use the command | SET CONTEXT-INFO [varbinary(128) value]
45
How is SESSION_CONTEXT different from CONTEXT_INFO?
SESSION_CONTEXT stores a list of key-value pairs where the key is nvarchar(128) and the value is a SQL_VARIANT
46
How do you add a value to SESSION_CONTEXT?
EXEC sys.sp_set_session_context @key = N'keyname', @value = 'value', @read_only = [0 or 1]
47
How do you read a value from SESSION_CONTEXT?
SELECT SESSION_CONTEXT(N'keyname') as [alias]