CASE statement example - apache-spark

Can someone please help me identify the correct syntax for CASE statement with Spark SQL? I tried the following:
SELECT
CASE circle WHEN ("Panjab") THEN 2 END
FROM
siteinfo
where circle is a valid column name. However, I'm still unable to determine the correct approach.

This should work:
SELECT IF(circle='Panjab', 2, 0) FROM siteinfo

the correct case when syntax:
SELECT
CASE circle= "Panjab" THEN 2 END
FROM
siteinfo
or
SELECT
CASE circle= "Panjab" THEN 2 else 'some other value' END
FROM
siteinfo

Related

How to get rid of this error mismatched input 'line' expecting <EOF>(line 1, pos 17) in spark-sql

select case
when line = 'KEY'
then case
when length(pop)>0 and pop not rlike '^[0-9]+#.*'
then ''
else case
when pop rlike '^[0-9]+#.*'
then regexp_extract(pop, '^[^#]+', 0) end
else '' end as pop from input
The above code throughs the error "mismatched input 'line' expecting (line 1, pos 17)"
can anyone help me with this!
Sometimes it becomes tricky when writing nested "case" statements, use a editor
like notepad++ (Language -> sql) and check if the case/end are balanced.
And when you select "end" there are only 2.
Note that "else" is optional.
From the picture it is clear that an "end" statement is missing.
Try to align each case/end statement on the same vertical ruler using tabs/spaces.
Now you can identify the issue easily.
Check this.
You have 3 CASE clause but only 2 END statements, you missed one more END keyword.
And one CASE clause can have only one ELSE clause.
Below modified query should work for you.
SELECT
CASE
WHEN line = 'KEY' THEN
CASE
WHEN Length(pop)>0
AND pop NOT rlike '^[0-9]+#.*' THEN ''
ELSE
CASE
WHEN pop rlike '^[0-9]+#.*' THEN regexp_extract(pop, '^[^#]+', 0)
ELSE ''
END
END
ELSE ''
END as pop
FROM input

Excel 2013 VBA Like condition with colon won't work

The value of gettxt = 'ATCH 6: Page 2 of 2
gettxt values are from text formatted cells.
I cannot get the following Like condition to be true.
If LCase(gettxt) Like "*atch #*:" Then ...
Suggestions on how to fix the Like statement are appreciated.
You're just missing one more "*" after the colon.
Your condition states that there shouldn't be anything AFTER the colon, but your "gettxt" value has something in it after that.
So the correct condition should be:
If LCase(gettxt) Like "*atch #*:*" Then
Full working version:
Sub test()
gettxt = "ATCH 6: Page 2 of 2"
If LCase(gettxt) Like "*atch #:*" Then
Debug.Print "Working!"
Else
Debug.Print "Not Working!"
End If
End Sub
Hope this helps!

Case Statement in VBA

Case statement below is not working when the condition met.
Dim TemplatePick As String
Select Case TemplatePick
Case OptCreate.Value = True
Call WebFormInfo
Case OptModify = True
Call ModifyTemplate
' many more case statement to come
End Select
Select case is used to test the one value in this case TemplatePick then the Case would be Case "A" which would fire when TemplatePick = "A"
So for this to work:
Select Case True
Case OptCreate.Value
Call WebFormInfo
Case OptModify
Call ModifyTemplate
' many more case statement to come
End Select
Now one caveat with Select Case, once it finds a match it ignores all others. In other words if OptCreate.Value is True then it will stop and not test whether OptModify is True.
Your case test expression (TemplatePick) is not the same as your expresion list (OptCreate.Value, OptMOdify). I have a hard time even understanding what your are trying to do. Properly structured it would look something like this:
Dim TemplatePick As String
Select Case TemplatePick
Case "Template 1"
Call WebFormInfo
Case "Template 2"
Call ModifyTemplate
...
case Else
'Do default behavior
End Select
More resources https://msdn.microsoft.com/en-us/library/cy37t14y.aspx

VBA in Excel v2010 - Select Case command within another Select Case

I have a Excel v2010 Project I'm trying to complete, and I'm trying to use the "Select Case" command within another "Select Case" .... at the moment it doesn't seem to work... so my question is.. can it actually work and I'm doing it wrong or should I replace it with "if-else-end if" ??
Select Case LCase(Cells(i, "B").Value)
Case LCase("ABC")
Select Case LCase(Cells(i, "C").Value)
Case "DEF"
x = x + 1
Thanks Guys :)
Sure you can. Read this article.
Actually, your code doesn't work, because LCase returns lower case, but your Case "DEF" is in upper case, so you need to slightly modify your code:
Select Case LCase(Cells(i, "C").Value)
Case "def"

Excel - VBA : Simplify this If statement comparing cell to words

Easy question here.
I am currently using this in my program :
If LCase(inp_rng.Offset(1, 0).Value) = "street" Or LCase(inp_rng.Offset(1, 0)) = "ave." Then
score = score - 50
End If
It is obviously not clean but I can find a way to put it in one sentence only. What is the programming way of writing something like this:
If LCase(inp_rng.Offset(1,0).Value = ("street", "ave.", "road", "...", etc.) Then
'do something
End If
Thanks in advance!
You can use Select Case statement instead:
i = LCase(inp_rng.Offset(1,0).Value
Select Case i
Case "street", "ave.", "road"
'do something
Case Else
'do something
End Select
Alternatively you can populate all possible answers in an array and search the array for a match.
You may use Filter() array function
http://msdn.microsoft.com/en-us/library/office/aa164525(v=office.10).aspx

Resources