CASE and NULLS issue - tsql

I don't use CASE very often but have a pretty simple query I would like to return a generic statement for when the value is NULL. I have written the below and do not get any errors but it doesn't catch the NULL values either. I have shortened the list of articles as it is several thousand but was hoping someone could point out what I may be doing wrong. Thank you in advance.
**USE Asag_Reporting
GO
SELECT DISTINCT ARTICLE_NUMBER,
CASE PM_NAME
WHEN NULL THEN 'No PM Available'
ELSE PM_NAME
END AS 'PM_NAME'
FROM vw_GIM_Articles
WHERE ARTICLE_NUMBER IN ('15110',
'19228',
'34563',
'36516');**

After more thinking, I would prefer this over what I wrote below.
SELECT DISTINCT ARTICLE_NUMBER,
COALESCE(PM_NAME, 'No PM Available') AS [PM_NAME]
FROM vw_GIM_Articles
WHERE ARTICLE_NUMBER IN ('15110','19228','34563','36516');
Try this.
SELECT DISTINCT ARTICLE_NUMBER,
CASE
WHEN PM_NAME IS NULL THEN 'No PM Available'
ELSE PM_NAME
END AS [PM_NAME]
FROM vw_GIM_Articles
WHERE ARTICLE_NUMBER IN ('15110',
'19228',
'34563',
'36516');

Related

Crystal formula case statement with like

I'm trying to convert sql case statement to crystal formula.
The select in sql had this in it:
...
,pf.Status_category AS Category
,CASE WHEN p.degree LIKE '%P%' AND
pf.department_name LIKE 'Occ%' THEN isnull(pf.department2, '') ELSE isnull(pf.department_name, '') END AS Department,
CASE WHEN p.degree LIKE '%P%' AND pf.department_name LIKE 'Occ%' THEN isnull(pf.Section2, '') ELSE isnull(pf.Section_name, '') END AS Section,
CASE WHEN p.degree LIKE '%P%' AND pf.department_name LIKE 'Occ%' THEN isnull(pf.department3, '') ELSE isnull(pf.department2, '') END AS [Department 2],
CASE WHEN p.degree LIKE '%P%' AND pf.department_name LIKE 'Occ%' THEN isnull(pf.Section3, '') ELSE isnull(pf.Section2, '') END AS [Section 2],
pdd.DepartmentName AS DP
,pdv.PrivilegeDetailText AS Privilages
...
FROM dbo.Person_PrivDtl_V AS pdv INNER JOIN
dbo.Person_Privs_Facs_V ON pdv.M_ID = dbo.Person_Privs_Facs_V.Person_Priv_M_ID INNER JOIN
dbo.PrivDefDepartments_PDF AS pdd ON pdd.PDDept_ID = pdv.PDDept_ID INNER JOIN
dbo.Person AS p ON pdv.Person_ID = p.Person_ID INNER JOIN
dbo.Person_Facilities AS pf ON pf.FacCode = dbo.Person_Privs_Facs_V.FacCode AND pdv.Person_ID = pf.Person_ID
When I convert this to crystal, I can't put this in the columns of the report design. So my idea is to use a formula for each column selected. I have this so far for the first formula, but it won't let me save it:
Formula name= Department:
select {Person_Facilities.Department_name}
case is like "Occ%" : {Person_Facilities.Department2}
The error seems to be the like. I looked up crystal like and it seems ok except for they use "is" for the accepted answer, but when I add "is" the error seems to be the "is" when I try to save it.
What is wrong with this formula so I can use like and the case?
Is there a better way to do this? I suppose I can use a view, but my boss doesn't want views cluttering the DB. Is using a formula the way to do this in crystal? The sql also handled null, which I'm not doing, but I'm not sure how to incorporate that at this point. I'm pretty new to crystal and my team doesn't like questions. We have Crystal Reports 2008 and SQL server 2008 R2.
Not sure if this is what you are looking for but try this;
select {Person_Facilities.Department_name}
case "Occ%" : {Person_Facilities.Department2}
default : 'Unknown';
The default is optional.
To use the LIKE operator, it is as follows;
If {Command_Main.Name} LIKE '*Manager*' then Do Something
To handle NULLS in CR, there is an option to do so in the formula editor. See below. Change it from Exception for Nulls to Default Values for Nulls.

CASE statement Conditions postgres

