SQL Clone
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
SSC-Enthusiastic
SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)

Group: General Forum Members
Points: 107 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
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27091 Visits: 13268
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?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
twin.devil
twin.devil
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2334 Visits: 2665
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
SSC-Enthusiastic
SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)

Group: General Forum Members
Points: 107 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