Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


convert decimal to int in case condition


convert decimal to int in case condition

Author
Message
neethu payal
neethu payal
Valued Member
Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)

Group: General Forum Members
Points: 63 Visits: 94
i have a table exam_setup
CREATE TABLE exam_setup
(
setup_id INT,
sub_id INT,
assignment decimal(4,1),
attendance decimal(4,1),
INT_1 decimal(4,1),
INT_2 decimal(4,1)
)
table values are,
sub_id = 75,
assignment = 25.0
attendance = 15.5
INT_1 =0.0
INT_2 =0.0

I have to fetch the values like
assignment = 25
attendance = 15.5
INT_1 =0
INT_2 =0
if decimal point is 0 then have to return integer value only.. but the below query doesn't work, why?

SELECT CASE WHEN assignment LIKE '%.0' THEN CONVERT(INT,assignment) ELSE assignment END,
CASE WHEN attendance LIKE '%.0' THEN CONVERT(INT,attendance) ELSE attendance END,
CASE WHEN INT_1 LIKE '%.0' THEN CONVERT(INT,INT_1) ELSE INT_1 END,
CASE WHEN INT_2 LIKE '%.0' THEN CONVERT(INT,INT_2) ELSE INT_2 END
FROM exam_setup
WHERE sub_id = 75
Koen Verbeeck
Koen Verbeeck
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16461 Visits: 13207
CASE returns a single data type.
From BOL:

Returns the highest precedence type from the set of types in result_expressions and the optional else_result_expression.


Formatting is best done in the front tool, not in SQL Server itself.



How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at SQLKover.

MCSA SQL Server 2012 - MCSE Business Intelligence
twin.devil
twin.devil
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1378 Visits: 2597
you can do this in t-sql using Format function.


Declare @tbl table ( id int, assignment decimal(11,8))

insert into @tbl
select 1, 10.00 union all
select 1, 8.55 union all
select 1, 0.55


select id, assignment, format(assignment,'###.##') AS assignment_format
from @tbl




As per BOL the return data type will be in nvachar. Koen Verbeeck is rightly said that

Formatting is best done in the front tool, not in SQL Server itself.


hope it helps
neethu payal
neethu payal
Valued Member
Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)

Group: General Forum Members
Points: 63 Visits: 94
thank u
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