Solved

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

Posted on 2006-06-26
8
2,131 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 50

Accepted Solution

by:
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..
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In today’s complex data management environments, it is not unusual for UNIX servers to be dedicated to a particular department, purpose, or database.  As a result, a SAS® data analyst often works with multiple servers, each with its own data storage…
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now