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 ««1234»»»

Casting question Expand / Collapse
Author
Message
Posted Friday, March 15, 2013 1:32 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 2:56 AM
Points: 2,842, Visits: 3,876
Good question but the correct answer is "0 " (with 2 trailing spaces), not "0".#

Edit: Add example:
SELECT REPLACE(ISNULL ( CONVERT (char(3),1/9),'*'),' ','<Space>') AS Ret

0<Space><Space>


Best Regards,
Chris Büttner
Post #1431385
Posted Friday, March 15, 2013 1:45 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: Thursday, October 23, 2014 2:32 AM
Points: 844, Visits: 1,378
Really easy question, but the explanation puzzles me. As far as I can see it is no implicit conversion, just a pure integer division.



Ole Kristian Velstadbråten Bangås - Virinco - Facebook - Twitter

Concatenating Row Values in Transact-SQL
Post #1431388
Posted Friday, March 15, 2013 1:53 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 9:50 AM
Points: 3,143, Visits: 3,419
Nice and easy question to end the week on - thanks

-------------------------------
Posting Data Etiquette - Jeff Moden
Smart way to ask a question

There are naive questions, tedious questions, ill-phrased questions, questions put after inadequate self-criticism. But every question is a cry to understand (the world). There is no such thing as a dumb question. ― Carl Sagan
I would never join a club that would allow me as a member - Groucho Marx
Post #1431391
Posted Friday, March 15, 2013 1:54 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, July 31, 2014 7:15 PM
Points: 1,129, Visits: 1,163
okbangas (3/15/2013)
Really easy question, but the explanation puzzles me. As far as I can see it is no implicit conversion, just a pure integer division.

+1
0.111111 being counted as 0 , is a round off to integer ..

will that be implicit conversion OP had in mind,0.11111-float to 0 - int?



~ demonfox
___________________________________________________________________
Wondering what I would do next , when I am done with this one
Post #1431392
Posted Friday, March 15, 2013 2:21 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 1:45 AM
Points: 2,570, Visits: 2,428
okbangas (3/15/2013)
Really easy question, but the explanation puzzles me. As far as I can see it is no implicit conversion, just a pure integer division.

+1
Post #1431401
Posted Friday, March 15, 2013 3:31 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 3:12 PM
Points: 1,016, Visits: 2,316
ah ... this was soo easy, i just selected the answer in one click.... but selected the wrong one. (interesting read, so far)

ww; Raghu
--
The first and the hardest SQL statement I have wrote- "select * from customers" - and I was happy and felt smart.
Post #1431424
Posted Friday, March 15, 2013 4:08 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, February 6, 2014 4:15 AM
Points: 1,242, Visits: 1,546
kapil_kk (3/15/2013)
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.

[code="sql"]
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),'*')


Kapil, you need to SET ANSI_WARNINGS OFF. Look at below two examples. Example 1 execute successfully but example 2 give an error.



---- Example 1
SET ANSI_WARNINGS OFF
SET ARITHABORT OFF
GO

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

SET ARITHABORT ON
SET ANSI_WARNINGS ON



---- Example 2
SET ANSI_WARNINGS ON
GO

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





Thanks
Vinay Kumar
-----------------------------------------------------------------
Keep Learning - Keep Growing !!!
www.GrowWithSql.com

Post #1431434
Posted Friday, March 15, 2013 4:40 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 2:07 PM
Points: 4,043, Visits: 5,361
Nice, simple question to end the week, thanks Eli

____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
Post #1431442
Posted Friday, March 15, 2013 4:44 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, October 16, 2014 2:35 AM
Points: 89, Visits: 339
Nice question, thanks. It made me think - I better rest now ......

Dave Morris

"Measure twice, saw once"
Post #1431446
Posted Friday, March 15, 2013 5:25 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 2:47 AM
Points: 1,926, Visits: 2,352
Danny Ocean (3/15/2013)
kapil_kk (3/15/2013)
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.

[code="sql"]
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),'*')


Kapil, you need to SET ANSI_WARNINGS OFF. Look at below two examples. Example 1 execute successfully but example 2 give an error.



---- Example 1
SET ANSI_WARNINGS OFF
SET ARITHABORT OFF
GO

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

SET ARITHABORT ON
SET ANSI_WARNINGS ON



---- Example 2
SET ANSI_WARNINGS ON
GO

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



Yes, It works....



_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1431465
« Prev Topic | Next Topic »

Add to briefcase ««1234»»»

Permissions Expand / Collapse