How I can use case statment in BIP after where as paramter? - oracle

How I can use case statment in BIP App after where and compare it to a date
if is the date is null show Full data if he ask for Specific date display the data for the date that he asked for

Didn't understand the question but if you think of using case in where clause it could be something like this:
-- ...
WHERE
something = CASE
WHEN Nvl(date_column, To_Date('01.01.2099', 'dd.mm.yyyy') <= other_date_column -- or parameter or date calculation .....
THEN something
ELSE
something_else
END
-- ...
It depends what you want to do when one of comparing dates is null, but if it is null, you could use Nvl() function to create a date that is for sure out of scope and make it '=' or '<=' or '>=' or ... to the comparing value. This way you can manage the expresion and do the job. This is definitely sql question. Regards...

Related

convert TO_CHAR, IS_DATE to hive query

I want to convert specific data to Hive.
However, functions available in Oracle cannot be used in Hive. How can I solve this?
The applied conversion rule is as follows.
DECODE(TRUE, IS_DATE(TO_CHAR(columnname , 'YYYYMMDD')), 'YYYYMMDD',NULL)
In the case of DECODE, it was confirmed that it could be processed with IF.
But I couldn't find a way to change IS_DATE function and TO_CHAR function.
Oracle does not have an IS_DATE function. Are you sure this is not a user-defined function? If so then you will need to look at the source code and check what it does and duplicate that in Hive.
DECODE(a, b, c, d) can be rewritten as a CASE expression:
CASE WHEN a = b THEN c ELSE d END
So your code (assuming that columnname is a DATE and you are using TO_CHAR to convert it to a string and then IS_DATE checks if it is a valid date, which seems pointless as it will only not be a valid date when columnname is NULL) would convert to:
CASE
WHEN CAST(columnname AS STRING FORMAT 'YYYYMMDD') IS NOT NULL
THEN 'YYYYMMDD'
ELSE NULL
END
or, more simply:
CASE
WHEN columnname IS NOT NULL
THEN 'YYYYMMDD'
ELSE NULL
END

Validate null columns values in a select statement in BigQuery

I'm trying to make a select to a bigquery table, but I need to assign a default value to a column if it is null, because next in the process I need the default or the real item_id value, I was trying to use the CASE validation but I'm not pretty sure if I can use this clause for this purpose, and I'm getting the next error:
Expected end of input but got keyword case.
Select
p.item_id CASE WHEN item_id IS NULL THEN 'XXXXX' ELSE item_id END AS item_id,
from items
where -- rest of the query
Any ideas?
try this
Select
IFNULL(p.item_id,'XXXXX') AS item_id,
from items
where -- rest of the query
IFNULL() function in BigQuery
IFNULL(expr, null_result)
Description
If expr is NULL, return null_result. Otherwise, return expr. If expr is not NULL, null_result is not evaluated.
expr and null_result can be any type and must be implicitly coercible to a common supertype. Synonym for COALESCE(expr, null_result).
more details: https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#ifnull
Use
isnull(yourcolumn,'')
--or
isnull(yourcolumn,99999999)
and you can put anything between those quotes. If the value is an int that won't work though. You will need to make it some default number, preferably something that would never be used like 9999999999.

Whats the correct way to use a date parameter in WHERE clause

Whats the correct way to use a parameter for
WHERE WORKS_ORDER_HEADER.DATEREQ < TO_CHAR('DD/MM/YYYY', '&CUT_OFF_DATE')
The data is presented in 16/06/1995 format
So if the parameter is less than that date, show all prior dates
WHERE date_collumn <= TO_DATE(:PARATEMER, 'DD/MM/YYYY')
Why would you try and compare CHAR when you could compare dates.
I think, you are looking for:
WHERE WORKS_ORDER_HEADER.DATEREQ < TO_DATE('&CUT_OFF_DATE', 'DD/MM/YYYY')
This will of course work only, if your DATEREQ is of type DATE.

Dynamic order by date data type in Oracle using CASE

My code in the stored procedure:
SELECT * FROM
my_table ir
WHERE
--where clause goes here
ORDER BY
CASE WHEN p_order_by_field='Id' AND p_sort_order='ASC' THEN IR.ID end,
CASE WHEN p_order_by_field='Id' AND p_sort_order='DESC' THEN IR.ID end DESC,
CASE WHEN p_order_by_field='Date' AND p_sort_order='ASC' THEN TO_CHAR(IR.IDATE, 'MM/dd/yyyy') end,
CASE WHEN p_order_by_field='Date' AND p_sort_order='DESC' THEN TO_CHAR(IR.IDATE, 'MM/dd/yyyy') end DESC;
Problem is that sorting is done based on the char, which comes out wrong for the date case. CASE statement, however, won't allow any other datatype other than char. So what is the solution in this case? I need to be able to pass the p_order_by_field into the stored procedure.
Thanks
Should be simple - just use ISO date format in your case:
TO_CHAR(IR.IDATE, 'yyyy-mm-dd')
and you should be fine.
Another problem could occure when you want to sort on the date difference (let say number of days between two days).
For example such a sort would return number 13 (days) before 9 (days).
The solution is that you concatenate length of date difference and the difference itself:
length(trunc(date2) - trunc(date1)) || to_char(date2 - date1)

DATEDIFF (in months) in linq

select col1,col2,col3 from table1
where(DATEDIFF(mm, tblAccount.[State Change Date], GETDATE()) <= 4
I want to convert this sql query to LINQ. but I dont know any DateDiff alternative in LINQ. can you please suggest me?
You're looking for SqlMethods.DateDiffMonth.
EDIT: In EF4, use SqlFunctions.DateDiff.
Putting aside your original question for a moment, in your query you use:
where(DATEDIFF(mm, tblAccount.[State Change Date], GETDATE()) <= 4
This query would always cause a full table scan, since you're comparing the result of a function call against a constant. It would be much better if you calculate your date first, then compare your column value against the calculated value, which would allow SQL to use an index to find the results instead of having to evaluate every record in your table.
It looks like you're trying to retrieve anything within the past 4 months, so in your application code, try calculating the date that you can compare against first, and pass that value into your Linq2Entities expression:
DateTime earliestDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(-4);
var results = from t in context.table1
where t.col3 >= earliestDate
select t;
In EF6, the class to use is DbFunctions. See, for example, DbFunctions.DiffMonths.
I solved this problem in another manner: I calculated the date from code:
theDate = Date.Now.AddMonths(-4)
And in EF change the condition:
tblAccount.[State Change Date] < theDate

Resources