How to change parameter value in query dialog Jaspersoft? - sql

To run my report I need to set parameter $P{P_CLIENT_TYPE} to one of three strings: "Insured" , "Policy Holder" or "BOTH". I would like to use shorter versions: "INS", "PH" and "%".
How to convert later these 3 short parameters in my query dialog, so instead of "PH" it will know that it means "Insured"?
My query looks like below:
select distinct (SELECT MIN(INS_FROM)
FROM [dbo].[INSURANCE_DB]
where [CLIENT_TYPE] like $P{P_CLIENT_TYPE}
and code_type =$P{P_CODE_TYPE}
and code = $P{P_COD}
) MIN_INS_FROM
,IIF( x.max1 > DATEADD(day, -1, GETDATE())
,DATEADD(day, -1, GETDATE())
, x.max1
) MAX_INS_TO
I tried to make replace function or convert function at the beginning of the query but nothing seems to work.

I have found the solution, it was quite simple. I just needed to use CASE WHEN ... THEN
select distinct (SELECT MIN(INS_FROM)
FROM [dbo].[INSURANCE_DB]
where [CLIENT_TYPE] like
**CASE
WHEN $P{P_CLIENT_TYPE} = 'INS' THEN 'Insured'
WHEN $P{P_CLIENT_TYPE} = 'PH' THEN 'Policy Holder'
ELSE '%'
END**
and code_type =$P{P_CODE_TYPE}
and code = $P{P_COD}
) MIN_INS_FROM
,IIF( x.max1 > DATEADD(day, -1, GETDATE())
,DATEADD(day, -1, GETDATE())
, x.max1
) MAX_INS_TO

Related

CONCAT leading 0 in case statement

