?
Solved

How to insert minus sign (-) into column field

Posted on 2010-01-13
5
Medium Priority
?
937 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
[X]
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
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

752 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