SELECT
name,
type,
(case when to_char(expiresdate)>to_char(now())
then active
else
expired end
) as 'membership status',
expiresdate
FROM
membership
I got an syntax error on this sql but I can't figure out where I did wrong
Try this:
SELECT
name,
type,
case when to_char(expiresdate)>(now())
then active else
expired end as 'membership status',
expiresdate
FROM membership
My best guess is that it's from using single quotes for the expression alias. Try "membership status" or [membership status]
(case when to_char(expiresdate)>(now())
then active
else expired end)
as [membership status]
(Broken into individual lines for better clarity)
An explanation: the single quotes make 'membership status' into a string literal. That can't be used as an alias. Aliases can be a single word (beginning with a letter), a series of words in double quotes, or a series of words in square brackets.
So all these would work:
... as membershipstatus
... as [membership status]
... as "membership status"
This may not be the only problem with your SQL statement - others have pointed out that you're doing weird things with dates and strings - but I believe this is why you're getting a syntax error specifically, which is what you asked.
Related
i.e when location_old='r-0000' then location_new='r-0280' otherwise for anyother value of location_old the corresponding value of location_new be same.
select year, month, amount, account, location_old, costcentre_old, costcentre_new,
'location_new'= case when location_old='r-0000' then 'r-0280'
else 'loation_old'
end
from asign3;
Remove the single quotes from the identifiers. They should only be used for string literals. And unless you're using SQL Server, the syntax for an aliased column is <expression> [AS] <alias> not <alias> = <expression>. But as SQL Server also understands the latter, I'd recommend to use the latter at any rate.
SELECT year,
month,
amount,
account,
location_old,
costcentre_old,
costcentre_new,
CASE
WHEN location_old = 'r-0000' THEN
'r-0280'
ELSE
location_old
END location_new
FROM asign3;
Trying to use a Case Statement for the first time, which is why the code is so small/simple. Returning a syntax error, supposedly with my table name [Impact].
Select Case [Impact]
Case Is = 0
[New Impact] = "1"
End Select
Any assistance is appreciated. I've looked around for solutions, but most of the time answers are related to something else in their code, and not anything I have in this small test code.
In MS Access, the logic one would normally used is:
Select iif([Impact] = 0, "1", NULL) as [New Impact]
You could use switch() as well, but that seems like overkill.
The Microsoft Access Case statement can only be used in VBA code.
The structure is like so:
Select Case test_expression
Case condition_1
result_1
Case condition_n
result_n
[Case Else
result_else]
End Select
My guess, is you missed the part about using this through VBA only.
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;
I'm running into an ORA-00933: SQL command not properly ended error. What I am doing is querying on a table I built from a With query. I've been combing through Stack, trying to resolve this issue with every correctly answered question that I could find, but I still run into the error. It's something small and easy to fix I am sure. it's just beyond me at this point. The error occurs on the line with the second select statement:
;WITH sums
AS (SELECT a.client_number_id AS Client_Number_ID,
Count(Decode(a.sub_type_code, 'A', 1)) AS Applications,
Count(Decode(a.sub_type_code, 'S', 1)) AS License,
Count(Decode(a.sub_type_code, 'L', 1)) AS Lease
FROM mta_spatial.mta_acquired_tenure_svw a
WHERE a.tenure_type_description = 'Coal'
GROUP BY a.client_number_id)
SELECT client_number_id,
applications,
license,
lease,
applications + license + lease AS 'GrandTotal'
FROM sums;
applications + license + lease AS 'GrandTotal'
should be
applications + license + lease AS "GrandTotal"
quotes are for string
double quotes for field names.
Enclosing the alias in double quotes would make it case sensitive. In case you wish to make the alias case insensitive, write it without quotes. Oracle treats such cases as upper case by default. Therefore, Grandtotal, GRANDTOTAL,
grandtotal would all return the desired result.
applications + license + lease AS GrandTotal
Thanks everyone for their contribution. I solved it when I removed the semicolon before the 'WITH' statement. I borrowed the query from another stack thread
To calculate sum() two alias named columns - in sql
and modified it to my own use. They had used a semicolon, so I didn't think to remove it. I'm not sure why they had that there to begin with. this is my first attempt at using the 'WITH' clause.
Using a Case Statement in a query seems to work fine as long as the only case you want to match to is a string. But if you want to put ">" greater than (date or number), etc, SSMS automatically adds apostrophes around your statement (below) as if it were a string.
How can I get it to accept "greater than date" or "greater than number", etc??
There are two types of CASE expression:
The simple CASE expression compares an expression to a set of simple expressions to determine the result.
The searched CASE expression evaluates a set of Boolean expressions to determine the result.
You want a searched CASE expression. This means that the WHEN should come immediately after the CASE keyword.
CASE WHEN SoldDate <= SubjectDate THEN ...
There are two types of CASE expressions (from msdn):
A Simple CASE compares the specified expression for EQUIVALENCY ONLY
SELECT FieldName CASE WHEN 'Blah' THEN 'Foo' ELSE 'Bah' END
A Searched CASE can do inequalities too but has more verbose syntax:
SELECT CASE WHEN FieldName >= 'Blah' THEN 'Foo' ELSE 'Bah' END
You are trying to use the Simple syntax and need to use the Searched syntax by explicitly writing out the fieldname and comparison for each evaluation.
Your CASE statement syntax is incorrect. WHEN should come directly after CASE
This:
CASE SoldDate WHEN ...
Should be this:
CASE WHEN SoldDate <= SubjectDate THEN SoldFor ELSE EstSalePrice END
Additional Information
Take a look at the MSDN docs for additional examples of CASE syntax.