I found an error in a query I inherited from a previous coworker that I am trying to fix but it's a weird issue I haven't encountered before.. here is a snip of the original query:
CAST(CONCAT(DATEPART(YYYY,getdate()),
(CASE
WHEN DATEPART(WW,getdate()) < 10
THEN CONCAT('0', DATEPART(WW,getdate()))
ELSE DATEPART(WW,getdate())
END)
) AS INT)
When getdate() = 2021-01-08 10:16:41.440 the query results in
20212
Expected result should be
202102
I found the issue relies in in the CASE statement. When I tried to change the THEN clause to
CAST(CONCAT(DATEPART(YYYY,getdate()),
(CASE
WHEN DATEPART(WW,getdate()) < 10
THEN RIGHT('0'+ CONVERT(VARCHAR(2), DATEPART(WW,getdate())),2)
ELSE DATEPART(WW,getdate())
END)
) AS INT)
I still get
20212
But when I run
SELECT RIGHT('0'+ CONVERT(VARCHAR(2), DATEPART(WW,getdate())),2)
I get
02
Can someone explain this? Why does it work outside of the CASE statement, but not within?
case expressions return a single value and that has a single type. If any of the return values are numbers, then the return value is a number, not a string.
To get a string, use datename():
(CASE WHEN DATEPART(WW, getdate()) < 10
THEN CONCAT('0', DATENAME(WW,getdate()))
ELSE DATENAME(WW, getdate())
END)
Or you could simplify your logic:
RIGHT(CONCAT('0', DATENAME(WW, getdate()), 2)
Or simplify everything. For instance, a number might be sufficient:
YEAR(GETDATE()) * 100 + DATEPART(WW, getdate())
Your query has implicit cast to INT:
CAST(CONCAT(DATEPART(YYYY,getdate()),
(CASE
WHEN DATEPART(WW,getdate()) < 10
THEN RIGHT('0'+ CONVERT(VARCHAR(2), DATEPART(WW,getdate())),2)
ELSE CAST(DATEPART(WW,getdate()) AS VARCHAR(2)) -- adding explicit cast here
END)
) AS INT)
I can advice the simple way:
SELECT
CAST(
CONCAT(
DATEPART(YYYY,getdate()),
RIGHT(CONCAT('0000', DATEPART(WW,getdate())), 2)
) AS INT);
You can try T-SQL here

Improvise SQL query

I have the below sql which takes 1 or 2 secs to return the result. I am calling this SQL inside cursor for 500 plus times. I am trying to re-write this query.
SELECT Sum(CASE
WHEN UpdatedAdjustedOT IS NOT NULL
AND UpdatedAdjustedOT != ''
AND UpdatedAdjustedOT != '0'
THEN CONVERT(DECIMAL(18, 2), UpdatedAdjustedOT)
ELSE 0
END) AS OTHours
FROM tbl_OTAuthorization
WHERE EmployeeCodeFK = #EmployeeCode
AND month(OTDate) = Month(#FromDate)
AND year(OTDate) = Year(#FromDate)
Please suggest me how do I re-write this query in a better way
Don't filter on function results. Find a way to replace this:
AND month(OTDate) = Month(#FromDate)
AND year(OTDate) = Year(#FromDate)
to something like this:
and OTDate >= the first day of the month for #FromDate
and OTDate < the first day of the month following #FromDate
First, re-write the WHERE clause to look like this:
SELECT . . .
FROM tbl_OTAuthorization
WHERE EmployeeCodeFK = #EmployeeCode AND
OTDate >= DATEADD(day, 1 - DAY(#FromDate), #FromDate) AND
OTDate < DATEADD(month, 1, DATEADD(#FromDate, 1 - DAY(#FromDate), #FromDate));
Second, create an appropriate index for the table:
CREATE INDEX tbl_OTAuthorization_2 ON tbl_OTAuthorization(EmployeeCodeFK, OTDate, UpdatedAdjustedOT);
Third, go back and fix your code so it is not looping over employees and from dates. You should be able to handle the logic in a single query. In general, avoid cursors if you want to optimize the performance of using a database.
Try this:
SELECT Sum(CONVERT(DECIMAL(18, 2), ISNULL(NULLIF(UpdatedAdjustedOT, ''), 0))) as OTHours
FROM tbl_OTAuthorization
WHERE EmployeeCodeFK = #EmployeeCode
AND month(OTDate) = Month(#FromDate)
AND year(OTDate) = Year(#FromDate)
WHERE EmployeeCodeFK = #EmployeeCode
AND OTDate >= DATEADD(MONTH, DATEDIFF(MONTH, 0, #FromDate), 0)
AND OTDate < DATEADD(MONTH, 1, DATEADD(MONTH, DATEDIFF(MONTH, 0, #FromDate), 0))

SQL replace using SELECT and invalid column

I have a simple table where the date column answer_6 (formatted as varchar(max) either has a valid date or string Now. I wanted to substitute the Now with the current date/time and then calculate the difference. Here is what I did:
select
CASE [answer_6]
WHEN 'Now' THEN CONVERT(varchar, GETDATE())
ELSE answer_6
END as x,
answer_6 from [tDataMult] where DATEDIFF(yy, GETDATE(), [x]) > 5
The system give me invalid column name 'x'. If I remove the DateDiff SELECT statement works fine.
You can't use aliases in the WHERE clause of the same level as it was defined as WHERE clause is evaluated before the SELECT clause. Try the following :
SELECT t.* FROM (<...>) t WHERE DATEDIFF(yy, GETDATE(), [t.x]) > 5
Instead <...> put your query without WHERE clause.
You can't refer to an alias you've just created in a WHERE clause. The easiest way to fix it would just be to turn your original query into a subquery:
SELECT *
FROM
(
select
CASE [answer_6]
WHEN 'Now' THEN CONVERT(varchar, GETDATE())
ELSE answer_6
END as x,
answer_6 from [tDataMult]
) AS qry
WHERE DATEDIFF(yy, GETDATE(), [x]) > 5
Or you could replicate the expression in your WHERE clause:
select
CASE [answer_6]
WHEN 'Now' THEN CONVERT(varchar, GETDATE())
ELSE answer_6
END as x,
answer_6 from [tDataMult]
WHERE DATEDIFF(yy, GETDATE(),
(CASE [answer_6]
WHEN 'Now' THEN CONVERT(varchar, GETDATE())
ELSE answer_6
END)
) > 5
Column X does not exist in tDataMult table because it is an alias.
Replace it with answer_6 in DateDiff function.
However, one of the possible value of answer_6 column is varchar format ('Now').
You need to have valid data format to use DATEDIFF function properly. (for example: 'yyyy-mm-dd').

If not <> NULL then "xxxField" equals '1'

I have a feeling this is a fairly simple one. I need to edit a line in the SELECT statement that looks to see if there is a value of NULL in a field. If there is a NULL value, I need the new column (not named) to display a '0' for that row. Where the row has data I need to display '1' in that row. Is there a way to do this without greatly modifying the logic I have? (Using SQL Server Management Studio)
Here's the code:
SELECT DISTINCT t.Name,
CONVERT(VARCHAR(10), t.DischargeDateTime, 120) AS DischargeDate,
t.PatientPortalEnabled,
t.Allergy,
CONVERT(VARCHAR(10), t.AllergyUpdateTime, 120) AS AllergyUpdate,
/*This is where I would like to put the logic if possible*/ <> NULL,
t.ElapseTimeForAllergyUpdateInMinutes,
t.OldValue,
t.NewValue,
t.ElapseTimeForAllergyUpdateInHours,
t.DischargeDateTime
Try this:
CASE WHEN MyField IS NULL THEN 0 ELSE 1 END
Here it is in your code:
SELECT DISTINCT t.Name,
CONVERT(VARCHAR(10), t.DischargeDateTime, 120) AS DischargeDate,
t.PatientPortalEnabled,
t.Allergy,
CONVERT(VARCHAR(10), t.AllergyUpdateTime, 120) AS AllergyUpdate,
CASE WHEN t.MyField IS NULL THEN 0 ELSE 1 END,
t.ElapseTimeForAllergyUpdateInMinutes,
t.OldValue,
t.NewValue,
t.ElapseTimeForAllergyUpdateInHours,
t.DischargeDateTime
You can do this using a CASE statement.
SELECT DISTINCT t.Name,
CONVERT(VARCHAR(10), t.DischargeDateTime, 120) AS DischargeDate,
t.PatientPortalEnabled,
t.Allergy,
CONVERT(VARCHAR(10), t.AllergyUpdateTime, 120) AS AllergyUpdate,
/*This is where I would like to put the logic if possible*/
CASE
WHEN t.MyField IS NULL THEN 0
ELSE 1
END AS MyNewField,
t.ElapseTimeForAllergyUpdateInMinutes,
t.OldValue,
t.NewValue,
t.ElapseTimeForAllergyUpdateInHours,
t.DischargeDateTime
Using XOR bitwise operator:
declare #x int = null
select isnull((#x ^ #x),-1)+1

CASE expressions on datetime columns

I'm trying to access a datetime column to find out whether the date is within a week from today, or overdue. Then write a new column's value to say Incoming, Overdue or Fine.
SELECT
CASE next_action_date
WHEN (BETWEEN GETDATE()+7 AND GETDATE()) THEN 'Incoming'
WHEN (< GETDATE()) THEN 'Overdue'
ELSE 'Fine'
END AS condition
FROM
tableName
This is what I've got so far, but as you can probably see by looking, it doesn't work at all:
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'BETWEEN'.
There are two syntaxes of the CASE expression - the so-called simple one that compares a single value against a list of other values, and a searched one with generic boolean conditions. You picked the simple case, but it does not have enough flexibility for what you need; you should switch to the searched syntax, like this:
SELECT
CASE
WHEN next_action_date BETWEEN GETDATE() AND GETDATE()+7 THEN 'Incoming'
WHEN next_action_date < GETDATE() THEN 'Overdue'
ELSE 'Fine'
END AS condition
FROM
tableName
Please try
select CASE
when next_action_date between GETDATE() and GETDATE()+7 then 'Incoming'
when next_action_date < GETDATE() THEN 'Overdue'
else 'fine' end as Condition
from(
select GETDATE()+6 next_action_date
)x
Try this one -
DECLARE #Date DATETIME
SELECT #Date = GETDATE()
SELECT
condition = CASE
WHEN t.next_action_date BETWEEN #Date AND DATEADD(DAY, 7, #Date) THEN 'Incoming'
WHEN t.next_action_date < #Date THEN 'Overdue'
ELSE 'Fine'
END
FROM dbo.tableName t
use DATEADD(Day, 7, GETDATE())
You should uses the other form of the case statement
SELECT
CASE
WHEN (next_action_date BETWEEN GETDATE()+7 AND GETDATE()) THEN 'Incoming'
WHEN (next_action_date < GETDATE()) THEN 'Overdue'
ELSE 'Fine'
END AS condition
FROM
tableName
http://www.devx.com/tips/Tip/15633