STR

  • Comments posted to this topic are about the item STR

  • So nice to be reminded of the fundamentals ... Thanks

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • Nice question, had to go do some digging again for this.

    Hope this helps...

    Ford Fairlane
    Rock and Roll Detective

  • 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.

  • 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

  • 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.

  • Nice question, thanks.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Here is the URL showing data type conversion chart:

    http://technet.microsoft.com/en-us/library/ms191530.aspx

  • good one

  • This was removed by the editor as SPAM

  • Nice and interesting question.

  • Good question, thanks!

  • 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

  • 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

  • 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