SQL Combine Two Columns in Select Statement

前端 未结 5 1526
栀梦
栀梦 2020-12-03 10:23

If I have a column that is Address1 and Address2 in my database, how do I combine those columns so that I could perform operations on it only in my select statement, I will

相关标签:
5条回答
  • 2020-12-03 10:30

    In MySQL you can use:

    SELECT CONCAT(Address1, " ", Address2)
    WHERE SOUNDEX(CONCAT(Address1, " ", Address2)) = SOUNDEX("Center St 3B")
    

    The SOUNDEX function works similarly in most database systems, I can't think of the syntax for MSSQL at the minute, but it wouldn't be too far away from the above.

    0 讨论(0)
  • 2020-12-03 10:31

    I think this is what you are looking for -

    select Address1+Address2 as CompleteAddress from YourTable
    where Address1+Address2 like '%YourSearchString%'
    

    To prevent a compound word being created when we append address1 with address2, you can use this -

    select Address1 + ' ' + Address2 as CompleteAddress from YourTable 
    where Address1 + ' ' + Address2 like '%YourSearchString%'
    

    So, '123 Center St' and 'Apt 3B' will not be '123 Center StApt 3B' but will be '123 Center St Apt 3B'.

    0 讨论(0)
  • 2020-12-03 10:36

    If you don't want to change your database schema (and I would not for this simple query) you can just combine them in the filter like this: WHERE (Address1 + Address2) LIKE '%searchstring%'

    0 讨论(0)
  • 2020-12-03 10:38

    If your address1 = '123 Center St' and address2 = 'Apt 3B' then even if you combine and do a LIKE, you cannot search on searchstring as 'Center St 3B'. However, if your searchstring was 'Center St Apt', then you can do it using -

    WHERE (address1 + ' ' + address2) LIKE '%searchstring%'
    
    0 讨论(0)
  • 2020-12-03 10:48
    SELECT StaffId,(Title+''+FirstName+''+LastName) AS FullName 
    FROM StaffInformation
    

    Where do you write with in the brackets this will be appear in the one single column. Where do you want a dot into the middle of the Title and First Name write syntax below,

    SELECT StaffId,(Title+'.'+FirstName+''+LastName) AS FullName 
    FROM StaffInformation
    

    These syntax works with MS SQL Server 2008 R2 Express Edition.

    0 讨论(0)
提交回复
热议问题