Trying to orderby with case when clause for MultiDocumentQuery? - kentico

I am trying to get all my pagetypes from kentico, but I would like to order them by all the page types that starts with the letter "F", first and then the other pages alphabetically.
What I am trying is:
1) MultiDocumentQuery docQuery = DocumentHelper.GetDocuments().Type("forms").OnSite(SiteContext.CurrentSiteID).Published();
2) docQuery.OrderBy("FormName");
But what I need is to order em using something like this (SQL):
SELECT FormName
FROM Forms
order by
case when FormName like 'C%' then 0 else 1 end,
FormName
What I cant do is to translate the case when properly to c#, and implement it to docQuery.
So the result should be for example,:
Form1
Form2
Form3
A...
B...

I think I'd look at adding your case statement in the columns rather then the where like this:
CASE WHEN FormName LIKE 'F%' THEN 0 ELSE 1 END AS IsForm
This way you can have a new column called IsForm and can have an Order by of:
IsForm, FormName

Related

Using Case statement in Where clause in SQL developer

I am trying to write a "case" statement inside a "where clause" which has an "in" statement in the "then" part. Basically, the following code is what I want, but it's not the Oracle correct syntax. Does anybody have any idea what the correct syntax should be like?
create or replace PROCEDURE "Test"
(
Type in number
)
as
begin
select Id, Name, AccCode from myTable
where case Type
when 0 then AccCode in (130,131)
when 1 then AccCode in (230,231);
end;
I don't think you want a case statement. You probably just want
where (accCode in (130,131) and type = 1)
or (accCode in (230,231) and type = 0)
If you really want to use a case statement, you could say
where (case when accCode in (130,131) then 1
when accCode in (230,231) then 0
else null
end) = type
But that will not be as readable nor will it be as easy for the optimizer to find a good execution plan.

Cognos Report Studio: CASE and IF Statements

I'm very new in using Cognos report studio and trying to filter some of the values and replace them into others.
I currently have values that are coming out as blanks and want to replace them as string "Property Claims"
what i'm trying to use in my main query is
CASE WHEN [Portfolio] is null
then 'Property Claims'
ELSE [Portfolio]
which is giving me an error. Also have a different filter i want to put in to replace windscreen flags to a string value rather than a number. For example if the flag is 1 i want to place it as 'Windscreen Claims'.
if [Claim Windscreen Flag] = 1
then ('Windscreen')
Else [Claim Windscreen Flag]
None of this works with the same error....can someone give me a hand?
Your first CASE statement is missing the END. The error message should be pretty clear. But there is a simpler way to do that:
coalesce([Portfolio], 'Property Claims')
The second problem is similar: Your IF...THEN...ELSE statement is missing a bunch of parentheses. But after correcting that you may have problems with incompatible data types. You may need to cast the numbers to strings:
case
when [Claim Windscreen Flag] = 1 then ('Windscreen')
else cast([Claim Windscreen Flag], varchar(50))
end
In future, please include the error messages.
it might be syntax
IS NULL (instead of = null)
NULL is not blank. You might also want = ' '
case might need an else and END at the bottom
referring to a data type as something else can cause errors. For example a numeric like [Sales] = 'Jane Doe'
For example (assuming the result is a string and data item 2 is also a string),
case
when([data item 1] IS NULL)Then('X')
when([data item 1] = ' ')Then('X')
else([data item 2])
end
Also, if you want to show a data item as a different type, you can use CAST

Grouping Classes & Subclasses in saved search

I'm trying to create a saved search that sums total sales by the parent class. I'm trying a CASE statement but can't seem to get it to work. This is what I have:
CASE
WHEN {class} LIKE 'B2B%’ THEN 'B2B'
WHEN {class} LIKE 'Amazon%’ THEN 'Amazon'
WHEN {class} LIKE 'Website%’ THEN ' B2C'
ELSE ‘’
END
Not sure why but that format never works for me either, although Netsuite documentation suggests that it should/does. Try nested CASE statements.
CASE WHEN {class} LIKE 'B2B' THEN 'B2B' ELSE
CASE WHEN {class} LIKE 'Amazon%' THEN 'Amazon' ELSE
CASE WHEN {class} LIKE 'Website%' THEN ' B2C' ELSE '' END
END
END
Or you can use SUBSTR({class},0,INSTR({class},':')-2) to get just the part of the string before " :", assuming all of your classes have these characters present.
Once you have that column formula working, group results based on this formula, add total sales as a column and group that but select "Sum".

Create new bit column based on string evaluation

I was looking into the function Contains but it can only be used in the Where predicate, what I'm looking for is something of the sorts
Select doc.* , IsSync = StringContains('Sync', doc.Url) from vw_Doc as doc
IsSync now would contain 1/0 or true/false, depending if the word Sync exists in the document Url.
Is this at all possible ?
Thank you for your time
Edit: StringContains is meant as a pseudo-function, it's not valid syntax
I would use case and charindex().
select
doc.* ,
case
when charindex('Sync', doc.Url) > 0
then 1
else 0
end as IsSync
from
vw_Doc as doc
Contains is not limited to a where clause, it returns a boolean that can be used in various contexts, e.g.:
select *,
case when Contains( doc.url, 'Sync' ) then 1 else 0 end as IsSync
from vw_Doc;

Conversion error If value = "" Then

I'm getting a error I can't quite explain I have a Excel list I want to load into memory, to see if the next row is still a relative row I check if the cell has a value by doing If value = "" Then but the value is 1012738 and it gives me a unhandled exception...
I can't quite understand why the code is giving a error, the cell value is formatted just like all previous cells that were checked. But here a error is thrown.
Maybe i'm just not seeing it, and someone can explain?
You should check the value each time.
Dim o As Object = oSheet.Range(xxx).Value
If (o IsNot Nothing) Then
Select Case o.GetType
Case GetType(Double)
' do work here
Case GetType(Integer)
' do work here
...
End Select
Else
...
End If
Your image isn't showing up for me.
Most exceptions in excel are datatype related. Most likely, you either have a NULL or a string that looks like an integer to the human eye. You can blindly try using trim(), int() or str() etc as needed to make sure you're actually testing an integer or matching a string if thats what you are about or you can test them in a programmatic method.
First, the programmatic method of testing ... isEmpty or isNull are needed to ensure the cell is good ...
if isEmpty(value) Then
<do something>
This will most likely catch the error which is causing the message. If empty is failing, try testing with isNull. One means the cell is empty, the other means that it wasn't initialize (rarely an issue in excel, but if using vba code it can happen).
Also ... your if statement is setup in a less than optimal... used <> in place of if then + else ...
Your code ...
If value = "" Then
General formula when you want to test that something is not something else, use the not equals ...
if value <> "value" Then
"some operation"

Resources