I am working in datastage and I am trying to input data from one column to another - sql

I used the following statement below:
Trim(IF FromDataSource.PID_VALID = 'Y' THEN FromDataSource.Person_ID ELSE #NULL)

Assuming you are using this code within a Transformer stage in DataStage
this will help
IF Trim(FromDataSource.PID_VALID) = 'Y' THEN Trim(FromDataSource.Person_ID) ELSE #NULL
Hint:
For you next question you might ask in this forum you should provide more details - do not let us guess. Also describe what you have tried and what error you got etc.

You can use case expression :
(CASE WHEN FromDataSource.PID_VALID = 'Y' THEN TRIM(FromDataSource.Person_ID) END)
else will return null if condition evaluate as false, you don't need to specify null.

If you want to set null if PID is not valid then->
IF FromDataSource.PID_VALID = 'Y' THEN trim(FromDataSource.Person_ID) ELSE setnull()
If you want to set empty when PID is not valid then->
IF FromDataSource.PID_VALID = 'Y' THEN trim(FromDataSource.Person_ID) ELSE ''

Related

Create multiple case statements in SAP HANA

I'm trying to understand how work the expression in SAP HANA.
I want to create multiple case in one expression.
I have this:
case when (case when "name" = 'NomEntreprise'
then "value"
end) = 'Entreprise Test'
Then 'OK'
end
But when I'm doing this, it doesn't accept the syntax of the code, someone can explain me why ?
case when (case when "name" = 'NomEntreprise'
then "value"
end) = 'Entreprise FAIL'
Then 'FAIL'
end
Thank you for you help

SQL case expression in UPDATE statement

I'm trying to map this particular SQL code for data warehousing purpose.
I have two columns (TARGET) and (NET_SALARY), purpose is to map NET_SALARY with 0 when TARGET is 700, in other cases sub-string net salary 1, 30
I'm receiving missing right parenthesis error
Both columns are varchar2 datatype
CASE
WHEN SRC_CUSTOMER.TARGET = '700' THEN SRC_CUSTOMER.NET_SALARY = '0'
ELSE SUBSTR(SRC_CUSTOMER.NET_SALARY,1,30)
END
If this is in a context of an ODI mapping/interface, you can only use SQL and not PL/SQL. You can't assign the value to SRC_CUSTOMER.NET_SALARY in the first THEN. You actually only need to set the value you want and it will be mapped to your target attribute.
Try with
CASE
WHEN SRC_CUSTOMER.TARGET = '700' THEN '0'
ELSE SUBSTR(SRC_CUSTOMER.NET_SALARY,1,30)
END
Move assignment before case keyword and put SRC_CUSTOMER.TARGET as the inner expression:
SRC_CUSTOMER.NET_SALARY =
CASE SRC_CUSTOMER.TARGET
WHEN '700' THEN '0'
ELSE SUBSTR(SRC_CUSTOMER.NET_SALARY,1,30)
END
Can be rewritten with decode function:
SRC_CUSTOMER.NET_SALARY = decode(SRC_CUSTOMER.TARGET,'700','0',SUBSTR(SRC_CUSTOMER.NET_SALARY,1,30))

how to prevent converting the text into boolean by the use of when statement in postgresql?

select fti.pa_serial_,fti.homeownerm_name,fti.ward_,fti.villagetole,fti.status,
ftrq.date_reporting, ftrq.name_of_recorder_reporting,
case
when fti.status='terminate' then ftrq.is_the_site_cleared ='1' end as is_the_site_cleared from fti join ftrq on ftrq.fulcrum_parent_id = fti.fulcrum_id
Here, is_the_site_cleared is text type of column which is converted into boolean by the when statement written and hence does not print as 1 and takes as true. I explicitly used print '1'. But this also did not work. My aim is to display '1' in the column 'is_the_site_cleared' when the value of fti.status='terminate'. Please help!!!
How about using integers rather than booleans?
select fti.pa_serial_, fti.homeownerm_name, fti.ward_,
fti.villagetole, fti.status, ftrq.date_reporting,
ftrq.name_of_recorder_reporting,
(case when fti.status = 'terminate' -- and ftrq.is_the_site_cleared = '1'
then 1 else 0
end) as is_the_site_cleared
from fti join
ftrq
on ftrq.fulcrum_parent_id = fti.fulcrum_id ;
From the description, I cannot tell if you want to include the condition ftrq.is_the_site_cleared = '1' in the when condition. But the idea is to have the then and else return numbers if that is what you want to see.

CASE in WHERE clause returns Error

I'm running this code:
SELECT hID
FROM logonsHistory
WHERE aIDs NOT LIKE '%''101''%' AND
CASE src
WHEN 0 THEN
uID IN(29,41,42,45,49,50,57,73,83,107,166,349,356,367,375,376,416,471,472,473,474,481)
END
I get this error:
Incorrect syntax near the keyword 'IN'
I have no idea what's wrong.
A CASE statement is not appropriate in this case. Just use a simple OR condition:
SELECT hID
FROM logonsHistory
WHERE aIDs NOT LIKE '%''101''%'
AND (
src <> 0 -- add a COALESCE here if src can be NULL
OR uID IN(29,41,42,45,49,50,57,73,83,107,166,349,356,367,375,376,416,471,472,473,474,481)
)
... which basically is the equivalent of only applying the uID filtering if src = 0, which is what you appeared to be trying to accomplish with your query.
It sounds like you don't have to use CASE please test this
SELECT hID
FROM logonsHistory
WHERE aIDs NOT LIKE '%''101''%'
AND ( (src = 0 --scr is 0 => must have uid in the list
AND UID IN (29,41,42,45,49,50,57,73,83,107,166,349,356,367,375,376,416,471,472,473,474,481))
OR src <> 0) --else src is not 0 and there is no additional condition
i have a doubt on your not like condition have you tested it alone?
Guess this is what you are looking for
WHERE aIDs NOT LIKE '%''101''%' or
(
src = 0
AND
uID IN(29,41,42,45,49,50,57,73,83,107,166,349,356,367,375,376,416,471,472,473,474,481
)
It looks like your statement is not formatted properly. you placed a condition rather than a value to set within "When 0 then ...... END
the uID IN(29,41,42,45,49,50,57,73,83,107,166,349,356,367,375,376,416,471,472,473,474,481) you have there is a condition and shouldn't have been there
The when part of a case statement should select a single value. what you are trying to do is to check for a condition.
Or if you are checking for uid in those values, you should do
case when src = 0 then
case when uID IN (29,41,42,45,49,50,57,73,83,107,166,349,356,367,375,376,416,471,472,473,474,481)
then 'It is a Client ID'
-- add another when or else part here if required
end
else 'Not a UID and not a Client ID'
end

Using a CASE statement in HQL select

Is there any way to do the following in HQL:
SELECT
case when flag = true then SUM(col1) else SUM(col2)
FROM
myTable
I guess you can (3.6, 4.3) [inline edit] ...for where-clauses:
"Simple" case, case ... when ... then ... else ... end, and "searched" case, case when ... then ... else ... end
Apparently the ability to do this was added in 3.0.4, with the limitation that you cannot use sub-selects in the else clause.
See Hibernate-Forum: https://forum.hibernate.org/viewtopic.php?t=942197
Answer from Team (Gavin):
case is supported in the where clause, but not in the select clause in HB3.
And seen in JIRA with State "Unresolved".
Below you can find a working query (hibernate on postgresql) that uses 2 case statements to replace a boolean value with the corresponding textual representation.
SELECT
CASE ps.open WHEN true THEN 'OPEN'
else 'CLOSED' END,
CASE ps.full WHEN true THEN 'FULL'
else 'FREE' END,
ps.availableCapacity
FROM ParkingState as ps
I facing the same problem in HQL then I solved the following query is
select CONCAT(event.address1,', ', CASE WHEN event.address2 IS NULL THEN '' ELSE concat(event.address2,', ') END, event.city from EventDetail event where event.startDate>=:startDate and event.endDate<=:endDate;
We use hibernate HQL query extensively and I think finally there is a hackish way of doing such a thing :
Assuming we originally had a query of
i2.element.id = :someId
Then decided to expand this to be something like this:
((i.element.id = :someId and i2.element.id=:someId) or (i2.element.id = :someId))
But there was an issue where we want it to only lookup for this based on classType so a case statement:
(case when type(i)=Item then
((i.element.id = :someId and i2.element.id=:someId) or (i2.element.id = :someId))
else
i.element.id = :someId
end)
Above will not work you could make an easy version of above work by doing:
(case when type(i)=Item then
i2.element.id
else
i.element.id
end)=:elementId
But this does not actually do what we need it to do, we want it to do exact above query, so knowing you can assign a variable at the end of a case statement in there where bit of HQL:
(
(
(case when
type(r)=Item then
i.element.id
else
i.element.id end) = :elementId
and
(case when
type(r)=Item then
i2.element.id
else
i.element.id end) = :elementId
)
or
(case when
type(r)=Item then
i2.element.id
else
i.element.id end) = :elementId
)
I have managed to make the query now work based on case statement, sure it is a lot more long winded but actually does the same as the first instance
This is an example using a string comparison in the condition:
SELECT CASE f.type WHEN 'REMOVE'
THEN f.previousLocation
ELSE f.currentLocation
END
FROM FileOperation f