Here is an issue that seems like it should be a simple solve but I have been working on it some time and cannot figure out why I cannot seem to combine CASE in one area of the query and IF in another.
Does anyone see what is going on here? I have an old data set that needs to be converted to work with new tables. The data is pulled from WDDX and put in a temp table. That's all working properly.
Here is where I am running into trouble. I need to extract a value to a new column called DetailValue when XXXX appears, and in such cases, the value appearing after XXXX is a number that belongs to DetailValue, then it is followed by _, and in such case, the number appearing after _ belongs in RiskValue.. Also, when ZZZZ appears, the following character belongs in DetailValue and it is always the last character.
When it's simply CASE, everything works fine, but when I add IF to grab a value, it tells me:
"Incorrect syntax near the keyword 'IF'. Msg 156, Level 15, State 1, Procedure OldSysDataConv Incorrect syntax near the keyword 'THEN'.
Code is:
SELECT VarName,
CASE
WHEN (CHARINDEX('XXXX', VarName) > 0 and SUBSTRING(VarName, CHARINDEX('XXXX', VarName), len(VarName)) like '%XXXX%') or SUBSTRING(VarName, CHARINDEX('ZZZZ', VarName), len(VarName)) like '%ZZZZ%' then
left(replace(left(replace(VarName, 'XXXX', ''), len(VarName)-4), 'ZZZZ', ''), (len(VarName)-5))
else
null
END as DetailName,
IF CHARINDEX('ZZZZ', VarName) > 0 THEN
right(VarName, 1)
END
as DetailValue,
There is no such thing as IF ... THEN ... END expression in pure SQL. This is a procedural statement that is used in stored procedure.
You want either IIF(), which is T-SQL specific:
IIF(CHARINDEX('ZZZZ', VarName) > 0, right(VarName, 1), null) as DetailValue
Or the more standard CASE :
CASE WHEN CHARINDEX('ZZZZ', VarName) > 0 THEN right(VarName, 1) END as DetailValue
I solved my own problem by simplifying my CASE statement. Some basic logic had simply slipped by me during a long day. ;)
Answer:
CASE
WHEN CHARINDEX('XXXX', VarName) > 0 AND CHARINDEX('ZZZZ', VarName) <> 0 THEN
substring(VarName, (CHARINDEX('_', VarName)-1), len(VarName))
ELSE
right(VarName, 1)
END
as DetailValue,
I have the following SQL query (in PostgreSQL) and I keep getting a syntax error.
SELECT data1, data1_class
CASE
WHEN data1 LIKE '%Bookmarked Removed%' THEN 'Class I'
ELSE 'Class II'
END
AS data1_class
from events
WHERE event_code = 11
The syntax error I get is:
ERROR: syntax error at or near "CASE"
LINE 2: CASE
^
********** Error **********
ERROR: syntax error at or near "CASE"
SQL state: 42601
Character: 31
To me, I don't think that there is an error and this should work however after trying to work it out a few times I can't it to work.
What am I missing from my CASE statement to make it work?
You are missing a comma before the CASE statement. It should be e.g.
SELECT data1, data1_class,
CASE
...
You are missing a comma before the CASE statement. It should be e.g.
SELECT data1, data1_class,
CASE
...
Though you repeat the data1_class as an alias for your case statement, so perhaps you mean just:
SELECT data1,
CASE
...
I am new to SQL plus . Could anyone help me figure out the syntax error for my code?
CREATE OR REPLACE VIEW BR_STATUS AS
SELECT CARTS_PER_CUSTOMER.loginName,CARTS_PER_CUSTOMER.number_of_carts,
CASE WHEN (number_of_carts < 1 ) THEN 'BR-1 Satisfied.'
ELSE 'BR-2 violated.'
END AS 'BR-status'
FROM CARTS_PER_CUSTOMER;
Whenever I try to run this part of the code, I get this error message
ORA-00923: FROM keyword not found where expected.
I followed several oracle documentation for CASE, but can't figure out what I am writing wrong. Any suggestions would be appreciated.
There is a unnecessary comma before FROM clause nothing wrong with CASE statement. Also use double quotes for Alias name instead of single quotes (thanks to Jarlh)
SELECT CARTS_PER_CUSTOMER.loginName,
CARTS_PER_CUSTOMER.number_of_carts,
CASE
WHEN ( number_of_carts < 1 ) THEN 'BR-1 Satisfied.'
ELSE 'BR-2 violated.'
END AS "BR-status" --Remove the comma here
FROM CARTS_PER_CUSTOMER;
Query is :
select * from Test.dbo.Test_Spoc where spocNo = 54986
In SQL 2008 its throwing error Incorrect syntax near ' '. IF I remove the space before Where and again give the space its working.
In SQL 2012 this query is showing red sign also before Where that problem is in this space.
I want to know what can be reason that earlier space is not working?
select * from Test.dbo.Test_Spoc where spocNo = '54986'
Add single quotes, I think this might be the issue; you check the code
I keep getting a error message when I try to use the between operator in SQL Server 2012.
select *
from Sales.Store
where SalesPersonID>283
order by Name between 'g' and'j' desc;
Error:
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'between'.
I'm not sure what I'm doing wrong. Has anyone seen this? Any suggestions
SQL Server doesn't allow you to order by a Boolean you can use
select *
from Sales.Store
where SalesPersonID>283
order by IIF(Name between 'g' and'j', 0,1);
But between probably doesn't have the semantics you want anyway.
You could use IIF(Name LIKE '[g-j]%',0,1) instead if you want to include all names beginning with j in the group ordered first.
But after your comment this is what you need
select *
from Sales.Store
where SalesPersonID>283 AND Name LIKE '[g-j]%';
You should not use between as that would only include names that were exactly "j" not all those beginning with "j".