Link to home
Start Free TrialLog in
Avatar of proudpaki9
proudpaki9

asked on

Adding 2 coulmns into 1

Hello:

I have 2 fields Field1(int) and Filed2 (bigint) and here is the statement:
Update [dbo].[591726273]
set [LINK] = [Field1] + left([Field2],18)
LINK is the data type bigint
I am getting the following error message:
Server: Msg 248, Level 16, State 1, Line 1
The conversion of the varchar value '200705311867085988' overflowed an int column. Maximum integer value exceeded.
The statement has been terminated.

How do I fix this problem?

Thank you.
Avatar of Ted Bouskill
Ted Bouskill
Flag of Canada image

Update [dbo].[591726273]
set [LINK] = CAST([Field1] AS BIGINT) + left([Field2],18)
Avatar of proudpaki9
proudpaki9

ASKER

I tried the statement:
Update [dbo].[562070036]
set [LINK]= Cast([Field1] As Bigint)+ left([Field2],18)

The result:
Its only updating the field with the left 18 of field2, but not updaing with the field1 at all.
Total length for the field2 = 9
Total length for the field1 = 20
Please advise.

Thank you.
Try:

Update [dbo].[562070036]
set [LINK]= Cast([Field1] As Bigint)+ CAST(left([Field2],18) AS BIGINT)

I've noticed that SQL has some oddities in picking how it converts values.
Still doing the same only picking up the field2, 18.

Thanks
Are there any NULL values in [Field1]?

Let's go brute force and use the DECIMAL type

Update [dbo].[562070036]
set [LINK]= Cast([Field1] As DECIMAL(10,0))+ CAST(left([Field2],18) AS DECIMAL(21,0))

Same thing again.

Thanks
No nulls in the fields.
I'm going to try some queries in my Query Analyzer.  Can you send me a 1/2 dozen sample rows with Field1 and Field2 values?
LINK field(bigint)
Field1 (int)                                                                                  Field2(bigint)
218421091                                                                 2006010508822916570
214992248                                                                 2006011108827054820
205497164                                                                 2006011311827200890
207773513                                                                 2006011318296207860
216251984                                                                 2006020618312128630
212400308                                                                 2006022218323990690      

Thanks
Well I ran the following queries and they all worked.

CREATE TABLE #Ints (
      LINK BIGINT NULL,
      Field1 INT NULL,
      Field2 BIGINT NULL
)

SET NOCOUNT ON
INSERT INTO #Ints (Field1, Field2) VALUES (218421091,2006010508822916570)
INSERT INTO #Ints (Field1, Field2) VALUES (214992248,2006011108827054820)
INSERT INTO #Ints (Field1, Field2) VALUES (205497164,2006011311827200890)
INSERT INTO #Ints (Field1, Field2) VALUES (207773513,2006011318296207860)
INSERT INTO #Ints (Field1, Field2) VALUES (216251984,2006020618312128630)
INSERT INTO #Ints (Field1, Field2) VALUES (212400308,2006022218323990690)

-- The following line fails
--UPDATE #Ints SET LINK = Field1+LEFT(Field2,18) FROM #Ints

UPDATE #Ints SET LINK = CAST(Field1 AS BIGINT)+LEFT(Field2,18) FROM #Ints
SELECT * FROM #Ints

UPDATE #Ints SET LINK = Field1+(Field2/10) FROM #Ints
SELECT * FROM #Ints

UPDATE #Ints SET LINK = Field1+CAST(LEFT(Field2,18) AS BIGINT) FROM #Ints
SELECT * FROM #Ints

DROP TABLE #Ints
The method use is upadtign the temp table. How can I update the real table with the field?
ASKER CERTIFIED SOLUTION
Avatar of Ted Bouskill
Ted Bouskill
Flag of Canada 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
I tried your suggestions still not giving the value I am looking for.

Thanks
Hmm, well I am stumped. I can't replicate your error.  I've tried my answer on SQL 2000 and SQL 2005 and they both work correctly.