Can I use SQL CASE statement inside GoogleSheets QUERY? - sql

How can I (or is it possible?) use something like CASE statement in T-SQL inside QUERY in Google spreadsheet?
I want to do something like this:
=QUERY(A6:AI,"select A,(CASE WHEN D>2.5 THEN 'Yes' ELSE 'No' END),E,C,J")
I want basically a custom column with Yes/No values based on other column. Is it possible?
EDIT: To better explain, I have data in table Existing table and I would like to transform it to the Transformed table using QUERY statement:
So I need something to say: if column D is empty, print No, otherwise print Yes. This has to be in the QUERY because it's not the last column, there will be more data after column Finished. So I have this:
=QUERY(A4:D,"Select A, B, (CASE WHEN D='' THEN 'No' ELSE 'Yes' END)") - But that doesn't work
Thank you for help,

CASE & THEN are not supported in google's query language
try:
=INDEX({A6:A, IF(D6:D>2.5, "yes", "no"), E6:E, C6:C, J6:J})

Related

SQL computed column based on a special character on another column

I am not good with SQL at all, barely have an idea on how to do basic scripts suck as delete, drop, add.
I have this data with about 12 columns, I want to add a calculated column which will change depending if a special character shows up in another column.
lets say
A C
Money$ YES
Money NO
that is the idea, I want to create a column C where it says yes if there is a $ sign on the column A. Is this possible? I am assuming you can use something similar to an if condition but I have no experience with SQL scripting.
You would use a case expression and like:
select t.*,
(case when a like '%$%' then 'YES' else 'NO' end) as c
from t;
The following is just commentary.
This is very basic syntax for SQL. I would recommend that you spend some time to learn the basics. Learning-as-you-go is an okay approach -- assuming you have some fundamentals to build on. Otherwise, you are likely to spend a lot of time to learn a few things, and you may not learn the best way to do things.
yes, this is possible. you'll have to replace the parts in braces ({}) with the appropriate object names. I also use a bit rather than 'Yes'/'No'; as that seems better suited:
ALTER TABLE {YourTable} ADD {New Column Name} AS CONVERT(bit, CASE WHEN {Column} LIKE '%$%' THEN 1 ELSE 0 END) PERSISTED;
Note that this will return 0 if the column ({Column}) has a value of NULL, not NULL; unsure if this is the correct logic however, this should be more than enough to get the ball rolling. If not, read up on the CASE expression and NULL logic.
Regexp match can help you find out if there is a character you consider as special char in the strings:
SELECT
ColumnA
, SUBSTRING(ColumnA, PATINDEX('%[^ a-zA-Z0-9]%', ColumnA), 1) AS FirstSpecialChar
WHERE
ColumnA LIKE '%[^ a-zA-Z0-9]%'
;
The pattern [^ a-zA-Z0-9] will match on any character which is not a number, a space or an alphabetic character (note the ^ at the beginning of the character group - that mean NOT)
You can use regex to check any special character in column EX:
SQL SERVER
SELECT CASE WHEN 'ABCD$' Like '%[^a-zA-Z0-9]%' 1 THEN 'YES' ELSE 'NO' END as result
MYSQL
SELECT CASE WHEN 'ABCD$' REGEXP '[^a-zA-Z0-9]' = 1 THEN 'YES' ELSE 'NO' END as result
Regex can be changed as per the requirement
REGEXP '[^[:alnum:]]'

How can I compare two columns for similarity in SQL Server?

I have one column that called 'message' and includes several data such as fund_no, detail, keywords. This column is in table called 'trackemails'.
I have another table, called 'sendemails' that has a column called 'Fund_no'.
I want to retrieve all data from 'trackemail' table that the column 'message' contains characters same as 'Fund_no' in 'trackemails' Table.
I think If I want to check the equality, I would write this code:
select
case when t.message=ts.fund_no then 1 else 0 end
from trackemails t, sendemails s
But, I do want something like below code:
select
case when t.message LIKE ts.fund_no then 1 else 0 end
from trackemails t, sendemails s
I would be appreciate any advice to how to do this:
SELECT *
FROM trackemails tr
INNER JOIN sendemail se on tr.Message like '%' + se.Fund_No + '%'
Dear Check SQL CHARINDEX() Function. This function finds a string in another string and returns int for the position they match. Like
SELECT CHARINDEX('ha','Elham')
-- Returns: 3
And as you need:
SELECT *
,(SELECT *
FROM sendemail
WHERE CHARINDEX(trackemails.Message,sendemail.Fund_No)>0 )
FROM trackemails
For more information, If you want something much better for greater purposes, you can use Fuzzy Lookup Component in SSDT SSIS. This Component gives you a new column in the output which shows the Percentages of similarity of two values in two columns.

