Specific date in a case statement in Looker - case

Writing a case statement in Looker and trying to put a specific date in the else portion. Is there a specific syntax I should be using? In the else portion I am trying to use a specific instead of the ${Today}, I would like to use 01/01/2050.
sql:CASE
when ${Today} < ${XXX} then ${XXX}
ELSE ${Today}
END;;
Tried ${TODAY} + 90(just to check syntax) , TRIED '01/01/2050.

You're writing SQL, so the syntax might be a little different for your RDBMS, but you should be okay with a string literal ISO Date value, optionally casted to a date type.
sql:CASE
when ${Today} < ${XXX} then ${XXX}
ELSE '2050-01-01'
END;;

Related

Datetime Tableau Calculation - Convert String to Datetype

I am attempting to write a case statement in tableau. I am casing the date field where I want to control for a specific date range.
CASE [Date] WHEN [Date] >= DATETIME('1/1/17') and [Date] < DATETIME('1/11/17') THEN 1 ELSE 0 END
However, I am getting a error -
"Expected type data, found boolean. Compare in case must be date type.
I got the same error using:
DATEPARSE('mm dd yyyy','1/1/17')
I understand my error. However, I can't seem to identify the function which convert my string into a date.
This post didn't help either.
How to convert string to date format in Tableau
Anyone know?
Looks like this works
sum(if year([Date]) == 2017 and MONTH([Date]) == 1 then [profit] END)
I'd still like to see the case solution if anyone can figure it out.

Datatype Mismatch in THEN/ELSE expression

I am trying to run below query snippet in a Teradata query
WHERE COALESCE(CAST (EXPC_DLVR_TS as date),'2020-12-31') >'2016-11-18'
I tried another but similar one
WHERE CAST(COALESCE(EXPC_DLVR_TS,'12/31/2020 17:00:00.000000-08:00') as date) >'2016-11-18'
For both the queries I am getting below error -
Datatype Mismatch in THEN/ELSE expression
You need to tell Teradata that '2020-12-31' is a date, otherwise it thinks it's a string. Just preface it by DATE. It's a good habit to always do that for dates.
where COALESCE(CAST (EXPC_DLVR_TS as date),date '2020-12-31') > date '2016-11-18'

Marklogic collate sequence in XQuery

Is there a way to modify the elements a sequence so only collated versions of the items are returned?
let $currencies := ('dollar', 'Dollar', 'dollar ')
return fn:collated-only($currencies, "http://marklogic.com/collation/en/S1/T00BB/AS")
=> ('dollar', 'dollar', 'dollar')
The values that are stored in the range index (that feeds the facets) are literally the first value that was encountered that compared equal to the others. (Because, the collation says you don't care...)
You can get a long way by calling
fn:replace(fn:lower-case(xdmp:diacritic-less(fn:normalize-unicode($str,"NFKC"))),"\p{P}","")
This won't be exactly the same in that it overfolds some things and underfolds others, but it may be good for your purposes.
Is this the expected output? There is no fn:collated-only function, so I'm assuming you're asking how to write such a function or whether there is such a function.
The thing is, there isn't a mapping from one string to another in collation comparisons, there is only a comparison algorithm (the Unicode Collation Algorithm) so there really is no canonical kind of string to return to you, and therefore no API to do so.
Stepping back, what is the problem you are actually trying to solve? By the rules of that collation, "dollar" and "Dollar" are equivalent, and by using it you declare you don't care which form you use, so you could use either one.
If these values are in XML elements and you have a range index using http://marklogic.com/collation/en/S1/T00BB/AS, you can do something like this:
let $ref := cts:element-reference(xs:QName("currency"), "collation=http://marklogic.com/collation/en/S1/T00BB/AS")
for $curr in cts:values($ref, (), "frequency-order")
return $curr || ": " || cts:frequency($curr)
This will produce results like:
"dollar: 15",
"euro: 12"
... and so on. The collation will disregard the differences among your sample inputs. These results could be formatted however you want. Is that what you're looking to do?

Doing datetime math in a function call in an Oracle query

Ok, I got the first part of my question answered, so here's the second part. :-) In a PLSQL query, I have criteria that looks like this:
where Doc3.clinicalDate >= ml.convert_date_to_id(:DateBegin)
and Doc3.clinicalDate < ml.convert_date_to_id(:DateEnd)
Now, I don't want to use :DateEnd itself -- I want to add 1 day so that when it compares the datetime to midnight, I get midnight of the next day. Unfortunately, when I do
where Doc3.clinicalDate >= ml.convert_date_to_id(:DateBegin)
and Doc3.clinicalDate < ml.convert_date_to_id(:DateEnd + 1)
I get "ORA-06553: PLS-306: wrong number or types of arguments in call to 'CONVERT_DATE_TO_ID'". ":DateEnd + interval '1' day" gives me "ORA-30081: invalid data type for datetime/interval arithmetic" (where :DateEnd is bound to 31-MAY-2012). If I do "convert_date_to_id(add_months(:DateEnd, 1))", it works fine. Any thoughts? Thanks.
ETA: I should clarify that this is an SSRS 2008 R2 project, and DateBegin and DateEnd are defined in the report parameters as DateTime parameters. My current workaround involves setting the :DateEnd query parameter equal to the #DateEnd report parameter + 1, but I'm worried that someday I'll forget to document this properly and confuse the heck out of whomever's trying to maintain the report (and it might be me). I don't want to pass string parameters, as suggested before.
I'm thinking that the parameters being DateTime is root of the problem. Microsoft DateTime datatypes are way more granular than Oracle's in that it supports fractional seconds and Oracle DATE format does not (Oracle TIMESTAMP does however).
Since ADD_MONTHS just spits back whatever it's passed in DATE datatype (i.e. passed TIMESTAMP becomes DATE). So maybe you can convert the parameter and add the day that way:
where Doc3.clinicalDate >= ml.convert_date_to_id(:DateBegin)
and Doc3.clinicalDate < ml.convert_date_to_id(CAST(:DateEnd as DATE)+1)
Alternatively, forget about conversions and date arithmetic on the parameters and subtract a day from the second clinicalDate:
where Doc3.clinicalDate >= ml.convert_date_to_id(:DateBegin)
and Doc3.clinicalDate - 1 < ml.convert_date_to_id(:DateEnd)
Assuming that ml.convert_date_to_id takes a DATE as an input parameter rather than a VARCHAR2 that represents a date, and assuming that the :DateEnd bind variable is a VARCHAR2, you would need something like
ml.convert_date_to_id( to_date( :DateEnd, 'DD-MON-YYYY' ) + 1 )
or
ml.convert_date_to_id( to_date( :DateEnd, 'DD-MON-YYYY' ) + interval '1' day )
Use to_date to convert the value. For example:
select &date + 1 from dual
Informing to_date('29052012','ddmmyyyy') works fine
Informing '29-may-2012' gives ORA-01722: invalid number

missing operand before '<' operator in datatable select

In the data table there is column that is composite date that has the value of the date with some conditions i want to filter the data table.
The expression that i am making is giving me error .
Expression
scaleID=8207 and CompositeDate >= '5/1/2009 6:01:23 PM' And CompositeDate =< '5/31/2009 6:01:23 PM'
what is that i am missing here if it cannot be done in this way can we use LINQ for this if yes can any one provide me with the syntax.
You're using =< when I suspect you mean <=.
The docs for the expression syntax include <=, but I can't see anything to suggest that =< is valid.
Personally I would try to avoid magic strings as far as possible and use LINQ instead (even within datatables) but that's a different matter.

Resources