PostgreSQL - Converting a string with quotes to different string using case? - sql

So hopefully this isn't a super difficult question. I've looked around but haven't been able to find an answer. Basically I have a table in a READ-ONLY db that I'm trying to use a case statement to convert a column that stores JSON values (I think?) to something more visually pleasing.
The table shows Emails, and the Status of whether or not they're subscribed to the mailing list.
EXAMPLE HERE
Basically my query looks something like this
select
f.data as "Email",
(
case f.status
when '{"value":true}' then 'Yes'
when '{"value":false}' then 'No'
else NULL
end
) as "Subscribed"
from fields f
When I run this in my example page it works just fine when set to POSTGRES 11 but when I run it on Metabase, I get an error "ERROR: operator does not exist: json = unknown" and I'm stumped on how to proceed.
Any help here would be greatly appreciated.

That error means f.status is of type json and you're trying to compare it to a string, which doesn't work.
You can try the following instead (related documentation):
case f.status->>'value'
when 'true' then 'Yes'
when 'false' then 'No'
end
as "Subscribed"

Related

SSRS: CASE WHEN NULL

I'm trying to import data from a table into my SQL Report Builder report.
In this particular column, the data will either be someone's name or "NULL".
I want to set my field to change NULL to "Other", but leave it how it is if it contains a name.
I know I must be close with what I have below, but I can't figure out how to get it to not alter the value if it's NOT NULL:
CASE WHEN ([Reviewed_By] IS NULL) THEN 'Other' ELSE '' END AS [Reviewed_By]
Obviously, with how it's written here, it will convert any name to a blank but I can't figure out the correct logic to get it to "skip" the line-item if it's a valid name.
Any help is appreciated!
Let me know if you need any other information.
Thanks in advance,
Cameron
you could just do ...
SELECT
ISNULL([Reviewed_By], 'Other') AS [Reviewed_By]
FROM myTable
To answer your question for the SQL side.
CASE WHEN [Reviewed_By] IS NULL THEN 'Other' ELSE [Reviewed_By] END AS [Reviewed_By]
Report builder has functionality to do this as well with expressions.
You can read more here 32716829/if-value-null-then-else-value-ssrs-expression-issues.

SQL Error (4145): An expression of non-boolean type specified in a context where condition is expected, near '('

Using the below statement to check whether a field is null and if it is null then I need to show "Not Yet Approved" message. Otherwise I would like to get a concatenated result of some fields which is mentioned below.
select
iif(isnull(AppByENo1,'true'),'Not Yet Approved',AppByENo1+AppByDesg1+AppByDate1) as result
from myDB
where (E_No = '25')
But getting the above mentioned error while trying to run the sql query.
Please advice where I am making mistake and how to tackle this issue.
try this:
select
iif(isnull(AppByENo1,'true') = 'true','Not Yet Approved',AppByENo1+AppByDesg1+AppByDate1) as result
from myDB
where (E_No = '25')
Use CASE Statement in SELECT clause instead of IFF :
SELECT CASE WHEN ISNULL(AppByENo1,'true') = 'true' THEN 'Not Yet Approved'
ELSE AppByENo1+AppByDesg1+AppByDate1 END as result
FROM myDB WHERE E_No = '25'
Since concatenating null values yields null (providing you're leaving appropiate settings turned on, which you should), you can just use a simple coalesce to replace the null if necessary:
select
COALESCE(AppByENo1+AppByDesg1+AppByDate1,'Not Yet Approved') as result
from myDB
where (E_No = '25')
If you insist on the iif approach, I'd favour:
iif(AppByENo1 is null,'Not Yet Approved',AppByENo1+AppByDesg1+AppByDate1)
E.g. an actual null test, rather than trying to identify a sentinel string that cannot appear naturally and performing a string comparison after isnull.

Recall stored case in TSQL

I have a set of cases that I frequently use to categorize query results and rather than copy/pasting them from a saved file every time I'm hoping I can turn this simplified version of what I use..
CASE
WHEN source = 'x' AND othersource = 'y' THEN 'region'
WHEN subsource = 'm' AND othersubsource = 'n' THEN 'district'
WHEN littlesource = 'a' AND otherlittlesource = 'b' THEN 'office'
ELSE 'ERROR - LOOK AT ME'
END AS "Service Channel"
Into
#RDOCASE
Where #RDOCASE is a stored, short-form version of the full case I've listed above. I'm thinking it would be like declaring a variable at the beginning of a query except it's stored somewhere and can be recalled at any time in any query being run on this database.
You should let a computed column do the work for you . . . assuming that the columns all come from the same table:
ALTER TABLE t ADD ServiceChannel AS (<your case here>)
If the values come from multiple tables, then you can use a view for this purpose. Or perhaps a scalar function, if you want to pass in the relevant variables.
On a side note: don't use single quotes for column aliases. This is just confusing. Single quotes should be used for string and date constants.

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.

Can I place oracle statements inside of if else statement of sql

Problem while placing oracle query inside of the oracle if else statements
select workgroupid, maxchats,
case
when a.maxchats ='-1' then
(select propvalue from ofproperty where name = 'xmpp.live.defaults.maxchats')
else
null (1)
end as aaaa
from fpworkgroup a;
when im placing maxchats at 1 position im getting error,,
how can i resolve this....?
help me..
thanks in advance
It's hard to understand your question but it sounds like you're trying to put maxchats in place of null (1) and it gives you an error?
If that is correct, I'm guessing that the error is that fpworkgroup.maxchats and ofproperty.propvalue have different column types that do not line up. All branches of a CASE must return the same column type. You cannot, for example, return an int from one branch and return a varchar from another.
You'll have to cast one or the other so that they return the same type.