Crystal Reports If Else in Database select SQL Command - sql

Im hoping to have have an if/else in my sql command within crystal 2013. The idea is something similar to this and I don't want to use stored procedures.
IF {?Search_Settings} = "This"
THEN
BEGIN (SELECT * FROM blah
WHERE GETDATE() < blah.date)
END
else if {?Search_Settings} = "That"
THEN
BEGIN (SELECT * FROM blah
WHERE GETDATE() > blah.date)
END
As mentioned I am looking to have this in the database expert > Modify Command Screen.
Is this even possible? If not, what is the best option for achieving this? I appreciate your help with this.

Related

Regex Syntax in sql script

I wanted to use two Regex in a SQL script to retrieve some information in the software. Here are Regex commands:
{TRIGGER(63f02a37-1f92-4e36-af2a-
0e9764b22c75|LastTrigger|Mail.Result.Mail.BodyText)|(?<=Exit No:\s)[0-9]
{9}}
{TRIGGER(63f02a37-1f92-4e36-af2a-
0e9764b22c75|LastTrigger|Mail.Result.Mail.BodyText)|(?<=Barcode:\s)[A-Z]
{9}[0-9]{10}}
These regex should be subsituded with 919500006 and 'Barcode'. Thank you Guys
Here is My SQL Script:
IF ((SELECT ExitPermissionNo FROM tbl_ExitPermission WHERE
(ExitPermissionNo = '919500006') AND (ReferenceCCS IS NULL))!=1)
BEGIN
UPDATE tbl_ExitPermission
SET ReferenceCCS = 'Barcode'
WHERE ExitPermissionNo = 919500006;
PRINT 'The ReferenceCCS Update Successfully!'
END
ELSE BEGIN
PRINT 'The ExitPermission Not Found!'
RAISERROR('The ExitPermission Not Found!',18,1)
END
My guess is that you can do so using:
ReferenceCCS\s*=\s*'([^']*)'
or:
ReferenceCCS\s*=\s*["']([^'"]*)["']
Demo 1
for the first one, and:
(ExitPermissionNo\s*=\s*['"]?)(\d+)
Demo 2
for the second one.

SAP HANA SQL Query with Dynamic Placeholder

I have a query that is passing the current year as a placeholder parameter that right now is hard coded. How can I have this just pass the current year? I've seen a few different potential solutions but most of them are in HANA Studio or involve dynamic SQL generation.
I'm putting the SQL into Tableau so those are both off the table.
...sum("StockInQualityInspection") as in_quality,
sum("StockInTransit") as its
from "_SYS_BIC"."stream.models.marketing.poly/InventoryQuery" ('PLACEHOLDER' = ('$$IPCurrentYear$$', '2018'))
where "StockValuatedUnrestrictedUse" <> 0 or "StockInQualityInspection" <> 0 or "StockInTransit" <> 0
group by case when "ReceivingPlant" is null then "Plant" else "ReceivingPlant" end,
case....
Remove the parameters input of your CV
Add this expression: year(now())
If you don't have access to manipulate the CV, into your query use:
('PLACEHOLDER' = ('$$IPCurrentYear$$', select year(now()) from DUMMY))
Regards
while placing a query is not permitted, you can pass a parameter as following
do begin
declare lv_param nvarchar(100);
select max('some_date')
into lv_param
from dummy /*your_table*/;
select *
from "_SYS_BIC"."path.to.your.view/CV_TEST" (
PLACEHOLDER."$$P_DUMMY$$" => :lv_param
);
end;
more can be found here
credit to #astentx

Convert Crystal Report Expression to SQL Query

How can I change the below Crystal Report Expression to SQL Server Query Case Statement ? Any help appreciated.
AvgLbsPerWeek
If {#EnoughWeightsSum} < {#FreqForCalcs} then
(AvgLbsPerWeek := AvgLbsPerWeek +0;
0;)
else If {#EnoughWeightsSum} >= {#FreqForCalcs} then
(AvgLbsPerWeek := AvgLbsPerWeek +{#AvgByDaySum}/{#EnoughWeightsSum}*{#FreqForCalcs};
{#AvgByDaySum}/{#EnoughWeightsSum}*{#FreqForCalcs};)
This is a simple CASE statement
SELECT
AvgLbsPerWeek =
CASE WHEN #EnoughWeightSum < #FreqForCalcs
THEN AvgPoundsPerWeek
ELSE
AvgPoundsPerWeek + #AvgByDaySum/(#EnoughWeightSum*FreqForCalcs)
-- I didn't quite understand how the last code fit in
END
FROM Table

UDF on DB2 11.0

I was asked to build a user defined function on our Mainframe environment that checks for a search string in a longer string. The only catch is that if we search for example for 'AA' in 'ABCAADAA' the only valid result is the last AA because the first AA actually split in CA and AD.
CREATE FUNCTION F#CRE#WK (WK CHAR(02), WKATTR CHAR(10))
RETURNS INTEGER
LANGUAGE SQL
READS SQL DATA
BEGIN
DECLARE INDEX INTEGER DEFAULT 1;
WHILE (INDEX < 9) DO
SET INDEX = LOCATE_IN_STRING(WKATTR, WK, INDEX);
IF (MOD(INDEX, 2) <> 0) THEN
RETURN 1;
END IF;
END WHILE;
RETURN 0;
END;
It is working fine when I implement it using Data Studio but if I put it onto the host directly (we're using Quick32770) I'm getting a bunch of errors which don't make sense at all. I couldn't find any helpful resources(searched the whole IBM page and Google of course).
First error I'm getting is:
SQLCODE = -104, ERROR: ILLEGAL SYMBOL "<END-OF-STATEMENT>". SOME
SYMBOLS THAT MIGHT BE LEGAL ARE: ;
Which refers to the line I'm declaring my index variable. If I remove the semicolon it tells me that the SET is illegal there because it is expecting a semicolon.
I cannot think of anything else I could try(I messed around with the code a lot but errors just kept getting more weird.). I started working in this field while being in college just a couple of weeks ago and nobody here has actual knowledge about this so I was hoping to find some help here.
If there's anything else you need, just let me know!
Thanks in advance.
This might help you:
https://bytes.com/topic/db2/answers/754686-db2-udf-need-eliminate-if-statement
It says the if statement is not allowed on the mainframe in UDF ?
So this user bend it around to a CASE function.
In order to fix this you need to go into the SPUFI settings and change the TERMINATOR option to something else than a semicolon. If I changed it to & my code must look like this:
CREATE FUNCTION F#CRE#WK (WK CHAR(02), WKATTR CHAR(10))
RETURNS INTEGER
LANGUAGE SQL
READS SQL DATA
BEGIN
DECLARE INDEX INTEGER DEFAULT 1;
WHILE (INDEX < 9) DO
SET INDEX = LOCATE_IN_STRING(WKATTR, WK, INDEX);
IF (MOD(INDEX, 2) <> 0) THEN
RETURN 1;
END IF;
END WHILE;
RETURN 0;
END&

Returning Errors without a stored procedure

I need to know the number of rows in a certain table. If it is under 250 rows I need to return an error to the sql job forcing it to quit. The problem is it's not a stored procedure. It's sql code is running straight from the job step as a Transact-SQL script. Is this possible to return anything, or is there a better way to do this?
This is what I have:
select case when (select cnt = count([col]) from db.dbo.table) < 250 THEN 1 ELSE 0 END
You can use the RAISERROR command.
IF (SELECT COUNT([col] FROM db.dbo.table) < 250
RAISERROR('My error message', 15, 1)
The severity level 15 is a level that will indicate to the job that the command failed.
Look here for more about the RAISERROR command.