How To Use The Where & Like Operator Together in SQL? - sql

I want to display a List of Instructors with only first name and last name, from Georgia and whose last name ends in ‘son’.
I have written this so far:
SELECT firstname, lastname, state
FROM instructors
WHERE state = 'ga'
AND lastname LIKE '%son'
But it is not returning the information requested just a blank table.
Any help would be greatly appreciated

To find names ending in 'son' you need to make a small change and remove the second '%' sign. with both it looks for 'son' any where such as 'sonnentag'
The second one I would guess that the DB has Georgia as 'GA' not 'ga'. Case is important.
SELECT firstname, lastname, state
FROM instructors
WHERE state = 'GA'
AND lastname LIKE '*son'

This is a formatted comment that shows you how to troubleshoot. Start with this:
SELECT firstname, lastname, state
FROM instructors
WHERE 1 = 1
-- and state = 'ga'
--AND lastname LIKE '%son'
If that returns no records, you have no data. If it does, uncomment the two other filters, one at a time, to see which one causes no records to be returned. It could well be that you simply have no matching records

FINALLY !!!!!
I Figured It Out!
SELECT firstname, lastname, state
FROM instructors
WHERE state = 'ga'
AND lastname LIKE '*son*'
Instead of using the %WildCard I inserted *WildCard on both ends and it displayed exactly what I was requesting !
Thanks Everyone For Your Help!

This should works to you.
SELECT [firstname],[Lastname],[state]
FROM instructors AS i
WHERE i.state = 'ga' AND i.lastname LIKE '%son'

Related

Ordery By and Like is not working as expected

I am working on the autosearch UI where, I need to search the names listed from the letter I search in text box. Ex. You can take as search box works in Google search engine, where when we start entering letters it shows list under that searched text.
Below is the query where I am getting results when using LIKE but it is not working as expected, there are two screen shots, currently I am getting the results like first pic.
I even feel even order by is also not working.
NOTE :
I need both Order By.
Even though there are list which starts from d it is not displaying at first.
Second Pic :
If you see the second results are coming correct, but why should I remove the wild card syntax under ProfileName.
Let me know how I should fix this.
Your result is correctly showing what you've written in your query.
The first result shows all records with
IsPrimary = 1 AND LastName starts with 'd', OR
ProfileName starts with 'd'
The second result shows records with
IsPrimary = 1 AND LastName starts with 'd', OR
ProfileName = 'd'
Note that LIKE is case insensitive.
If you want to prioritize the search to LastName then to ProfileName.
Do this
Select * FROM
(
SELECT [your list here], 1 as priority
FROM Employee E
INNER JOIN [your list here]
WHERE
EP.IsPrimary = 1 AND E.LastName LIKE #SearchByChar + '%'
UNION
SELECT [your list here], 2 as priority
FROM Employee E
INNER JOIN [your list here]
WHERE
P.ProfileName LIKE #SearchByChar + '%'
) s
ORDER BY priority
If you want name matches to appear above profile matches, you could just do something like:
ORDER BY
CASE WHEN E.LastName LIKE #SearchByChar + '%' THEN 0 ELSE 1 END,
E.LastName,
P.ProfileName

How to only pull data from table 1 if table 2 also have data

My problem is, I have two tables that we are using for shipping and this is what is happening. If ca.addr_1 has data is fine but if there is data in c.addr_2 then show it. And what I want is if ca.addr has the data on some line use it but if all lines are null then use c.addr.
CO.SHIP_TO_ADDR_NO AS Addr_No,
ISNULL(CA.NAME, C.NAME) AS Name,
ISNULL(CA.ADDR_1, C.ADDR_1) AS Addr_1,
ISNULL(CA.ADDR_2, C.ADDR_2) AS Addr_2,
ISNULL(CA.ADDR_3, C.ADDR_3) AS Addr_3,
ISNULL(CA.CITY, C.CITY) AS City,
ISNULL(CA.STATE, C.STATE) AS State,
ISNULL(CA.ZIPCODE, C.ZIPCODE) AS ZipCode,
ISNULL(CA.COUNTRY, C.COUNTRY) AS Country,
CASE WHEN should allow you to do what you need.
CASE WHEN ca.addr1 IS NULL AND CA.ADDR_2 IS NULL AND CA.ADDR_3 IS NULL
THEN c.addr
ELSE WHEN ....
You can add in as many variations using ELSE WHEN as you need to account for whatever permutation of address results you want to select.

