Cast, Convert and Parse Flashcards
What are some functions that can be used to apply explicit conversion?
- CAST
- CONVERT
- PARSE
- TRY_CAST
- TRY_CONVERT
- TRY_PARSE
SQL Server 70-461 02-02a
What is the difference between the TRY and non TRY versions of the CAST, CONVERT and PARSE functions?
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
What is the proper syntax for the CAST function?
CAST( expression AS datatype [length] )
SQL Server 70-461 02-02a
What is the proper syntax for the CONVERT function?
CONVERT ( data_type [length], expression [, style ] )
SQL Server 70-461 02-02a
What is the proper syntax for the PARSE function?
PARSE (string_value AS data_type [ USING culture ] )
SQL Server 70-461 02-02a
What is the difference between CAST, CONVERT and PARSE functions?
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
What does the optional Style argument in CONVERT represent?
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
What does the optional Culture argument in PARSE represent?
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