I need to write a case statement in Log Parser studio but with a less than clause
I've managed to write the case statment
"Select
Case time-taken
when 1000
then 1 else 3
end"
And I have written log parsa with <= as part of the where clause it just won't work when I try and combine them
"Select
Case time-taken
when <=1000
then 1 else 3
end"
----- I even tried
"Select
Case time-taken
when between 0 and 1000
then 1 else 3
end"
"Select
Case time-taken
when 1000
then 1 else 3
end"
Error Parsing Query: syntax error : Cannot find a valid
: <=1000 [SQL query syntax invalid or unsupported]
Try something like this:
SELECT cs-uri-stem, stats, COUNT(*)
using
CASE DIV(time-taken, 1000) WHEN 0 THEN 'Less than 1' WHEN 1 THEN 'Less than 2' WHEN 2 THEN 'Less than 3' else 'greater than 3' end as stats
FROM '[LOGFILEPATH]'
where cs-method = 'POST'
GROUP BY cs-uri-stem, stats
ORDER BY cs-uri-stem, stats
Related
Below is what my data looks like for the case when statement:
0
1
NULL
Not Obtainable
NULL
Not Obtainable
2
0
NULL
1
NULL
NULL
Here is the query I am trying to use:
SELECT
*,
CASE
WHEN ER_MENTAL > 1 THEN 'Yes'
WHEN ER_MENTAL < 1 THEN 'No'
WHEN ER_MENTAL = 'Not Obtainable' THEN 'No'
END AS ER_MENTAL_RESP
FROM
#HLQ
This is the error I'm getting:
Conversion failed when converting the varchar value 'Not Obtainable' to data type int.
I've tried various version of CONVERT in front of each case when with no luck
If you need a version that will work on earlier versions of SQL Server then using ISNUMERIC should work:
select er_mental,
case when ISNUMERIC(er_mental) = 1 then
case when er_mental > 1 then 'Yes'
when er_mental < 1 then 'No'
end
when er_mental = 'Not obtainable' then 'No'
end as er_mental_resp
from #hlq;
Your column is VARCHAR so you can't compare it to an INT without converting it. You can use TRY_CONVERT since you are using 2012 which will avoid your error. You could also compare it to it's varchar equivalent, like ER_MENTA > '1' but you could run into issues since varchar's aren't compared the same way. i.e. 11 is < 2 when using varchar. select 1 where '11' < '2'
SELECT *,
CASE WHEN TRY_CONVERT(INT, ER_MENTAL) > 1 THEN 'Yes'
WHEN TRY_CONVERT(INT, ER_MENTAL) < 1 THEN 'No'
WHEN ER_MENTAL = 'Not Obtainable' THEN 'No'
END AS ER_MENTAL_RESP
FROM #HLQ
I am trying to execute the following query in SSMS 2014. However, I am unable to access from the outer query, the column created using a case statement in the inner query. i.e.I am not able to access c.current.
Error obtained - Incorrect syntax near C
select C.trandate,C.plan,C.current from
(SELECT d.trandate,p.plan,
case when datediff(dd,trandate,getdate()) <=30 then d.amount else 0 end as 'Current',
case when datediff(dd,trandate,getdate()) between 31 and 60 then d.amount else 0 end as '31 to 60',
case when datediff(dd,trandate,getdate()) between 331 and 360 then d.amount else 0 end as '331 to 360',
case when datediff(dd,trandate,getdate()) > 360 then d.amount else 0 end as '>360',d.residentsys
FROM [HMXals_Reporting].[dbo].[TranARDetail] d
join [HMXals_Reporting].[dbo].[plans] p
on d.transys = p.plansys
) C
plan and current are both reserved keywords.
You'll have to go with
select C.trandate, C.[plan], C.[current] from
or choose other names.
pretty sure this is a pretty simple issue:
I need to create an exam results table for a student, but it needs to meet the conditions of If the student is awarded a grade of 70% or more then the result is to be shown as 'Distinction', a grade of at least 50% but less than 70% is to be shown as 'Pass' and grades below 50% are to be shown as 'Fail'. If the student has not taken the examination then the result is shown as 'Not taken'.
I'm having issues with my code which i can't seem to get right:
CREATE VIEW results_table
AS SELECT entry.excode, sname, student.sno, entry.egrade, result AS entry
FROM student
JOIN entry
ON student.sno = entry.sno
GROUP BY entry.excode, sname, student.sno, entry.egrade, result
SELECT result
CASE result
WHEN entry.egrade >= 70 THEN 'Distinction'
WHEN entry.egrade >= 50 THEN 'Pass'
WHEN entry.egrade < 50 THEN 'Fail'
WHEN entry.egrade = NULL THEN 'Not Taken'
END;
ORDER BY entry.excode,sname
could somebody please tell me where i'm going wrong?
There are two types of SQL CASE statement, as documented for PostgreSQL here:
CASE WHEN condition THEN result ELSE default END
CASE expression WHEN value THEN result ELSE default END
The first form tests a series of (unrelated) conditions, and returns the result for the first one which is true, or the ELSE if none are. The second form is a short-hand for testing multiple conditions against a single value; CASE x WHEN y THEN 'y' WHEN z THEN 'z' END is short-hand for CASE WHEN x = y THEN 'y' WHEN x = z THEN 'z' END.
In your code, you're muddling the two syntaxes; you can't use the short-hand because you're doing something more complex than = in the conditions, so you need to leave out the word result:
CASE
WHEN entry.egrade >= 70 THEN 'Distinction'
WHEN entry.egrade >= 50 THEN 'Pass'
WHEN entry.egrade < 50 THEN 'Fail'
WHEN entry.egrade IS NULL THEN 'Not Taken'
END
(Note: I've also changed = NULL to IS NULL, since no value is ever "equal to" NULL, because it means "we don't know what the value is equal to".)
You then need to put that into a SELECT query in the same place you'd select a normal column.
So starting from this simple query...
SELECT
entry.egrade as result
FROM
entry;
...you can put your CASE statement in like this:
SELECT
CASE
WHEN entry.egrade >= 70 THEN 'Distinction'
WHEN entry.egrade >= 50 THEN 'Pass'
WHEN entry.egrade < 50 THEN 'Fail'
WHEN entry.egrade IS NULL THEN 'Not Taken'
END as result
FROM
entry;
I am using SSMS 2008 R2 and am trying to figure out the SQL select statement to select all records where two or more of the values are found.
These are the four possible values I am looking for. If two or more of these values (SubstanceAbuse, BehaviorEmotion, SexualAbuse, DomesticViolence) are met, I want to set a new field to 1. How do I do this?
case when qav.[test_setup_details_caption] in ('Substance Abuse / Drug Use','Caregiver monitor youth for drug alcohol use') then 1 else 0 end SubstanceAbuse,
case when qav.[test_setup_details_caption] in ('Physical Aggression','Firesetting','Gang Involvement','Runaway Behavior') then 1 else 0 end BehaviorEmotion,
case when qav.[test_setup_details_caption] = 'Problem Sexual Behavior' then 1 else 0 end SexualAbuse,
case when qav.[test_setup_details_caption] LIKE '%Domestic%' then 1 else 0 end DomesticViolence,
My suggestion would be to take the above statement and make it a virtual table in a new SELECT statement. Then you can do a SUM on the ones (since they are calculated already) in your WHERE statement and display only
where (Sub + Beh + Sex + Dom) > 1
It would look something like this (pseudo-code):
SELECT t.*
FROM (SELECT sub case, Beh case, etc.
FROM yourtable) t
WHERE (t.sub + t.Beh + t.Sex + t.Dom) > 1
It seems like all you need is this WHERE clause:
WHERE SubstanceAbuse + BehaviorEmotion + SexualAbuse + DomesticViolence > 1
update myTable set myField = 1 where 2 <= (select SubstanceAbuse + BehaviorEmotion + SexualAbuse + DomesticViolence from ...)
Of course, this is just a template for your query, but you get the idea. If the answer is still unclear then I kindly ask you to give me more details.
Best regards,
Lajos Arpad.
Can anyone tell me how to translate the following T-SQL statement:
SELECT fileld1 = CASE
WHEN T.option1 THEN -1
ELSE
CASE WHEN T.option2 THEN 0
ELSE 1
END
END
FROM Table1 AS T
The point is I need to validate two different options from the table for a single field in the select statement..
I have tried to do somthing with an IF statement in pl/sql, but it just doesnt work for me:
SELECT IF T.option1 THEN -1
ELSE IF T.option2 THEN 0
ELSE 1
END
FROM Table1 AS T
I am not actually sure how to write IF statement inside the SELECT statement..
And also, I need to do it INSIDE the select statement because I am constructing a view.
Use:
SELECT CASE
WHEN T.option1 = ? THEN -1
WHEN T.option2 = ? THEN 0
ELSE 1
END AS field1
FROM Table1 AS T
I can't get your original TSQL to work - I get:
Msg 4145, Level 15, State 1, Line 4
An expression of non-boolean type specified in a context where a condition is expected, near 'THEN'.
...because there's no value evaluation. If you're checking if the columns are null, you'll need to use:
SELECT CASE
WHEN T.option1 IS NULL THEN -1
WHEN T.option2 IS NULL THEN 0
ELSE 1
END AS field1
FROM Table1 AS T
...or if you need when they are not null:
SELECT CASE
WHEN T.option1 IS NOT NULL THEN -1
WHEN T.option2 IS NOT NULL THEN 0
ELSE 1
END AS field1
FROM Table1 AS T
CASE expressions shortcircuit - if the first WHEN matches, it returns the value & exits handling for that row - so the options afterwards aren't considered.
If I remember correctly, PL/SQL also supports the case. You just would have to move the column alias from "field1=" before the expression to "AS filed1" after the expression.