varchars and int

M

Matthew

I have build a shopping cart system with MSSQL and it runs fine. However,
my client changed the project reqirements (how often have you heard that?)

They make wood parts, and want to sell them whole (as they are in the DB
now) and split in half.

My development site uses an Access .mdb file. The following worked OK:

SELECT ID + '.5' AS id
FROM tablename

However, when I tried it in MSSQL I get this error:
Syntax error converting the varchar value '.5' to a column of data type int.

I know it might be considered bad database design. However, I was trying to
save some work. My fear is I might have to redesign the order system to
reference by part number instead of the part's ID.

So, what do you folks think?

Matthew
 
D

Douglas J. Steele

What do you want it to be: numeric or text?

For numeric, I believe you just need to remove the quotes:

SELECT ID + .5 AS ID
FROM TableName

It's possible you might have to convert the integer ID to decimal first.

SELECT CAST([ID] AS decimal) + .5 AS ID
FROM TableName

or reversing the order of .5 and ID might work as well.

For text, you'll need to use the CAST function to convert the number to
text:

SELECT CAST([ID] AS varbinary(20)) + '.5' AS ID
FROM TableName
 
S

Sylvain Lafontaine

Convert first the ID to the varchar type:

select convert (varchar (12), id) + '.5' as id

There is also the operator CAST, which basically do the same thing as
Convert() but with a different syntax.

S. L.
 
M

Matthew

Douglas J. Steele said:
What do you want it to be: numeric or text?

For numeric, I believe you just need to remove the quotes:

SELECT ID + .5 AS ID
FROM TableName

That solved it! I am a bit embarrassed; I know better than that.
Oh well, I guess I just need to get some extra sleep tonight.

Thanks for your help.

Matthew
 
M

Matthew

Douglas J. Steele said:
What do you want it to be: numeric or text?

For numeric, I believe you just need to remove the quotes:

SELECT ID + .5 AS ID
FROM TableName

That solved it! I am a bit embarrassed; I know better than that.
Oh well, I guess I just need to get some extra sleep tonight.

Thanks for your help.

Matthew
 
P

peggyflying

Matthew said:
I have build a shopping cart system with MSSQL and it runs fine. However,
my client changed the project reqirements (how often have you heard that?)

They make wood parts, and want to sell them whole (as they are in the DB
now) and split in half.

My development site uses an Access .mdb file. The following worked OK:

SELECT ID + '.5' AS id
FROM tablename

However, when I tried it in MSSQL I get this error:
Syntax error converting the varchar value '.5' to a column of data type int.

I know it might be considered bad database design. However, I was trying to
save some work. My fear is I might have to redesign the order system to
reference by part number instead of the part's ID.

So, what do you folks think?

Matthew
 
A

ASS66767767667

U¿ytkownik "Matthew said:
I have build a shopping cart system with MSSQL and it runs fine. However,
my client changed the project reqirements (how often have you heard that?)

They make wood parts, and want to sell them whole (as they are in the DB
now) and split in half.

My development site uses an Access .mdb file. The following worked OK:

SELECT ID + '.5' AS id
FROM tablename

However, when I tried it in MSSQL I get this error:
Syntax error converting the varchar value '.5' to a column of data type int.

I know it might be considered bad database design. However, I was trying to
save some work. My fear is I might have to redesign the order system to
reference by part number instead of the part's ID.

So, what do you folks think?

Matthew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top