Jaspersoft Domain Designer: Determined what Y/N should be translated to - sql

I have an (Oracle 11g) table with a (single character) "Y/N" flag column. For purposes of simplicity, imagine we're talking about a "Person" table and the column in question is a "Gender Indicator". Without changing the structure of the DB, is there a way to manipulate my result set such that I am able to get "Male" returned for "Y" and "Female" returned for "N"? I have looked into the SQL translate() and replace() functions, but not sure how to do this for multiple rules without having to resort to PLSQL. Any advice would be appreciated.
If this isn't possible via a single , the requirement is for a Domain I am generating in Jasper Reports so any advice on how it may be done that way would help also.

SELECT CASE WHEN Gender_Indicator = 'Y' THEN 'Male' ELSE 'Female' END AS Gender_Indicator FROM MyTable;
This would give you what you are looking for from what I can tell.
You could also use DECODE(Gender_Indicator, 'Y', 'Male', 'N', 'Female')

If you keep gender indicator as Y/N, you can use the ternary if in Jasper Reports inside a text field expression to achieve your goal.
Assuming you have a field in your report (let's call it genderIndicator) that can hold values Y or N, then add a text field with the following expression:
$F{genderIndicator}.equals("Y") ? "Male" : "Female"

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.

How to extract numeric values from a column in SQL

I am trying to extract only the numeric values from a column that contains cells that are exclusively numbers, and cells that are exclusively letter values, so that I can multiply the column with another that contains only numeric values. I have tried
SELECT trim(INTENT_VOLUME)
from A
WHERE ISNUMERIC(INTENTVOLUME)
and also
SELECT trim(INTENT_VOLUME)
from A
WHERE ISNUMERIC(INTENTVOLUME) = 1
and neither works. I get the error Function ISNUMERIC(VARCHAR) does not exist. Can someone advise? Thank you!
It highly depends on DBMS.
in SqlServer you have a limited built-in features to do it, so the next query may not work with all variants of your data:
select CAST(INTENT_VOLUME AS DECIMAL(10, 4))
from A
where INTENT_VOLUME LIKE '%[0-9.-]%'
and INTENT_VOLUME NOT LIKE '%[^0-9.-]%';
In Oracle you can use regex in a normal way:
select to_number(INTENT_VOLUME)
from A
where REGEXP_LIKE(INTENT_VOLUME,'^[-+]?[0-9]+(\.[0-9]+)?$');
MySQL DBMS has also built-in regex
Try this, which tests if that text value can be cast as numeric...
select intent_volume
from a
where (intent_volume ~ '^([0-9]+[.]?[0-9]*|[.][0-9]+)$') = 't'

Compare the text of two columns

I’m working on this dataset to cleaning it
https://www.kaggle.com/heesoo37/120-years-of-olympic-history-athletes-and-results, using Dremio (an online tool) so I can use an SQL editor (but i don't know what DBMS it use).
Now I’m trying to delete from the column Event the words that are contained in the column Sport. (I’ve already done some modification indeed in the column Event I’ve deleted the occurance of the words “man’s” and “women’s”).
Attached you’ll find
The current situtation and the desired result
How can I solve the problem?
I hope I have been clear, Thank you in advance for help. :)
Edit: I've found the original query made by Dremio
SELECT ID, Name, Gender, Age, Height, Weight, Team, "Olympic Games"."Year" AS "Year", Season, City, Sport, CASE WHEN regexp_like(CASE WHEN regexp_like(Event, '.*?\QMen''s\E.*?') THEN regexp_replace(Event, '\QMen''s\E', '') ELSE Event END, '.*?\QWomen''s\E.*?') THEN regexp_replace(CASE WHEN regexp_like(Event, '.*?\QMen''s\E.*?') THEN regexp_replace(Event, '\QMen''s\E', '') ELSE Event END, '\QWomen''s\E', '') ELSE CASE WHEN regexp_like(Event, '.*?\QMen''s\E.*?') THEN regexp_replace(Event, '\QMen''s\E', '') ELSE Event END END AS Event, Medal
FROM "#Sboorn"."Olympic Games"
WHERE NOT regexp_like(ID, '.*?\QID\E.*?')
You can use a CASE to heck if event starts with the sport and a space. If so use substring() to omit the first n characters for n the length of sport and the space. Else return event unchanged.
SELECT sport,
CASE
WHEN event LIKE concatenate(sport, ' %') THEN
substring(event, length(sport) + 2, length(event) - length(sport) - 1)
ELSE
event
END event
FROM elbat;
As you didn't tag your actual DBMS, the names of the functions might differ (e.g. concat() instead of concatenate(), substr() instead of substring() or len() instead of length()). But some equivalent should be available in most DBMS.
Depending on the actual DBMS there also might be more elegant solutions, like regular expressions.
And next time please don't post images. Use CREATE TABLE and INSERT INTO statements to show how your tables look like and plain text to show the desired result.

SQL Select to keep out fields that are NULL

I am trying to connect a Filemaker DB to Firebird SQL DB in both ways import to FM and export back to Firebird DB.
So far it works using the MBS Plug-in but FM 13 Pro canot handle NULL.
That means that for example Timestamp fields that are empty (NULL) produce a "0" value.
Thats means in Time something like 01.01.1889 00:00:00.
So my idea was to simply ignore fields containing NULL.
But here my poor knowlege stops.
First I thought I can do this with WHERE, but this is ignoring whole records sets:
SELECT * FROM TABLE WHERE FIELD IS NOT NULL
Also I tried to filter it later on like this:
If (IsEmpty (MBS("SQL.GetFieldAsDateTime"; $command; "FIELD") ) = 0 ; MBS("SQL.GetFieldAsDateTime"; $command; "FIELD"))
With no result either.
This is a direct answer to halfbit's suggestion, which is correct but not for this SQL dialect. In a query to provide a replacement value when a field is NULL you need to use COALESCE(x,y). Where if X is null, Y will be used, and if Y is null then the field is NULL. Thats why it is common for me to use it like COALESCE(table.field,'') such that a constant is always outputted if table.field happens to be NULL.
select COALESCE(null,'Hello') as stackoverflow from rdb$database
You can use COALESCE() for more than two arguments, I just used two for conciseness.
I dont know the special SQL dialect, but
SELECT field1, field2, value(field, 0), ...FROM TABLE
should help you:
value gives the first argument, ie, your field if it is NOT NULL or the second argument if it is.

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.