Query regarding sql server [Resolved]

Posted by Sriharim under Sql Server on 2/6/2015 | Points: 10 | Views : 575 | Status : [Member] | Replies : 6
HI Team,
I have attended interview and below query output i din't understand.

In general, 1/2 and 1/2.0 is 0.50000 But in sql server i am getting different output:

1) output of :select 1/2 is 0
2) output of :select 1/2.0 is 0.50000

can please explain, why output is differenct and how the sql treats above 2 queries, how it works ?

---
Srihari



Responses

Posted by: Bandi on: 2/6/2015 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
In SQL Server, the result from division of two integers is INTEGER; whereas if any one of the operands of Division is DECIMAL, then it results DECIMAL value....


1/2 --> Here, the two operands 1, 2 are integers, so the result truncates the decimal value.... this way, it results 0

1/2.0 ( or 1.0/2) --> Here, one of the operand is DECIMAL, so the result doesn't truncate the DECIMAL scaling value.... In this way, it results 0.500000

Even if both operands are DECIMALS, it results DECIMAL value ( 0.500000 )

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sriharim, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 2/6/2015 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
refer
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/4cf91f5a-9c22-451a-8811-dc6621fe365f/how-to-get-values-with-decimals-when-dividing-2-integers-in-tsql

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sriharim, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 2/9/2015 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
updated above post.......

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sriharim, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sriharim on: 2/8/2015 [Member] Starter | Points: 25

Up
0
Down
Hi

This link is not working. Please update it.

---
Srihari

Sriharim, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sriharim on: 2/24/2015 [Member] Starter | Points: 25

Up
0
Down
Hi

Please help to resolve another question and please click below link for question

http://www.dotnetfunda.com/forums/show/19341/how-to-create-flat-files-dynamically-in-ssis?com=updatedquestion

---
Srihari

Sriharim, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 2/24/2015 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi,

Check those posts, I have posted for SSIS package requirements

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sriharim, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response