October 7, 2013 at 8:12 pm
October 7, 2013 at 8:52 pm
Nice question, had to go do some digging again for this.
Hope this helps...
Ford Fairlane
Rock and Roll Detective
October 7, 2013 at 9:31 pm
I try to figure these out from in-memory knowledge rather than BOL...
Can you clarify for those who assumed you were being tricky using a string casting function on already-a-string?
STR ( float_expression [ , length [ , decimal ] ] )
float_expression
Is an expression of approximate numeric (float) data type with a decimal point.
STR ( '8E10' )
'8E10' is a string literal, not a float_expression
Even with implicit casting I'd have expected to get a numeric value of eight (8) but that expectation might come from scripting languages' treatment of numbers in strings.
October 8, 2013 at 12:26 am
Mike Dougherty-384281 (10/7/2013)
'8E10' is a string literal, not a float_expression
Even with implicit casting I'd have expected to get a numeric value of eight (8) but that expectation might come from scripting languages' treatment of numbers in strings.
'8E10' is a string, But since we need float the function convert it to float and then back to string
October 8, 2013 at 1:26 am
Mike & Kupy, that was done to make it little tricky (and thats why the last option was there). Though it seems many got it correct so most of them are good. For the people who took it wrong, here it goes:
declare @STR varchar(4), @flt1 float, @flt2 float
select @STR = '8E10'
select @flt1 = '8E10'
select @flt2 = 8E10
select ISNUMERIC(@str) as 'StringVar', ISNUMERIC(@flt1) as 'FloatAsChar', ISNUMERIC(@flt2) as 'FloatAsFloat'
The result is:
StringVarFloatAsCharFloatAsFloat
111
As the function expects the float expression, if there is not the expected type it will try to do an implicit conversion first. If it is able to convert then it works fine as in this case. If not, then it will give error as below:
Msg 8114, Level 16, State 5, Line 7
Error converting data type varchar to float.
October 8, 2013 at 1:30 am
Nice question, thanks.
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
October 8, 2013 at 1:33 am
Here is the URL showing data type conversion chart:
October 8, 2013 at 1:58 am
good one
October 8, 2013 at 2:20 am
This was removed by the editor as SPAM
October 8, 2013 at 5:48 am
Nice and interesting question.
October 8, 2013 at 6:12 am
Good question, thanks!
October 8, 2013 at 6:24 am
Excellent! So good to see a very brief question with quite a few basic concepts packed into it. I got it right only because I realized that the literal string would be converted to the parameter type required for the function, and then noted that the length parameter was not used, resulting in a default length.
Let's see some more from this poster please?
[font="Verdana"]Please don't go. The drones need you. They look up to you.[/font]
Connect to me on LinkedIn
October 8, 2013 at 8:45 am
I have run this in all versions of SQL Server from 2000 to 2008 R2 and get the same answer - 10 '*'s.
select STR('8E10') -- returns '**********'
select len(STR('8E10')) -- returns 10
October 8, 2013 at 10:43 am
Thanks for this good question.
Viewing 15 posts - 1 through 15 (of 17 total)
You must be logged in to reply to this topic. Login to reply