Link to home
Start Free TrialLog in
Avatar of moonraker12
moonraker12Flag for United States of America

asked on

How to insert minus sign (-) into column field

I know that this may be a simple question, but I am not too familiar with SQL and have not been able to find a solution.  In a SQL 2005 Database table, I am trying to update a column field with a '-'.

As an example, I want to fill the column field 'code' with '12345-6789'.  The problem is that when I attempt to do this, the SQL Query sees this as a request for the difference of the 2 numbers (12345 - 6789 = 5556), resulting in a field value of 5556.

Is there anyway to add the '-' into the column field, without having it subtract the values before and after it?
Avatar of rajvja
rajvja
Flag of United Kingdom of Great Britain and Northern Ireland image

select cast(1234 as varchar(4)) + '-' + cast(342 as varchar(3)) from tablename
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Change your column datatype to varchar
update table_name
set code = col1 + '-' + col2
where <your_condiontion>

try this
Avatar of moonraker12

ASKER

angelIII,

That worked perfectly.  I ran:

update table
set column = '12345-6789'

and got the desired outcome.

Thank you.