January 13, 2020 at 12:00 am
Comments posted to this topic are about the item More Choice
January 13, 2020 at 6:24 am
Nice, easy one to start the day on, thanks Steve
____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
January 13, 2020 at 4:31 pm
The data type of the values is not important here, as they can vary. The value of @j-2 will determine the casting. If the data cannot be implicitly cast to the data type, an error is returned.
This is, at best, a misleading explanation. From BOL:
Returns the data type with the highest precedence from the set of types passed to the function.
In the example above, the highest precedence in the list is decimal/numeric, and so no matter what value @j-2 is, the value will first be cast to decimal/numeric. It may then be cast a second time when assigned to @j-2, depending on @j-2's data type, but the first casting can cause an error even if @j-2's type matches the expression.
Consider this variation on the question:
DECLARE @i INT = 6, @j varchar(10)
SELECT @j = CHOOSE(@i, 10,20,30,3.14,50,'Test')
SELECT @j
On the surface, we're assigning 'Test' to a varchar(10) variable. However, this fails because prior to assigning 'Test' to @j-2, it must be converted to numeric in order to evaluate the CHOOSE expression. It is true that any data types can be in a CHOOSE expression, and interesting to note that invalid conversions are not attempted except for the expression at the specified index. But it is not true that the other data types aren't important.
January 13, 2020 at 5:45 pm
I modified the explanation a bit. The answer stands since he's asking about the value returned by that batch which runs successfully.
January 16, 2020 at 1:25 pm
misconception:
1 - Index used to begin with (0) meaning index of (6) = null
2- Even if index begins with (1) the index of (6) = 'test' which would cause an error since 'test' could not be an integer
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply