Getting error when using with keyword - sql

Msg 319, Level 15, State 1, Line 25
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
I am getting this error when I am using WITH keyword in CASE statement.
How can I solve that?
case
when 1
then I am with you
else I am not with you.

you need to enclose strings in single quotes 'like so'
case when 1 = 1
then 'I am with you'
else 'I am not with you.'
END
As it currently stands, SQL is trying to interpret "I", "am", "with", and "you" as commands/keywords/whatevers, when they are actually part of a string.

you can also use the 1 after when, no need to put 1=1 :
case
when 1
then 'I am with you'
else 'I am not with you.' end

Related

Strange issue combining IF and CASE in TSQL query

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,

Syntax error at or near “case” when using a case statement

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
...

Correct Syntax for this SQL 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;

SQL query is throwing this error Msg 102, Level 15, State 1, Line 1 Incorrect syntax near ' '

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

Msg 156, Level 15, State 1, Line 4 Incorrect syntax near the keyword 'between'

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".