SQL if condition not met then fail the query - sql

So I am trying to figure out how to fail a SQL statement incase the case - when statement is not met.
I have been doing some searching and didn't find anything useful
this is the query for example ..
select
case when 1 = 1 then 'ok'
else < what to write here?>
end
If I am writing a typo or something like that this will fail the query entirely for syntax even if the condition is met .
Hope I could use your help !

Related

What does XI8 mean in a SQL query? (Teradata/Big Query)

At work I saw I saw a SQL query in Teradata that had the following:
CASE WHEN (sp_ce / ‘0000001000000000’XI8) MOD 2 = 1 THEN ‘Y’ ELSE ‘N’ END AS res_p
What is the meaning of XI8? What is it doing to the number string?
Also when I tried to run this in Big Query it’s not supported, it errors out with
Syntax error: Unexpected identifier \XI8\
How would I write that case statement in big query?
Using MOD on big query without the numbers as a string and without the XI8 yields different results.
Thank you

Syntax Error in Simple Case Statement

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.

PostgreSQL query for this case?

I have this below case statement in postgreSQL.
select eng.id,
case et.id
when 2 then cwd.rate::text
when 1 then cwd.amount::text else 'SCD' end terms
I need to put some text like 'WC' before cwd.rate. Its giving me syntax error. Any idea how do I do this.

Firebird If-Else with Queries

I have the following problem:
I'd like to use a specific query depending what number a user has set in Firebird SQL, so in pseudo code:
IF (TABLENAME.USERFLAG <> 5 WHERE TABLENAME.USER = 15555) THEN
EXECUTE QUERY 1
ELSE
EXECUTE QUERY 2
The problem is, that does not seem to work. What could I do to get it working?

Why is my UPDATE query returning fewer results than the equivilent SELECT query?

I'm trying to run an UPDATE query in Access 2010 to remove trailing spaces from a field. Before running the full query, I'm writing a test query limited by an ID that returns 1 result, in case anything goes horribly wrong.
The SELECT version of the query returns one result, as expected:
SELECT dbo_Contact.ContactID, dbo_Contact.Pref
FROM dbo_Contact
WHERE (((dbo_Contact.ContactID)=11906) AND ((dbo_Contact.Pref) Like "% "));
However, when I change it to an UPDATE query, it says "You are about to update 0 row(s)."
The UPDATE query is below:
UPDATE dbo_Contact SET dbo_Contact.Pref = Left([Pref],(Len([Pref])-1))
WHERE (((dbo_Contact.ContactID)=11906) AND ((dbo_Contact.Pref) Like "% "));
What am I doing wrong? Being that the WHERE filter is the same, I'm assuming it's in the expression I'm using as the value to update to. If this is the case, what's wrong with that?
For the most part, the Access wildcard is *, not %. There are exceptions, but I am not sure if you have such a setup.