Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««123»»

cast, convert and float! Expand / Collapse
Author
Message
Posted Thursday, August 20, 2009 8:58 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 3:10 AM
Points: 1,712, Visits: 6,241
A pity that 11 wasn't one of the options - then we'd have had to decide whether casting 10.5 as an integer truncated or rounded (and I would probably have guessed wrong!)
Post #774296
Posted Thursday, August 20, 2009 9:07 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, July 27, 2011 4:29 PM
Points: 36, Visits: 23
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 [Wink]


Yea! That's right! I'm only five short sips into my coke this morning so my wrong answer way way didn't count!

I'm guessing that the first @c declaration made it an implied int, which I didn't get right off. I thought that it would change once it was converted to a floating point integer. I've been out of this game for a while now. It's amazing on how much you start to lose ... or middle age is finally starting to play a small role.
Post #774302
Posted Thursday, August 20, 2009 10:31 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Yesterday @ 10:26 AM
Points: 77, Visits: 166
Re SET and SELECT, is there a realistic scenario OTHER than capturing previous statement's @@ERROR and @@ROWCOUNT where a single statement must be used? I think it's the only one that I've wanted.

I hadn't thought of the trick on that Web page of converting them to strings and concatenating them, to get both values captured by a SET statement. Effective - but ugly.

I suppose there isn't much point asking whether ANSI requires that each SET statement resets @@ROWCOUNT to 1 after execution - whether that's an ANSI rule or not, we are stuck with it. (Or are we?)
Post #774392
Posted Thursday, August 20, 2009 10:45 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 11:55 AM
Points: 1,293, Visits: 1,647
Jim in Arizona (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 [Wink]


Yea! That's right! I'm only five short sips into my coke this morning so my wrong answer way way didn't count!

I'm guessing that the first @c declaration made it an implied int, which I didn't get right off. I thought that it would change once it was converted to a floating point integer. I've been out of this game for a while now. It's amazing on how much you start to lose ... or middle age is finally starting to play a small role.


A floating point integer? I think you might need a stronger drink, Jim...
Post #774405
Posted Thursday, August 20, 2009 11:27 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, July 27, 2011 4:29 PM
Points: 36, Visits: 23
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.
Post #774439
Posted Thursday, August 20, 2009 3:59 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, March 24, 2014 4:01 PM
Points: 96, Visits: 85
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
Post #774649
Posted Thursday, August 20, 2009 4:09 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, July 27, 2011 4:29 PM
Points: 36, Visits: 23

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.
Post #774657
Posted Friday, August 21, 2009 3:57 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 4:02 PM
Points: 5,916, Visits: 8,167
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 MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #774872
Posted Friday, August 21, 2009 5:18 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Monday, July 21, 2014 7:44 PM
Points: 817, Visits: 1,646
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 :/
Post #774926
Posted Monday, August 24, 2009 2:30 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Yesterday @ 10:26 AM
Points: 77, Visits: 166
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??)
Post #775836
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse