cast, convert and float!

  • That is good to know the next time I want to:

    A.) Convert an interger to a varchar then

    B.) Convert that varchar to a smaller varchar then

    C.) Cast said varchar as a float then

    D.) Store float as an integer then

    E.) Select the output for use somewhere else...

    DECLARE @C INT,@d INT

    SET @C = 211

    SET @d = CAST(CONVERT(VARCHAR(2),CONVERT(VARCHAR(3),@c)) as FLOAT) * 0.5

    SELECT @d

  • That is good to know the next time I want to:

    A.) Convert an interger to a varchar then

    B.) Convert that varchar to a smaller varchar then

    C.) Cast said varchar as a float then

    D.) Store float as an integer then

    E.) Select the output for use somewhere else...

    Yea, you're right. This is good practice for the next time I have to do such tasks.

  • JacekO (8/20/2009)


    Scary part is over 30% (at the time of this post) got it wrong. Many QoDs are quite tricky but this one is very basic and really not SQL specific (data type conversion or casting is an ABC of any programming language).

    Looks like a concept of code review or inspection is missing way too often...

    I'm part of that 30%. (32% by now...)

    In my case, the problem was that I spent all my energy at dissecting and understanding the nested CONVERT and CAST operations, and when I was finished I immediately answered without noticing that the result was first assigned to a variable before displaying it. Oh well.

    On a side note, the explanation is not entirely correct. It says: "which, multiplied by 0.5 gives 10 (and not 10.5) as @C is of int datatype". Better would have been to write "which, multiplied by 0.5 gives 10.5, which is then truncated to 10 as @C is of int datatype". But maybe that's a bit too nitpicky?


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • same as everyone else who got it wrong ... focussed on the varchar and float operations, and totally forgot it was being assigned to an int at the end :/

  • Jim in Arizona (8/20/2009)


    A floating point integer? I think you might need a stronger drink, Jim... [Tongue]

    Yea, it's not my day, let me tell ya. It sounded good though. 🙂

    Everyone's "voice" here sounds equally authoritative - we should have different fonts for different levels of perceived authority. Dibs on ancient Gothic 🙂 ("Cuneiform", what's that??)

  • Chris Harshman (8/20/2009)


    I think the 30% of us (me included) that got it wrong just need to make sure we get our morning caffiene before trying to answer the questions 😉

    Yep - add me to that 30% (31%now). Spent too long working out what 21 *0.5 was to remember to check the @C assignment afterwards! Easy point thrown away there! Coffee please. Black. Strong!

  • webrunner (8/20/2009)


    I just plain got it wrong because of the int @C, after focusing too much on confirming the 21 * 0.5 calculation. Good question in that it highlights rookie mistakes I shouldn't be making....

    Ditto :-). FWIW, though, I would never name an @int variable "c" (or "d")!

  • JacekO (8/20/2009)


    Scary part is over 30% (at the time of this post) got it wrong. Many QoDs are quite tricky but this one is very basic and really not SQL specific (data type conversion or casting is an ABC of any programming language).

    Looks like a concept of code review or inspection is missing way too often...

    For me the really scary part is that I agree wth JacekO's comment and I got it wrong! Maybe senile dementia is setting in!!!

    Tom

Viewing 8 posts - 16 through 22 (of 22 total)

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