简体   繁体   中英

SQL Query to retrieve a certain first name

Write a SQL query to retrieve the loan number, state and city, customer first name for loans that are in the states of CA,TX,FL,NV,NM but exclude the following cities (Dallas, SanFrancisco, Oakland) and only return loans where the customer first name begins with John.

What I attempted:

SELECT [LoanNumber], [State], [City], [CustomerFname]
FROM dbo.Loan
WHERE STATE IN ('CA','TX','FL','NV','NM') 
AND CITY NOT IN ('DALLAS','SANFRANCISCO','OAKLAND')
HAVING [CustomerFname] = ' Mr.John'

Add wild card entry (%) only at the end of the string to get customers starting with John.

SELECT [LoanNumber],[State],[City],[CustomerFname]
    FROM dbo.Loan
    WHERE STATE IN( 'CA','TX','FL','NV','NM') 
    AND CITY NOT IN('DALLAS','SANFRANCISCO','OAKLAND')
    AND [CustomerFname] LIKE 'Mr.John%'
SELECT [LoanNumber],[State],[City],[CustomerFname]
    FROM dbo.Loan
    WHERE STATE IN( 'CA','TX','FL','NV','NM') 
    AND CITY NOT IN('DALLAS','SANFRANCISCO','OAKLAND')
    AND [CustomerFname] LIKE '%Mr.John%'
SELECT [LoanNumber], [CustomerFname], [City], [State]
FROM [dbo].[Loan]
WHERE [State] IN ('CA','TX','FL','NV','NM') 
AND [City] NOT IN  ('Dallas', 'SanFrancisco', 'Oakland')
AND  [CustomerFname] LIKE '%John%'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM