Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 278
  • Last Modified:

SQL Server insert query

INSERT INTO [DBNAME].[dbo].[PART]
           ([ID]
           ,[DESCRIPTION]
SELECT [ID] & '-RG' AS Expr1
      ,[DESCRIPTION]
From OTHERDB.dbo.Part

So the key is adding the -RG to the end of the ID field from the otherdb which as the query stands gives me:

Msg 402, Level 16, State 1, Line 147
The data types varchar and varchar are incompatible in the '&' operator.
0
MyDanes
Asked:
MyDanes
  • 2
1 Solution
 
carsRSTCommented:
SQL Server uses the "+" sign for concatenation.
Take out the "&" and replace with "+"
0
 
carsRSTCommented:
Also you may need to use the CAST function if you get another error.

something like this...


INSERT INTO [DBNAME].[dbo].[PART]
           ([ID]
           ,[DESCRIPTION]
SELECT CAST([ID] as varchar(5)) + '-RG' AS Expr1
      ,[DESCRIPTION]
From OTHERDB.dbo.Part
0
 
SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
hello, you can not store string into int datatype you should take one more field for save this ID and store with -RG into this field.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now