Click here to Skip to main content
15,747,637 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SQL
create table a_mark
(
    roll_no varchar(20),
    regulation_sub_id int,
    ob_mark decimal(5,2),
    mv_mark decimal(5,2)
)
 
 DECLARE @mv_mark varchar(2000),
         @ob_mark varchar(2000),
         @tot varchar(2000),
         @mv_mark1 varchar(2000),
         @ob_mark1 varchar(2000)

 SELECT  @mv_mark =mv_mark
 FROM    a_mark
 WHERE   roll_no = '11csu002' AND regulation_sub_id = 1

 SELECT  @ob_mark = ob_mark
 FROM    a_mark
 WHERE   roll_no = '11csu002' AND regulation_sub_id = 1

    SET @mv_mark1 = CAST(COALESCE(@mv_mark , 0) AS VARCHAR)  

    SET @ob_mark1 = CAST(COALESCE(@ob_mark, 0) AS VARCHAR) 

select @mv_mark1,@ob_mark1

When i execute this query it returns 'Conversion failed when converting the varchar value '12.00' to data type int'.i need '12.00' as result.what should i change here to gt a result like that??? can anyone help me
Posted
Updated 14-Jun-13 19:29pm
v6
Comments
_Amy 15-Jun-13 1:12am    
Here in this query, your are not using semester column and also where '12.00' in your code snippet? Please click on Improve question and provide the missing code block.

--Amit
nithya jen 15-Jun-13 1:21am    
this is just a sample.and '12.00' is mv_mark value from a_mark table.i just tried to select the value from that table directly.

Don't use int, use decimal (or whatever it is). When the source type wider then int, automatic conversion is incorrect, isn't that obvious. For example, none of the integer values can provide acceptable approximation for 12.34…

—SA
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Jun-13 1:44am    
You did not even explain what variables cause trouble, how can I give you a recipe. You really need understanding, not recipe.
And please, don't use you "textspeach", it's hard to read and is simply impolite to readers. Use full and correct spelling, capitalization, punctuation and grammar. I don't spare time to write all texts in full, why do you think you can?
—SA
Try this:
SQL
SELECT @mv_mark =CAST(mv_mark as VARCHAR)
FROM a_mark
WHERE roll_no = '11csu002' AND regulation_sub_id = 1
SELECT @ob_mark = CAST(ob_mark as VARCHAR)
FROM a_mark
WHERE roll_no = '11csu002' AND regulation_sub_id = 1



--Amit
 
Share this answer
 
Comments
nithya jen 15-Jun-13 1:37am    
thnk u sir....it works well...
_Amy 15-Jun-13 1:42am    
Welcome. Will you accept the answer(green button), if it is working for you?
nithya jen 15-Jun-13 2:30am    
i have to sum both the values.for example @mv_mark='15.00' and @ob_mark='5.00'.it shows the value like '15.005.00'.so i just tried to convert the value to int and then to varchar. but that doesnt work.it retunrns the same error.
SET @total =CAST(( CAST(ISNULL(@mv_mark, 0) AS INT) + CAST(ISNULL(@ob_mark, 0) AS INT)) AS VARCHAR)
_Amy 15-Jun-13 2:39am    
Then change @mv_mark, @mv_mark1, @ob_mark, @ob_mark1 to decimal(5,2). In that case it'll not require conversion at all. :)
nithya jen 15-Jun-13 3:03am    
thank you..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900