Adding a "calculated field" in bigquery

I have a raw table in bigquery, and I want to add a "conditional"/"calculated" column based on a given value, saying something like:
CASE
WHEN `columnA`="valueX" THEN `columnB`
ELSE NULL
AS `valueX`
The problem is that I can't just say SELECT *, CASE..., since then I must do GROUP BY over all of the columns.
Is there a simple way to create a "calculated" column like that ? Maybe using some UDF and views "tricks" ?
Input example:
Output example:
Thanks In advance,
Shushu
SELECT *,
CASE
WHEN columnA='valueX' THEN columnB
ELSE NULL
END AS valueX
FROM

SQL - Conditionally joining two columns in same table into one

I am working with a table that contains two versions of stored information. To simplify it, one column contains the old description of a file run while another column contains the updated standard for displaying ran files. It gets more complicated in that the older column can have multiple standards within itself. The table:
Old Column New Column
Desc: LGX/101/rpt null
null Home
Print: LGX/234/rpt null
null Print
null Page
I need to combine the two columns into one, but I also need to delete the "Print: " and "Desc: " string from the beginning of the old column values. Any suggestions? Let me know if/when I'm forgetting something you need to know!
(I am writing in Cache SQL, but I'd just like a general approach to my problem, I can figure out the specifics past that.)
EDIT: the condition is that if substr(oldcol,1,5) = 'desc: ' then substr(oldcol,6)
else if substr(oldcol,1,6) = 'print: ' then substr(oldcol,7) etc. So as to take out the "desc: " and the "print: " to sanitize the data somewhat.
EDIT2: I want to make the table look like this:
Col
LGX/101/rpt
Home
LGX/234/rpt
Print
Page
It's difficult to understand what you are looking for exactly. Does the above represent before/after, or both columns that need combining/merging.
My guess is that COALESCE might be able to help you. It takes a bunch of parameters and returns the first non NULL.
It looks like you're wanting to grab values from new if old is NULL and old if new is null. To do that you can use a case statement in your SQL. I know CASE statements are supported by MySQL, I'm not sure if they'll help you here.
SELECT (CASE WHEN old_col IS NULL THEN new_col ELSE old_col END) as val FROM table_name
This will grab new_col if old_col is NULL, otherwise it will grab old_col.
You can remove the Print: and Desc: by using a combination of CharIndex and Substring functions. Here it goes
SELECT CASE WHEN CHARINDEX(':',COALESCE(OldCol,NewCol)) > 0 THEN
SUBSTRING(COALESCE(OldCol,NewCol),CHARINDEX(':',COALESCE(OldCol,NewCol))+1,8000)
ELSE
COALESCE(OldCol,NewCol)
END AS Newcolvalue
FROM [SchemaName].[TableName]
The Charindex gives the position of the character/string you are searching for.
So you get the position of ":" in the computed column(Coalesce part) and pass that value to the substring function. Then add +1 to the position which indicates the substring function to get the part after the ":". Now you have a string without "Desc:" and "Print:".
Hope this helps.

MYSQL if statement (From mysql newb)

Is it possible to get this if statement to NOT print a column if EQ_Type!='ENGINE'?
The empty column on my out put is bothering me.
select if(EQUIPMENT.EQ_Type='ENGINE',ENGINE.Capacity,'') as Capacity, ....
Thanks for your help.
No, you can't selectively include a column on a row by row basis.
No - you will have to either have a column for all rows, or omit the comun altogether. You can control exactly what is displayed by using CASE, which is basically the same as you have done using IF (maybe a tad more self-explanatory):
select case when equipment.eq_type = 'ENGINE' then
engine.capacity
else
'put something you want here'
end as capacity
from...