Subscribe to Subscribers
Talk to Webmaster Tony John

Forums » .NET » .NET »

How to cast varchar to int


Posted Date: 25 Jun 2012      Posted By:: pinky     Member Level: Silver    Member Rank: 1242     Points: 2   Responses: 3



I have table called ExamInfo.In that table sub1 is column having varchar(50).Sub1 contain numeric,null and non-numeric. I want to select from ExamInfo where sub1 is greter than 81.
Here is my query: select * from Exam where cast(sub1 as int) > 81.But it fail to cast getting an error.




Responses

#677198    Author: Anil Kumar Pandey      Member Level: Platinum      Member Rank: 1     Date: 25/Jun/2012   Rating: 2 out of 52 out of 5     Points: 2

If the value contains decimal you must cast it to decimal value as int can not have the decimal part.

make sure the value is compatible with int if you are type casting it as INT

Thanks & Regards
Anil Kumar Pandey
Microsoft MVP, DNS MVM



 
#677212    Author: Ravindra Gaurana      Member Level: Gold      Member Rank: 179     Date: 25/Jun/2012   Rating: 2 out of 52 out of 5     Points: 2

Hi



Declare @value nvarchar(40)
--set @value='5'
set @value=NULL
--set @value='NULL' this will throw an error
--set @value=' '
SELECT convert(int,isnull(@value,0))


Thanks & Regards
Ravindra Gaurana
Dotnetravindera@gmail.com
Catch Me dotnetspider



 
#677225    Author: Ultimaterengan        Member Level: Gold      Member Rank: 9     Date: 25/Jun/2012   Rating: 2 out of 52 out of 5     Points: 2

If you column have only numeric then you can convert it by using cast.

In case you column have numeric and non numeric then you can not able to use cast.



Thanks & Regards
G.Renganathan
Nothing is mine ,Everything is yours!!!


http://renganathan1984.blogspot.com/






 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Next : How can I connect Oracle Database using C#
Previous : Access child class function
Return to Discussion Forum
Post New Message
Category:

Related Messages
Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
2005 - 2012 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.