I am going to continue the 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 the PARSE function which 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.
The TSQL example below shows valid and invalid conversions. Since I am not familiar with the presentation of German dates and times, I used the FORMAT function to create me the strings for use in my code.
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 |
-- -- Convert strings w/ culture info 2 data types using PARSE() -- -- Passes since write culture SELECT PARSE('£45.99' AS MONEY USING 'en-GB') AS COST_OF_TOY1; -- Fails since wrong culture SELECT PARSE('£45.99' AS MONEY USING 'en-US') AS COST_OF_TOY2; -- Declare variables DECLARE @VAR_DE_DATE VARCHAR(32); DECLARE @VAR_DE_TIME VARCHAR(32); -- Store german date/time SELECT @VAR_DE_DATE = FORMAT(SYSDATETIME(), 'D', 'de') SELECT @VAR_DE_TIME = FORMAT(SYSDATETIME(), 'G', 'de') -- Show our german strings SELECT @VAR_DE_DATE AS GERMAN_FULLDATE_STR SELECT @VAR_DE_TIME AS GERMAN_DATETIME_STR -- Convert to english data types SELECT PARSE(@VAR_DE_DATE AS DATE USING 'de') AS ENGLISH_DATE; SELECT PARSE(@VAR_DE_TIME AS TIME USING 'de') AS ENGLISH_TIME; |
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: COST_OF_TOY1 --------------------- 45.99 COST_OF_TOY2 --------------------- Msg 9819, Level 16, State 1, Line 2 Error converting string value '£45.99' into data type money using culture 'en-US'. GERMAN_FULLDATE_STR -------------------------------- Dienstag, 23. Juli 2013 GERMAN_DATETIME_STR -------------------------------- 23.07.2013 15:47:24 ENGLISH_DATE ------------ 2013-07-23 ENGLISH_TIME ---------------- 15:47:24.0000000 |
Here are the results from executing the TSQL example.
- I was able to convert £45.99 pounds to a money data type when using the Great Britain cultural format. However, it fails when using the English cultural format. We use dollars in America!
- I was able to generate both a full date string as well as a numeric date string in the German language.
- Using German as the cultural format, I was able to convert the prior strings to both a DATE and TIME data type.
In summary, the PARSE function is used to convert strings to other data types. Cultural specific information for money, date, or time formatting can be accounted for.
Next time, I will be talking about the TRY_CONVERT function. This function was introduced in SQL Server 2012. Unlike its cousin, invalid casting of data results in a NULL value instead of an ERROR.