Cast, Convert and Parse Flashcards

1
Q

What are some functions that can be used to apply explicit conversion?

A
  1. CAST
  2. CONVERT
  3. PARSE
  4. TRY_CAST
  5. TRY_CONVERT
  6. TRY_PARSE

SQL Server 70-461 02-02a

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

What is the difference between the TRY and non TRY versions of the CAST, CONVERT and PARSE functions?

A

Those without the TRY fail if the value isn’t convertible, whereas those with the TRY return a NULL in such a case.

Forexample, the following code fails.
SELECT CAST(‘abc’ AS INT);

Conversely, the following code returns a NULL.
SELECT TRY_CAST(‘abc’ AS INT);

SQL Server 70-461 02-02a

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

What is the proper syntax for the CAST function?

A

CAST( expression AS datatype [length] )

SQL Server 70-461 02-02a

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

What is the proper syntax for the CONVERT function?

A

CONVERT ( data_type [length], expression [, style ] )

SQL Server 70-461 02-02a

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

What is the proper syntax for the PARSE function?

A

PARSE (string_value AS data_type [ USING culture ] )

SQL Server 70-461 02-02a

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

What is the difference between CAST, CONVERT and PARSE functions?

A

With CAST you indicate the expression and a target data type. With CONVERT there is a third optional Style argument. With PARSE there is a third optional Culture argument.

CAST( expression AS datatype [length] )
CONVERT(DATE, ‘1/2/2012’, 101)
PARSE(‘1/2/2012’ AS DATE USING ‘en–US’)

SQL Server 70-461 02-02a

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

What does the optional Style argument in CONVERT represent?

A

The format used to convert between data types, such as a date format or string format.

You can:
- Convert datetime to character
- Convert float to real
- Convert money to character

Example of Style converting money to character:
Style of 0: No comma delimiters, 2 digits to the right of decimal (ie: 1234.56)

Style of 1: Comma delimiters, 2 digits to the right of decimal (ie: 1,234.56)

SQL Server 70-461 02-02a

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

What does the optional Culture argument in PARSE represent?

A

Which culture to use that is supported by the .NET framework.

Ex.
en–US (English)
fr_FR (French)
ja_JP (Japanese)

SQL Server 70-461 02-02a

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