STR

  • sqlnaive

    SSCoach

    Points: 17435

    Comments posted to this topic are about the item STR

  • Ron McCullough

    SSC Guru

    Points: 63877

    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]

  • Ford Fairlane

    SSCertifiable

    Points: 7664

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

    Hope this helps...

    Ford Fairlane
    Rock and Roll Detective

  • Mike Dougherty-384281

    SSCrazy

    Points: 2764

    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.

  • kupy

    SSCommitted

    Points: 1604

    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

  • sqlnaive

    SSCoach

    Points: 17435

    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:

    StringVar FloatAsChar FloatAsFloat

    1 1 1

    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.

  • Koen Verbeeck

    SSC Guru

    Points: 258909

    Nice question, thanks.

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

  • sqlnaive

    SSCoach

    Points: 17435

    Here is the URL showing data type conversion chart:

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

  • T.Ashish

    SSCarpal Tunnel

    Points: 4243

    good one

  • This was removed by the editor as SPAM

  • Bangla

    Hall of Fame

    Points: 3137

    Nice and interesting question.

  • Tee Time

    Hall of Fame

    Points: 3663

    Good question, thanks!

  • Thomas Abraham

    SSChampion

    Points: 10761

    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

  • Mark Cooper-1068662

    SSC Journeyman

    Points: 86

    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

  • Narud

    SSCrazy

    Points: 2819

    Thanks for this good question.

Viewing 15 posts - 1 through 15 (of 18 total)

You must be logged in to reply to this topic. Login to reply