SQL for Splitting Names - Some with Middle Initial, Some Without

I'm fairly new to SQL, I'm actually just building a small database in Access right now although if there is necessary functionality that Access can't do I'll remake the tables in SQL Server.
Here's my situation, I have a list of names that come from a data dump from a third party. In our database I need to be able to compare first and last names in separate columns.
I've been trying to use InStr, Left and Right - but am getting hung up with weird results
Left([NewClaims]![Claimant Full Name],InStr([NewClaims]![Claimant Full Name],",")-1) AS LastName,
Right([NewClaims]![Claimant Full Name],InStr([NewClaims]![Claimant Full Name], ", ")+2) AS FirstName,
On some names it works perfectly
West, Krystal --becomes--> LastName = West, FirstName= Krystal
On other names, similar in formant it doesn't work
Dalton, Kathy ----> LastName = Dalton, First Name = ON, KATHY
On Names with middle initials I get
Earles, Barbara A. ----> LastName = Earles, FirstName= ARBARA A. (one missing letter)
OR
Beard, Chekitha G. ----> LastName = Beard, FirstName= KITHA G. (three missing letters)
I'm frustrated. Can anyone offer another idea on how to make this work? I seem to have the last name down, but I can't get the first name to be consistently correct.
Try this. But I'm assuming that there's always a comma that separates last name from first name.
select
txt,
LastName = left(txt,charindex(',',txt)-1),
FirstName = ltrim(right(txt,len(txt)-charindex(',',txt)))
from (
select 'West, Krystal' as txt union all
select 'Dalton, Kathy' union all
select 'Earles, Barbara A.' union all
select 'Beard, Chekitha G.'
) x
Your mistake was that when using right to extract first name, you didn't take the length of the string under consideration.

Two oracle db sets of data. How do I select data that is in one set and not in the other and vice versa

