SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Casting question


Casting question

Author
Message
Eli Leiba
Eli Leiba
SSC Veteran
SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)

Group: General Forum Members
Points: 245 Visits: 19
Comments posted to this topic are about the item Casting question
bitbucket-25253
bitbucket-25253
SSC-Dedicated
SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)

Group: General Forum Members
Points: 34873 Visits: 25280
Nice question - had to think long and hard for the correct answer

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

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
demonfox
demonfox
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3645 Visits: 1192
cheeky ...

didn't occur a doubt on the 0 :-P
though I had a doubt over

select isnull ( convert (char(3),1.0/9) ,'*')
-- or

select isnull ( convert (char(3),1/9.0) ,'*')



and it clears out as it does throws an error
Msg 8115, Level 16, State 5, Line 1
Arithmetic overflow error converting numeric to data type varchar.



When Implicit conversion occurs , it gives out the output with default precision of 18,6..

so 1.0/9 is 0.111111..

Select select isnull ( convert (char(8),1.0/9) ,'*')



anything less than 8 would have the arithmetic overflow error for this scenario BigGrin

Nice question , though ...

Thanks for the question ..

~ demonfox
___________________________________________________________________
Wondering what I would do next , when I am done with this one Ermm
Dineshbabu
Dineshbabu
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1912 Visits: 569
Nice question..

--
Dineshbabu
Desire to learn new things..
Lokesh Vij
Lokesh Vij
SSCertifiable
SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)

Group: General Forum Members
Points: 5884 Visits: 1599
Easy one for me. Thanks!

~ Lokesh Vij

Guidelines for quicker answers on T-SQL question
Guidelines for answers on Performance questions

Link to my Blog Post --> www.SQLPathy.com

Follow me @Twitter


kapil_kk
kapil_kk
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10635 Visits: 2778
demonfox (3/14/2013)
cheeky ...

didn't occur a doubt on the 0 :-P
though I had a doubt over

select isnull ( convert (char(3),1.0/9) ,'*')
-- or

select isnull ( convert (char(3),1/9.0) ,'*')



and it clears out as it does throws an error
Msg 8115, Level 16, State 5, Line 1
Arithmetic overflow error converting numeric to data type varchar.

thanks for the explanation :-)

When Implicit conversion occurs , it gives out the output with default precision of 18,6..

so 1.0/9 is 0.111111..

Select select isnull ( convert (char(8),1.0/9) ,'*')



anything less than 8 would have the arithmetic overflow error for this scenario BigGrin

Nice question , though ...

Thanks for the question ..


_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Danny Ocean
Danny Ocean
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3404 Visits: 1549
Really a nice and good question.

one more thing apart from this question. if you set ARITHABORT OFF and then execute below code, it will give output as "*" due to insufficient char length. and if you set char length more than 7, it will give "0.111111".

It denotes that we need to provide an insufficient length whenever we convert values in char or varchar.


SET ARITHABORT OFF
select isnull ( convert (char(3),1.0/9) ,'*')

Thanks
Vinay Kumar
-----------------------------------------------------------------
Keep Learning - Keep Growing !!!
www.GrowWithSql.com
kapil_kk
kapil_kk
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10635 Visits: 2778
Danny Ocean (3/15/2013)
Really a nice and good question.

one more thing apart from this question. if you set ARITHABORT OFF and then execute below code, it will give output as "*" due to insufficient char length. and if you set char length more than 7, it will give "0.111111".

It denotes that we need to provide an insufficient length whenever we convert values in char or varchar.


SET ARITHABORT OFF
select isnull ( convert (char(3),1.0/9) ,'*')


Thanks Vinay for this... :-)

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Danny Ocean
Danny Ocean
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3404 Visits: 1549
Your Welcome Kapil :-)

Thanks
Vinay Kumar
-----------------------------------------------------------------
Keep Learning - Keep Growing !!!
www.GrowWithSql.com
kapil_kk
kapil_kk
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10635 Visits: 2778
Danny Ocean (3/15/2013)
Really a nice and good question.

one more thing apart from this question. if you set ARITHABORT OFF and then execute below code, it will give output as "*" due to insufficient char length. and if you set char length more than 7, it will give "0.111111".

It denotes that we need to provide an insufficient length whenever we convert values in char or varchar.


SET ARITHABORT OFF
select isnull ( convert (char(3),1.0/9) ,'*')


Vinay, I tried with ARITHABORT OFF but still I am getting same error "Arithmetic overflow error converting numeric to data type varchar."
set arithabort off
select ISNULL(CONVERT(char(3),1.0/9),'*')

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search