SQL command to clear specific text - sql

Trying to run a SQL command that reads * from my database but I need it to seek specific text in the email field and return a null if it doesn't equal. Example, I'm trying to filter out all emails in our database that are not internal email addresses, so it would need to filter out any that don't have our company name in it.
I was able to filter this with a LIKE command, but it just simply ignores the rest of the fields in the results. I know access has the IIf(InStr command, so I'm hoping there is something similar in SQL

Not sure I fully understand, but something like this may work for you.
USE Server
SELECT
firstName AS EmployeeFirstname
, lastName AS EmployeeLastname
, title AS EmployeeTitle
, id AS EmployeeID
,
Case
When emailAddress Like '%#mycompany.com'
Then emailAddress
Else Null
End AS EmployeeEmailAddress
FROM db.Information
WHERE ISNUMERIC(id)<> 0
AND empStatus = 'A'
So what this will do is give you the rows regardless of what email they have (still taking into account your other selections of ISNUMERIC(id)<> 0 AND empStatus = 'A'). But in those rows, if the email is your company they would display and if not the field would be Null. Obviously change the “mycompany.com” it the Like to whatever string you actually need to search for,

If you want to exclude all records where the company is not in the email address, add
where
emailAddress not like '%companyname%'
If you want to blank out the non-company email addresses, you can use case
Select
case when emailaddress not like '%company%' then null else emailaddress end

Related

Select Records with same email in different columns

I want to extract all records that has same email in different fields from an email table. I am not sure how to do that in sql. looking for help.The image has id and email fields. I am trying to extract records which has same email in different fields.
If I understand your question correctly, you need to compare each email address in a row with every other email address in the same row, taking into account that you don't want to have empty email addresses compare equal. Here's my suggestion (which I've played around with a bit here):
select * from email
where
(email1 <> '' and email1 in (email2, email3, Add1, Add2))
or (email2 <> '' and email2 in (email3, Add1, Add2))
or (email3 <> '' and email3 in (Add1, Add2))
or (Add1 <> '' and Add1 = Add2)
Additional note: If you have superfluous spaces tagged on to any emails, you may need to trim them all.

SQL Select statement separate values into two columns

I have string value in the column Username.
Sample usernames:
USERNAME
--------
foobar123
john
smith23
steve
peter
king213
The user names with numbers at the end means that these users are no longer active. I want to separate these usernames into two columns Active and Not_Active in one Select Statement since i'll be using these for reports purposes.
Result should be:
Active Not_Active
john foobar123
steve smith23
peter king213
Query:
SELECT
Username,
(
CASE Username
WHEN '%[0-9]%' THEN 'Not'
ELSE 'Active'
END
)
FROM
Users;
I tried Case but I don't know how to get the username value.
Expanding on my comment reply to your original posting, you don't want the data in the same output result-set; instead you will want two result sets (i.e. two tables), each with the different criteria.
Note that you cannot use LIKE string matching (%) with the WHEN statement in SQL. You have to use it in a CASE WHEN statement (one without a "switch" expression)
-- Result set one: Active users
SELECT
UserName
FROM
Users
WHERE
UserName NOT LIKE '%[0-9]';
-- Result set two: Inactive users
SELECT
UserName
FROM
Users
WHERE
UserName LIKE '%[0-9]';
If you really want, you can combine these two queries into a single result-set with the data in different columns. This would be done by adding a ROW_NUMBER() column to each intermediate table, then doing a FULL OUTER JOIN on ROW_NUMBER(), however the output result would be meaningless and painful to iterate over in any consuming client code.
Another option might be a single result-set, with a computed IsActive column:
SELECT
UserName,
( CASE WHEN UserName NOT LIKE '%[0-9]' THEN 1 ELSE 0 END ) AS IsActive
FROM
Users
...which would be considerably easier to process in any consuming code.
this format of saving data is quite useful in this one column holds username and other holds status of the user
select username,
case username
when '%[0-9]% then inactive
else active
end as user_status
from table_1
username user_status
john active
john123 inactive

Trying to write a SQL expression that drops a collection of rows

I've indexed my email inbox and have a table of email_addresses, thread_id, subject, to, from...etc
I would like to "group" (but group by doesn't work) the rows by email address, and then eliminate that collection of rows if any row inside that collection contains a certain email address inside the to field.
I've tried:
select *
from emails
where emails.[to] != "test#email_address.com"
But that just eliminates individual rows, and not the collection of rows that share an email address.
Unfortunately using group by loses all information beyond the top level row and the database no longer can check to see if there are other rows with that email address in the to field.
Any thoughts?
You need a group by on email_address and a conditional check for any of the to email specified.
select email_address
from emails
group by email_address
having count(case when emails.[to] = 'test#email_address.com' then 1 end) = 0
If you need the entire row information for such email_addresses use
select * from emails where email_address in (
select email_address
from emails
group by email_address
having count(case when emails.[to] = 'test#email_address.com' then 1 end) = 0)

Company name pattern Given by user should not match with some key words

At the time of company registration given by user should not match list of names stored in the one table.
For example user input is:
MYINDIALTD
But INDIA is a restricted name stored in a table, so we need to raise an exception.
I tried like, but it matched exact only, but if company name just contained the word, it did not work.
This query will return true if the input contains a restricted name, false otherwise:
select exists (
select *
from restricted_names
where ? like concat('%', name, '%')
)
Obviously, you would substitute the ? with your input, eg MYINDIA LTD.
If you need the reverse of the boolean result, select not exists ... instead.

SQL SERVER Query Select by FirstName, Lastname

My Products table has almost 10,000 records where i want the "artist" ( varchar ) column name to sort by Firstname and Lastname. In the where clause i am sending initial letter of either first name or lastname. This Query works for few records but not for all, its probably because of the pattern of the records ?
Here is my query
SELECT * FROM Products WHERE SUBSTRING(artist, 1, 1) Like 'C'
Doesnt return the records... the way i want it ...Few of the records of the table ( artist) column are below
Comtesse Mathilde duMonceau de Bergendael
Mulholland, S.J.
Cleminson
NULL
Samuel Jackson
Your LIKE clause is wrong. Also you should not be using LIKE at all here, as you're only filtering by one letter:
.... WHERE LEFT(artist, 1) = 'C'
EDIT LEFT is actually faster than SUBSTRING.
Also, you can not expect anything that's not starting with a C to be returned, as you're explicitly only asking for entries that start with a C.
I guess what you really need is the following:
.... WHERE LEFT(FirstName, 1) = 'C' OR LEFT(LastName, 1) = 'C' ORDER BY LastName, Firstname
If you only have the Artist column, things get complicated, as you actually do need to use LIKE, which makes things pretty slow
.... WHERE Artist LIKE `%C%`
returns any artist, the name of which contains a C anywhere in the name. This would also return these:
Clemens Test
Test Cutler
Arachnophobia
User this where clause
where left(artist, 1) = 'C'
or
where artist like 'C%'