Using SQL to return 6 check box flags into three columns - sql

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

Related

How to optimally find all fields that contain an email address

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;

Case statement in where clause using not like and is not null

I'm pulling data from an existing table using a stored procedure that has some yes or no choices that the user picks on the front end through a checkbox. I want to limit writing a bunch of different If statements for every choice they make.
This portion of my where clause works. Data is either Y or N for this column.
Where... and IsSigned = Case When #IncludeSigned = 'Y' then IsSigned else 'N' end
I would like to add to the where using is not null and not like if this is possible between the square brackets. So far I have
and SignatureType = case when #IncludeElectronic = 'Y' then Type else [NOT like electronic] end
also
and ReviewDate = Case When #HasReviewDate = 'Y' then [ReviewDate is not null] else null end
This may help you use AND/OR instead of case
where (ReviewDate is not null or #HasReviewDate = 'Y' ) And (....)
ie when #HasReviewDate = 'Y' query will return the records with ReviewDate is not null
and when #HasReviewDate != 'Y' then query will return the records with ReviewDate is null
think of it this way:-
Your first case statement has two possible results:-
IsSigned = 'Y'
IsSigned = 'N'
Your subsequent ones have problems as they don't make sense syntactically. So the second one as written returns
SignatureType = Type
SignatureType = [NOT like electronic]
and your third:
ReviewDate = [ReviewDate is not null]
ReviewDate = null end
SO the operator has to be before the case statement and apply to all of the results of the case statement.
For example
WHERE myfield not like CASE WHEN thatfield=1 THEN 'Fish' ELSE 'Chips END
would produce either
myfield not like 'Fish'
myfield not like 'Chips'
I believe you can not use in this way, apart of that, the impact that is not like can have inside your query can be high, my recommendation changes the strategy that you are using.

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

sql records validation in a table

I like to check the length of a specified fields is x length and available value is a numeric value
i can do this by using two separate case statements like below
(case when len(columnA) = 10 then 0 else 1 end)+
(case when IsNumeric(columnA) = 1 then 0 else 1 end)+
(case when len(columnB) = 8 then 0 else 1 end)+
(case when IsNumeric(columnB) = 1 then 0 else 1 end)
is there any better approach as i need to this for more than 40 columns and their datatype is varchar and each of the column will have specific length.
using some short cut to reduce above two case statements into one line
If you only care that a column is invalid and are less interested in which criteria it failed on then you could just put both criteria into the one case statement as
(case when len(columnA) = 10 and IsNumeric(columnA) = 1 then 0 else 1 end)
In regards to the issue Sean raised, ISNUMERIC only confirms the value can be converted to a numeric datatype so commas and periods are valid too. you could do a check for any single character that isn't in the range of numbers
case when len(columnA) = 10 and ColumnA not like '%[^0-9]%' then 0 else 1 end
It is a little ugly because we have to say is not [not in range], so you might want to change the logic a bit.

Why does this case statement not work?

Why does this case statement not work? My table is defined as below. I want the case to return true when M = "M" or false if it is not. Something does not seem to look right with this CASE statement but this is the simple case statement. But I thought that could be as many WHEN values as needed but in this cases there is only True or False and it does not appear that there could be anything different.
The input should be all of the M column in the row. I am expecting the output to be one row since I only have one row that has an M value in the column. I am not certain if the case statement should return false in this case.
CASE M = "M <-- is this is what the criteria is based on
I found that this is not the case however from the post below. When i meant that this did not look right I was referring two the fact that the WHEN portion of the code
WHEN true THEN something
just did not look like it normally does.
Code:
select CASE M = "M"
WHEN true THEN "PPP"
WHEN false THEN "False -- Look Again!"
ELSE "Not Found"
END
from Bat;
Image:
New Code:
select *,
CASE
WHEN M = 'M' THEN
Case
when O = 'O' then 'you found a double (MM)' <--Compiles but does not show result
End
WHEN M is null THEN 'False -- Look Again!'
END as 'What is this value'
from Bat;
That fixed the first problem but you can embed case statements N deep I would think so if i search again on O it should return a correct. But the first case returns only one record. So my second case would have to search in the record correct.
The normal way to write this is:
select (CASE WHEN M = 'M' THEN 'PPP'
WHEN M <> 'M' THEN 'False -- Look Again!'
ELSE 'Not Found'
END)
from Bat;
or:
select (CASE WHEN M is null then 'Not Found'
WHEN M = 'M' THEN 'PPP'
ELSE 'False -- Look Again!'
END)
from Bat;
If I had to guess, your version has unexpected behavior for NULL values, but it is hard to say without knowing what you expect or what it is doing.
Change it like this
select CASE
WHEN M = 'M' THEN 'PPP'
WHEN M is null THEN 'False -- Look Again!'
ELSE "Not Found"
END
from Bat;