I am going press on with my series of articles (tidbits) on type conversion functions available in the Transaction SQL (T-SQL) language.
One analogy of a data type conversion is apply the correct action to change the physical form of water (H2O). You can convert water to ice by cooling it. You can convert water to vapor by heating it. However, the chemical composition of water is still the same!
Information is stored in a database as rows in a table. Each field in a table is defined with a certain data type. The original designer of the database schema might not be aware of all potential uses of the stored information. Therefore, it is not uncommon as a developer to convert data from one type to another.
Today, I want to talk about how to use CAST function which takes a source data value and a target data type as parameters. It returns the the source data value casted to the target data type.
The TSQL example below shows valid and invalid conversions for strings, numbers, and dates.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
-- -- Format variables using CAST() -- -- Famous quote DECLARE @VAR_QUOTE VARCHAR(128) = 'It is the mark of an educated mind to be able to entertain a thought without accepting it - Aristotle'; -- Full quote SELECT @VAR_QUOTE AS FULL_QUOTE; -- Unexpected truncation SELECT CAST(@VAR_QUOTE AS CHAR(34)) AS SHORT_QUOTE; -- My 64 K number DECLARE @VAR_NUMBER INT = POWER(2, 16); -- Full number SELECT @VAR_NUMBER AS LARGE_NUMBER; -- Overflow when converting SELECT CAST(@VAR_NUMBER AS SMALLINT) AS SHORT_NUMBER; -- Date as hex numbers DECLARE @VAR_BYTE AS BINARY(4) = 0x20130723; -- Conversion is allowed SELECT CAST('20130723' as DATE) AS RIGHT_DATE -- Conversion is not allowed SELECT CAST(@VAR_BYTE as DATE) AS WRONG_DATE |
The output from the TSQL example is shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
output: FULL_QUOTE ----------------------------------------------------- It is the mark of an educated mind to be able to entertain a thought without accepting it - Aristotle SHORT_QUOTE ----------------------------------------------------- It is the mark of an educated mind LARGE_NUMBER ------------ 65536 SHORT_NUMBER ------------ Msg 220, Level 16, State 1, Line 8 Arithmetic overflow error for data type smallint, value = 65536. RIGHT_DATE ---------- 2013-07-23 WRONG_DATE ---------- Msg 241, Level 16, State 1, Line 3 Conversion failed when converting date and/or time from character string. |
Here are the results from executing the TSQL example.
- The large string is converted to a smaller string. Truncation occurs as a side effect of the operation. No warning is produced.
- I am storing 2 raised to the 16th power as a integer. Casting the larger number to a smaller number results in a arithmetic overflow. A error is raised because of this operation.
- I am able to cast a string to a date. However, converting the date stored as a 4 byte hex code to a date data type fails. A error is raised because this conversion is not allowed.
Please note the CAST function uses the following matrix to transform data from one form to another.
Next time, I will be talking about the PARSE function that takes a source string, a target data type, and a optional culture information as parameters. It returns the target data type containing the transformed source string.