Skip to main content
Inspiring
February 12, 2023
Answered

Correct syntax for string comparison in SQL?

  • February 12, 2023
  • 1 reply
  • 1988 views

Hello

Sorry I think I may have asked this question in the past but now this has become a comedy of errors.

Simply put, there are two tables in the same DB containing rows of individuals last names, first, DOB etc,

Table 1 has 3000 records

Table 2 has 4000 in which the above 3000 are intermingled with no other matching identity except last name.

I need the query to pull out the 1000 names that were added.

 

No success yet on several approaches. Last names are nvarchar max, SQL Server.

 

<cfquery name="find_added_lastnames" datasource="#Request.BaseDSN#">

Select lastname1
from TABLE1
join TABLE2
on ' '+ lastname1+' ' not like '% '+ lastname2+' %'
ORDER BY lastname2 ASC

 

<cfquery name="find_added_lastnames" datasource="#Request.BaseDSN#">

SELECT lastname1, lastname2
FROM table1, table2
WHERE lastname1 NOT LIKE '%lastname2%'
ORDER BY lastname2 ASC
</cfquery>

 

 

<cfquery name="find_added_lastnames" datasource="#Request.BaseDSN#">

SELECT lastname1, lastname2
FROM table1, table2
WHERE lastname1 NOT LIKE lastname2 + '%'
ORDER BY lastname2 ASC
</cfquery>

 

Actually running the above query works with “LIKE” vs “NOT LIKE” returns- New Last Names: 14388 (cartesian product).

Plus several iterations of these.

Any help much appreciated.

Thank You- Norman B.

    This topic has been closed for replies.
    Correct answer seasonedweb

    Hello I'm still not able to read the results if you want to see it's here:

    https://chicopeeclan.net/bkbk-1.cfm


    I think I did it- and I appreciate all the help from BKBK & Dave Watts!

    I successfully added 147 new records to the main DB that did not match first & last names already there.

     

    I used:

    INSERT INTO a

    VALUES ( b.lastname, b.firstname

    WHERE NOT EXISTS

    (SELECT a.lastname, a.firstname
    FROM a
    WHERE a.lastname =  b.lastname 
    AND a.firstname =  b.firstname 

    )

    1 reply

    BKBK
    Community Expert
    Community Expert
    February 15, 2023

    There is a simple solution I can think of. Select from Table2, using Table1 to filter by lastname.

     

    That is, 

    SELECT firstname, lastname, dob, etc
    FROM table2
    WHERE lastname NOT IN (SELECT lastname FROM table1)

    Inspiring
    February 15, 2023

    BKBK Thank you so much for your help all these years! I'm 72 now & neen to finally keep a DB with all the queries that address what I often encounter- I'm still working on it.

    So this query using NOT IN:

    SELECT ged_lastname_utf, ged_firstname_utf
    FROM gedcomutf8
    WHERE ged_lastname_utf NOT IN

    (SELECT ged_lastname, ged_firstname
    FROM ged_main
    )

    ORDER BY ged_lastname ASC

     

    Results:

    ][SQLServer]Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

    Thanks again!

     

    Inspiring
    February 15, 2023

    taking out , ged_firstname errors

     
    Invalid column name 'ged_lastname' think I have it backwards