I have these two subqueries that I got working
(
SELECT GLOBAL_USERS.ID AS USER_ID, GLOBAL_USERS.USER_ID AS USERNAME,
GLOBAL_USERS.DEPARTMENT AS DEPARTMENT,
GLOBAL_USERS.FIRST_NAME AS FIRSTNAME, GLOBAL_USERS.LAST_NAME AS LASTNAME,
GLOBAL_USERS.TITLE AS TITLE,
USER_FIRST_ENTITLEMENTS.ENTITLEMENT_NAME AS ENTITLEMENTNAME,
USER_FIRST_ENTITLEMENTS.APPLICATION_NAME AS APPLICATIONNAME
FROM GLOBAL_USERS
INNER JOIN USER_FIRST_ENTITLEMENTS
ON GLOBAL_USERS.ID=USER_FIRST_ENTITLEMENTS.USER_ID AND
(USER_FIRST_ENTITLEMENTS.APPLICATION_NAME='MY APPLICATION NAME'
AND USER_FIRST_ENTITLEMENTS.ENTITLEMENT_NAME LIKE '%\fis\%')
) join1
(
SELECT DISTINCT injoin1.ID,injoin2.APPLICATION_ROLE_ID, injoin2.NAME FROM
(
SELECT GLOBAL_USERS.ID,USER_SECOND_ENTITLEMENTS.APPLICATION_ROLE_ID
FROM GLOBAL_USERS
INNER JOIN USER_SECOND_ENTITLEMENTS
ON GLOBAL_USERS.ID=USER_SECOND_ENTITLEMENTS.USER_ID
) injoin1,
(
SELECT USER_SECOND_ENTITLEMENTS.APPLICATION_ROLE_ID,SECOND_ENTITLEMENT_DEFINITIONS.NAME
FROM USER_SECOND_ENTITLEMENTS
INNER JOIN SECOND_ENTITLEMENT_DEFINITIONS
ON USER_SECOND_ENTITLEMENTS.APPLICATION_ROLE_ID=SECOND_ENTITLEMENT_DEFINITIONS.ID
) injoin2
WHERE injoin1.APPLICATION_ROLE_ID=injoin2.APPLICATION_ROLE_ID
) join2
essentially, what I need to do now is see if every join1.ENTITLEMENTNAME for each join1.USER_ID exists for that same join2.ID in join2.NAME. If it doesn't exist then I need to have a row that says join1.USERNAME join1.DEPARTMENT, join1.FIRSTNAME, join1.LASTNAME, join1.TITLE, join1.ENTITLEMENTNAME, join1.APPLICATIONNAME
I, at the same time, need to see if every join2.NAME for each join2.ID exists for that same join1.USER_ID in join1.ENTITLEMENTNAME
I am a little lost on how to do this, but I am sure it is with some type of join.
Notes: comparing join1.ENTITLEMENTNAME's with join2.NAME's and the userids are in join1.USER_ID and join2.ID.
I'm not worried about efficiency or speed, I just need functionality, so a simple answer will suffice.
Extra brownie points if you help me with the regex. The join2.NAME's are stored as themselves, but the join1.ENTITLEMENTNAME's are stored as "/fis/" I'm not exactly sure how to filter on that so it would be helpful if anyone could explain.
Thanks in advance!
Have you looked into Oracle's MINUS command? It is like the inverse of a UNION. It subtracts similarities between two queries so that the result set is the difference between the two. It sounds like it's exactly what you need. Just take your first set and MINUS the second set.
I think the below code is what you described, but you might need to modify it to fit your needs exactly.
SELECT
GLOBAL_USERS.ID AS USER_ID,
GLOBAL_USERS.USER_ID AS USERNAME,
GLOBAL_USERS.DEPARTMENT AS DEPARTMENT,
GLOBAL_USERS.FIRST_NAME AS FIRSTNAME,
GLOBAL_USERS.LAST_NAME AS LASTNAME,
GLOBAL_USERS.TITLE AS TITLE,
USER_FIRST_ENTITLEMENTS.ENTITLEMENT_NAME AS ENTITLEMENTNAME,
USER_FIRST_ENTITLEMENTS.APPLICATION_NAME AS APPLICATIONNAME
FROM GLOBAL_USERS
INNER JOIN USER_FIRST_ENTITLEMENTS
ON GLOBAL_USERS.ID=USER_FIRST_ENTITLEMENTS.USER_ID
AND ( USER_FIRST_ENTITLEMENTS.APPLICATION_NAME='MY APPLICATION NAME'
AND USER_FIRST_ENTITLEMENTS.ENTITLEMENT_NAME LIKE '%\fis\%')
MINUS
SELECT
GLOBAL_USERS.ID AS USER_ID,
GLOBAL_USERS.USER_ID AS USERNAME,
GLOBAL_USERS.DEPARTMENT AS DEPARTMENT,
GLOBAL_USERS.FIRST_NAME AS FIRSTNAME,
GLOBAL_USERS.LAST_NAME AS LASTNAME,
GLOBAL_USERS.TITLE AS TITLE,
USER_SECOND_ENTITLEMENTS.ENTITLEMENT_NAME AS ENTITLEMENTNAME,
USER_SECOND_ENTITLEMENTS.APPLICATION_NAME AS APPLICATIONNAME
FROM GLOBAL_USERS
INNER JOIN USER_SECOND_ENTITLEMENTS
ON GLOBAL_USERS.ID=USER_SECOND_ENTITLEMENTS.USER_ID
AND ( USER_SECOND_ENTITLEMENTS.APPLICATION_NAME='MY APPLICATION NAME'
AND USER_SECOND_ENTITLEMENTS.ENTITLEMENT_NAME LIKE '%\fis\%')
As far as the regex goes, '%\/fis\/%' should do the trick. You needed to escape the '/'.

How to match two fields in an SQL query?

Please correct the query.
It is working but it gives the wrong result, I have database tables that look like this.
[3rdi_EventsRolePrice] :-EventID, RoleID, RolePrice
[3rdi_EventsRolePrice]:- FirstName, LaastName And EventID
I want to get FirstName, LastName, RoleID by joining these two, and I am passing an event value as a parameter which is 13 in my case just for getting result.
SELECT ep.FirstName, ep.LastName, erp.RoleID
from [3rdi_EventParticipants] ep,[3rdi_EventsRolePrice] erp
WHERE ep.EventID==erp.EventID and erp.EventID='13'
I want to match where these two things "ep.EventID==erp.EventID" are equal, and their value is also 13. My query is also working syntaxically correct but I get a thoroughly wrong result.
SELECT
ep.FirstName,
ep.LastName,
erp.RoleID
FROM [3rdi_EventParticipants] ep
INNER JOIN [3rdi_EventsRolePrice] erp
ON ep.EventID = erp.EventID
WHERE erp.EventID='13'
I think it will work..
ep.EventID==erp.EventID to ep.EventID=erp.EventID