Results 1 to 7 of 7

Thread: combine columns with SELECT

  1. #1
    Join Date
    Apr 2009
    Posts
    8

    combine columns with SELECT

    Hello, I've searched for this solution but none have worked for me so I thought I ask directly.
    I have a variable called "name"
    I have a DB which contains 2 columns: FirstName and LastName

    how can I create a query like:
    SELECT First.Name, LastName FROMl dbo.Actors
    WHERE First.Name+ ' '+LastName = "Elijah Wood"

    The string "Elijah Wood" has a 'SPACE'.

    Thanks in advance for any assistance.

    --
    Jeff

  2. #2
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    You just did, use single quote for string instead of double quote and you are good to go.

    SELECT First.Name, LastName FROMl dbo.Actors
    WHERE FirstName+ ' '+LastName = 'Elijah Wood'

  3. #3
    Join Date
    Sep 2002
    Posts
    5,938
    SELECT First.Name, LastName FROM dbo.Actors
    WHERE First.Name = 'Elijah' and LastName = 'Wood'

  4. #4
    Join Date
    Apr 2009
    Posts
    3
    Obviously care must be taken when concattenating columns and strings together and comparing. Are the columns varchar or fixed length?. Strip or Trim functions may be used if valid in your RDBMS.

  5. #5
    Join Date
    Apr 2009
    Posts
    8
    Quote Originally Posted by skhanal View Post
    You just did, use single quote for string instead of double quote and you are good to go.

    SELECT First.Name, LastName FROMl dbo.Actors
    WHERE FirstName+ ' '+LastName = 'Elijah Wood'
    Thanks skhanal, I had tried that as well (single quotes). The query doesn't complain, it just doesn't return a record. I only get the column headings: FirstName LastName

  6. #6
    Join Date
    Apr 2009
    Posts
    8
    Quote Originally Posted by MikeH View Post
    Obviously care must be taken when concattenating columns and strings together and comparing. Are the columns varchar or fixed length?. Strip or Trim functions may be used if valid in your RDBMS.
    Mike, you may have hit onto something: the columns are both nchar(20)

    I'm using sqlserver express, I'll look into the strip or trim functions.

  7. #7
    Join Date
    Apr 2009
    Posts
    8
    Quote Originally Posted by jboyd111 View Post
    Mike, you may have hit onto something: the columns are both nchar(20)

    I'm using sqlserver express, I'll look into the strip or trim functions.
    I figured it out, thanks to the suggestions I read here. The code that works is:

    SELECT First.Name, LastName FROM dbo.Actors
    WHERE LTRIM(RTRIM(FirstName))+ ' '+LTRIM(RTRIM(LastName) = 'Elijah Wood'

    Thanks to everyone for your assistance!

    ------------
    Jeff

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •