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

Posted on 2006-06-26
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.
Question by:winterhowl
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
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.
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)

Author Comment

ID: 16986597
Actually, I was trying to convert 0 when I received the error.
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

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

LVL 50

Accepted Solution

Lowfatspread earned 100 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..

Author Comment

ID: 17002410
I couldn't get any of the CONVERT or CAST to work, but once I removed them, everything works fine.
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?

Author Comment

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ESX 6.0 Best Practices for datastore size 5 168
physical_device_name field in SQL 3 38
simple bash script needed to unpack tar.gz 4 29
error in oracle form 11 28
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

733 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