How to optimally find all fields that contain an email address - sql

I would like to return the column names for all columns containing an email address (... LIKE '%#%.%'). Is there a way to search fields for emails without doing it exhaustively? That is as soon as one email instance is found in the field the next is searched as oppose to looking at all its values? This is to scale better to large databases.
In the case below 'Foo' and 'Email' should be returned without searching all their records.

You could use conditional aggregation here:
SELECT
CASE WHEN SUM(Foo LIKE '%#%.%') > 0 THEN 'YES' ELSE 'NO' END AS Foo,
CASE WHEN SUM(Bar LIKE '%#%.%') > 0 THEN 'YES' ELSE 'NO' END AS Bar,
CASE WHEN SUM(Email LIKE '%#%.%') > 0 THEN 'YES' ELSE 'NO' END AS Email,
CASE WHEN SUM(Name LIKE '%#%.%') > 0 THEN 'YES' ELSE 'NO' END AS Name
FROM yourTable;

Related

How to write case condition to check particular value exist among multiple values in sql?

I have a column in table which is having single/multiple value. I have to convert it yes/no based on condition.
example:
rendition_type_sys
distribution
uploaded, distribution
uploaded
single
I need to change the value based on condition. If column having distribution then value should convert as 'Yes' otherwise 'No'
Final Output:
rendition_type_sys
Yes
Yes
No
No
I tried one case statement but that is working for single value not multiple value-
case when ren.rendition_type__sys='distribution' then 'Yes' else 'No' end as rendition_type__sys
First, you should fix your data model so you are not storing multiple values in a string.
In the meantime, like should do what you want. For your example:
(case when ren.rendition_type__sys like '%distribution%'
then 'Yes' else 'No'
end) as rendition_type__sys
Note: In case "distribution" is part of an element name and you don't want that, you can check for delimiters. In Standard SQL, this would be:
(case when ', ' || ren.rendition_type__sys || ', ' like '%, distribution, %'
then 'Yes' else 'No'
end) as rendition_type__sys
The string concatenation operator may vary depending on the database you are using.
This below code would work assuming distribution is not part of any other discrete value; here is the code in a sample form:
WITH rt AS (
SELECT 'distribution' AS rendition_type_sys
UNION SELECT 'uploaded, distribution'
UNION SELECT 'uploaded'
UNION SELECT 'single'
)
SELECT CASE WHEN rt.rendition_type_sys LIKE 'distribution' THEN 'Yes' ELSE 'No' END AS Col
FROM rt
You might consider adding a table with for these "tags" so that they are discrete.

Condition SQL Server

This is my problem: I added a column Cause to my table. This column contains different conditions (up to here, everything is fine). But since I have a lot of lines for each product, it can have 3 conditions at the same time.
What I'm trying to do is that once it finds a condition, it does not go to the one after (and it is by this order of priority).
I do not know if I was clear, but if you want more explanation do not hesitate to ask me questions
Cause = (CASE
WHEN Four IS NOT NULL THEN 'Retards'
WHEN (MAX(DateP BETWEEN '2018-10-24' AND '2018-10-14') THEN 'stock'
WHEN Reference = 0 THEN 'respecté'
WHEN Produit = 2 THEN 'non respecté'
ELSE 'Erreur'
END)
This is an example of what I want to do:
The CASE expression stops just after the first WHEN..THEN is found. If you want to concatenate labels and check all conditions, you can use multiple CASE expression.
(case when Four IS NOT NULL THEN 'Retards' ELSE '' END +
case when (MAX(DateP) between '2018-10-24' AND '2018-10-14') THEN 'stock' ELSE '' END +
case when Reference = 0 THEN 'respecté' ELSE '' END +
case when Produit = 2 THEN 'non respecté' ELSE '' END
) AS Cause

Using SQL to return 6 check box flags into three columns

I am trying to figure out how to turn multiple check box results in differnet fileds into seperate columns.
The current case statement below only tracked the lowest score into a a single filed called 'Activities Registered For (1) – (5)'. I would like to convert them into 5 columns 'a-e' where 'a' is always filled with a result, and if two options are checked the results are in 'a' and 'b'. The form can be filled in with up to all selections checked. The else statement appears to be an error, since there are to be at least one of the five boxes checked.
I am new to SQL and I adopted this from someone else, so I am sorry for not showing my previous attempts to resolve my issue.
,CASE
WHEN [1524#1] = 'Y' THEN '1'
WHEN [1525#1] = 'Y' THEN '2'
WHEN [1526#1] = 'Y' THEN '3'
WHEN [1527#1] = 'Y' THEN '4'
WHEN [1528#1] = 'Y' THEN '5'
ELSE ' ' END AS 'Activities Registered For (1) – (5)'
You could use a PIVOT but multiple CASEs are just as effective, use about the same amount of code and is easier for beginners to decipher.
CASE WHEN [1524#1] = 'Y' THEN 1 ELSE 0 END AS ACTIVITY_1,
CASE WHEN [1525#1] = 'Y' THEN 1 ELSE 0 END AS ACTIVITY_2,
CASE WHEN [1526#1] = 'Y' THEN 1 ELSE 0 END AS ACTIVITY_3,
CASE WHEN [1527#1] = 'Y' THEN 1 ELSE 0 END AS ACTIVITY_4,
CASE WHEN [1528#1] = 'Y' THEN 1 ELSE 0 END AS ACTIVITY_5
I don't think the ELSE is necessarily an error. I often add an ELSE than shouldn't be used in case there is unexpected data (no Y in any field in your example).
Here's some info on PIVOTs if you want to check it out:
http://www.codeproject.com/Tips/500811/Simple-Way-To-Use-Pivot-In-SQL-Query

How to approach the following query in oracle

How to approach the following query in oracle sql
select last_trans_date,
last_trans_date<sysdate-180 as "a",
last_trans_date<sysdate-360 as "b"
from x;
I'm guessing you want something like this:
select last_trans_date,
CASE
WHEN last_trans_date < sysdate-180 THEN 'YES'
ELSE 'NO'
END as "a",
CASE
WHEN last_trans_date < sysdate-360 THEN 'YES'
ELSE 'NO'
END as "b"
from x;
The Oracle database doesn't support a BOOLEAN type so you can't return the result of the comparison directly. Here I've arbitrarily chosen to return 'YES' and 'NO' - change it to whatever you like better/best.

How do I set values to email addresses using 'INSTR'?

I have a school assignment where I have a list of people with email addresses. There are three types of email addresses: #gmail.com, #yahoo.com, and #hotmail.com. I need to create a column that shows a 'Y' next to all the #gmail.com and #yahoo.com email addresses, and a 'N' next to everyone with #hotmail.com.
This is what I have so far: DECODE(INSTR(mail, LIKE '%gmail.com' AND '&yahoo.com'), 1, 'Y', 'N') c15
I'm not quite sure what I'm doing wrong.
select case when INSTR(mail, 'gmail.com') > 0 or INSTR(mail, 'yahoo.com') > 0
then 'y'
else 'n'
end as c15
You could use a simple CASE statement to determine the correct value (y or n),
SELECT email, CASE WHEN LIKE '%gmail.com' OR '%yahoo.com' THEN 'Y' ELSE 'N' END AS result
FROM Yourtable
Utilizing Juergen D's Answer:
select case when INSTR(mail, 'gmail.com') > 0 or INSTR(mail, 'yahoo.com') > 0
then 'y'
case when INSTR(mail, 'hotmail.com') > 0
then 'n'
else 'n'
end as c15
I believe in explicitely defining, but in the end it is the same answer. Also, if you're expecting to go into a career with IT then do some research and try things out before giving up and asking!