Wednesday, March 21, 2012

Joining two fields in a query

I am trying to join two fields in a query in SQL 2000. For example.

Update myTable SET field_1 = @.field_1_value , field_2 = @.field_2_value, field_3 = @.field_1_value + ' x ' + field_2_value

Is this even possible.

I want the user to input values for fields 1 and 2, then in the background combine the two and insert that value in field 3.

Thanks in advance,

Scotty_C

the ' x ' should also be inserted between the values.

|||that'll work! there's just a typo for the @.field_2_value

Update myTable SET field_1 = @.field_1_value , field_2 = @.field_2_value, field_3 = @.field_1_value + ' x ' + @.field_2_value|||

I agree that there was a typo, however, the given SQL Statement was just fabricated for the forum as an example.

Thank you for you input.

When I attempt to execute the Statement I get the Error Message:

"Syntax Error Converting the varChar value ' x ' to a column of datatype int."

The actual SQL Statement being used is this:

UPDATE SheetSizes
SET Width = @.Width, Length = @.Length, Standard = @.Standard, Label = @.Width + ' x ' + @.Length
WHERE (SheetSizeID = @.SheetSizeID)

The datatype for the column "Length" is varChar(50)

|||excuse me, it has been a long day, the datatype for the column "Label" is varChar(50) and the dataype for the columns "Length" and "Width" is int.|||

Scotty_C wrote:

I agree that there was a typo, however, the given SQL Statement was just fabricated for the forum as an example.

Thank you for you input.

When I attempt to execute the Statement I get the Error Message:

"Syntax Error Converting the varChar value ' x ' to a column of datatype int."

The actual SQL Statement being used is this:

UPDATE SheetSizes
SET Width = @.Width, Length = @.Length, Standard = @.Standard, Label = @.Width + ' x ' + @.Length
WHERE (SheetSizeID = @.SheetSizeID)

The datatype for the column "Length" is varChar(50)

you have to use CAST or CONVERT before concatenating your values...
UPDATE SheetSizes
SET Width = @.Width, Length = @.Length, Standard = @.Standard, Label = CAST(@.Width AS varchar(10)) + ' x ' + CAST(@.Length AS varchar(10))
WHERE (SheetSizeID = @.SheetSizeID)

HTH,|||

Yes CryptoKnight,

That works very well, thank you!

Thanks,

Scotty_C

No comments:

Post a Comment