I am trying to create a report for employees that start with the code 10....and apply a specific job code for them. They have a bad job code in the table.How and where do I check the condition in the case
statement ?
Example condition :
Apply job_code 'AC' for the employees that start with the code 10.
Table1 : customer_test
employee_code
Job_code_id
company_code
join_year
Table 2: Company_store
Job_code_id
Job_code
Company_code
Join_year
SELECT
ct.employee_code,ct.job_code_id old,ct.company_code ,cs.job_Code_id new ,cs.job_code new code,
CASE
WHEN (ct.employee_code LIKE '10%') THEN CAST(cs.job_Code_id AS varchar) //I need to apply the condition here? apply the specific job code id based on the conditions.
ELSE CAST(cs.job_Code_id AS varchar)
END AS REPORT
FROM customer_test ct,company_store cs
WHERE ct.company_code=cs.company_code
and ct.job_code_id =cs.job_code_id
and ct.join_year=cs.join_year;
Thanks in advance.
You could try something like:
case
when employee_code like '10%' then
(
select job_code
from customer_test
where employee_code ='AC'
)
else cast(job_Code as varchar(50))
end

T-SQL where clause case statement

Need some small help with some SQL. I am using a VARCHAR value to determine in a case which logic to use in WHERE clause but I am having some issues writing this case statement.
where
(CASE WHEN #p_flag = 'ATA'
THEN (#p_start_ata IS NULL AND #p_end_ata IS NULL) OR (vso.poa_ata between #p_start_ata and #p_end_ata)
ELSE (#p_start_atd IS NULL AND #p_end_atd IS NULL) OR (vso.pol_atd between #p_start_atd and #p_end_atd)
)
Line 93 Incorrect syntax near the keyword 'IS'.
Its probably a minor error but its frustrating me. Maybe there is a better way of writing this? Thanks!
Like said before you can not use a case statement like you are doing it. So this might be a suggestion:
WHERE
(
#p_flag = 'ATA'
AND
(
(#p_start_ata IS NULL AND #p_end_ata)
OR (vso.poa_ata between #p_start_ata and #p_end_ata)
)
)
OR
(
NOT #p_flag = 'ATA'
AND
(
(#p_start_atd IS NULL AND #p_end_atd IS NULL)
OR (vso.pol_atd between #p_start_atd and #p_end_atd)
)
)
You can't use the CASE like that!
It's use to get values and not restrictions. Check this to view how to properly use CASE.
In your case you've to change your logic probably using IF's and making the wanted SELECT's.

Jasper-reports: Error: operator does not exist date = boolean

[EDIT: This problem was a result of a bug within version 3.7.6]
The following postgresql query is returning an error:
operator does not exist date = boolean.
I can't figure out why. Here is the postgresql code that is giving me the error:
select
c.source,
s.name,
s.grouping,
s.kli,
s.term_desc,
(s.population - s.online) as non_hb_pop,
s.online as hb_pop,
s.population as full_pop,
s.rep_date
from
dwh.rpt_cu_private_kli_summary s, dwh.rpt_sgmt_clients c
where
s.partner_id::integer = $P{rpt_cu}
and s.rep_date = $P{rpt_date_beg}
and s.userid=c.userid
group by
c.source, s.term_desc, s.name, s.grouping,
s.population, s.online, s.kli, s.rep_date
order by
s.grouping,
full_pop desc,
s.term_desc;
What does the above error message mean?
What is the value for $P{rpt_date_beg} ? That's where things go wrong. Check the real query, might be in the errorlog, and do some debugging. Maybe some quotes ' are missing around the date value.

Using the CASE function in MYSQL with Zend_DB_Expr

I'm stuck with the following problem. In my product table i've two columns
Date_start and Date_end (both of them are a DATE datatype in my table).
I want to check if the current date is between the Date_start and Date_end then the status must be 'not available', else it must have the status 'available'.
How can I fix that in Zend_Db_Expr?
I've now the following query.
$getProducts = $this->oSelect
->from(array('p'=>'producten'))
->columns(array('link' => "CONCAT('/t/', p.titel_key)"))
->joinLeft(array('c'=>'categorie'),'p.categorie_id = c.id',array('cat_titel'=>'c.titel'))
->joinLeft(array('sc'=>'subcategorie'),'p.subcategorie_id = sc.id', array('subcat_titel'=>'sc.titel'))
->where('p.online = 1');
In your columns:
->columns(array('link' => "CONCAT('/t/', p.titel_key)",
'status' => new Zend_Db_Expr("...")))
The 'CASE' should look something like this (i split it out for readability);
CASE WHEN p.Date_end < NOW() AND p.Date_start > NOW()
THEN 'not available' ELSE 'available' END
Zend_Db_Expr will take anything you give it and use it literally. Just remember that any DB specific commands might break if for some reason you switch systems.