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

x
?
Solved

How to insert minus sign (-) into column field

Posted on 2010-01-13
5
Medium Priority
?
941 Views
Last Modified: 2012-08-14
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?
0
Comment
Question by:moonraker12
5 Comments
 
LVL 11

Expert Comment

by:rajvja
ID: 26303134
select cast(1234 as varchar(4)) + '-' + cast(342 as varchar(3)) from tablename
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 26303140
you have to use single quotes to delimit the string:

UPDATE yourtable
  SET yourfield = '12345-6789'
 WHERE ...

instead of:

UPDATE yourtable
  SET yourfield = 12345-6789
 WHERE ...

0
 
LVL 2

Expert Comment

by:saloj
ID: 26303269
Change your column datatype to varchar
0
 
LVL 14

Expert Comment

by:shru_0409
ID: 26303409
update table_name
set code = col1 + '-' + col2
where <your_condiontion>

try this
0
 

Author Comment

by:moonraker12
ID: 26303415
angelIII,

That worked perfectly.  I ran:

update table
set column = '12345-6789'

and got the desired outcome.

Thank you.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
Integration Management Part 2
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…

580 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