Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How do I convert an integer to tinyint within my VB.NET code?

Posted on 2006-06-26
8
Medium Priority
?
2,435 Views
Last Modified: 2008-01-09
I'm trying to run an INSERT statement, but I need to convert a regular integer to a tinyint.
How do I do that?


I tried using CAST(# as tinyint), but I receive an error.
0
Comment
Question by:winterhowl
  • 4
  • 3
8 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 16986287
>>I tried using CAST(# as tinyint), but I receive an error.<<
You will get an error if the integer is greater than 256.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 16986322
Actually that should have read greater than 255 and the error is:
Arithmetic overflow error for data type tinyint, value = 256.

If you do not care what value is inserted when greater than 255, you can do something like this:
CAST(YourIntegerValue % 256 As tinyint)
0
 

Author Comment

by:winterhowl
ID: 16986597
Actually, I was trying to convert 0 when I received the error.
0
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 16987467
Works fine for me:

Declare @X integer, @Y tinyint
Set @X = 0
Set @Y = CAST(@X as tinyint)
Select @Y

0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 400 total points
ID: 16991529
as long as the value is less than 256 you shouldn't need to convert SQL will do that automatically...

if the number is > 255 then your logic has an error..
0
 

Author Comment

by:winterhowl
ID: 17002410
I couldn't get any of the CONVERT or CAST to work, but once I removed them, everything works fine.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 17002747
>>I couldn't get any of the CONVERT or CAST to work<<
You do of course realize that CONVERT and CAST are T-SQL functions and not VB.NET functions, right?
0
 

Author Comment

by:winterhowl
ID: 17008784
Yes.  I was using them in the SQL I was using to get data from our database.
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

579 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question