SQL SERVER CASE STATEMENT CLARIFICATION - asp.net

I have to execute a statement like ( i need and keyword along with when ).
select
'Is Allowed'= case A.Column
when
A.Column='XXXX' and Isnull(A.AnotherColumn,'')<>'' then 'Yes'
else
'No' end from TableName
I am getting syntax error,how to rewrite it without affecting the condition.

Try:
select case when A.Column='XXXX' and Isnull(A.AnotherColumn,'')<>'' then 'Yes'
else 'No' end as 'Is Allowed'
from TableName

SELECT
CASE A.Column
WHEN 'Is Allowed THEN 'First'
WHEN 2 THEN 'Second'
WHEN 3 THEN 'Third'
ELSE 'Other'
END
Is the general way of making a CASE (which is your question). However, your query/logic looks a bit convoluted. More detailed answer / query is possible, but would perhaps use more statements/ nested CASE.

Have a closer look at CASE (Transact-SQL)
SELECT CASE
WHEN some boolean expression
THEN value
ELSE default value
END
or
SELECT CASE value to check
WHEN vlue to check agains
THEN value
ELSE default value
END

Related

Teradata Case Expression and Text Comparison <> Not Working

I am trying to run a Case expression that will return a value when a field does not equal a specific text value.
Specifically, I am trying to return 'YES' when the field does not equal 'Yes'. The current value is null. I would expect the result to return 'YES' since the value does not equal 'Yes'. The query is returning 'NO'.
The excerpt of my query is listed below. Any help would be greatly appreciated.
SELECT
CASE
WHEN "Renewal"<>'Yes'
THEN 'YES'
ELSE 'NO'
END
FROM "DATABASE"."TABLE"
WHERE "Asset_Id" = '006817'

Sqlite3 Case statement weirdness

I have the following case statement on a TEXT file and I'm trying to get it to say '' instead of none.
CASE CustomField1
When 'None'
then ''
Else CAST(CustomField1 as INT)
End ReceiptLoc
Can anyone see anything wrong with this. If the column is NULL then put nothing in the column or empty string is fine. I've tried replacing 'None' with NULL and NaN. What am I missing?
I'm more of a SQL Server person so This is a little different for me.
You could use searched CASE expression:
CASE WHEN CustomField1 IS NULL THEN ''
WHEN CustomField1 = 'None' THEN ''
Else CAST(CustomField1 AS INT)
End ReceiptLoc
db<>fiddle demo

SQLite - update command yields null values

I'm having trouble performing some update on a SQLite database. I'm using the SQLite 3 shell for Windows.
I'm running the following command:
update resovled_chrom_counts set genus =
case resolved_name_full
when resolved_name_full is not null and resolved_name_full != ''
then substr(resolved_name_full,0,instr(resolved_name_full,' ')-1)
else
substr(original_name,0,instr(original_name,' ')-1)
end;
It seems to work on most rows, but some simply end up with a null value in their genus field. I tried checking some of them manually, by using the 'id' field of this table. For example, I found out that the row with id='kew-1' is null in it's genus field, and ran the following query:
select substr(resolved_name_full,0,instr(resolved_name_full,' ')-1)
from resovled_chrom_counts
where id='kew-1';
and to my surprise, I got a result (not null)!
Looks like the query works under the 'select' statement, but not under the 'update' statement.
Can anyone give an explanation and/or a solution?
Any help would be appreciated. Thanks!
The problem is not with the substr(resolved_name_full... but with the CASE.
A CASE expression can have two different forms:
CASE x WHEN y THEN ...: This compares the value of x against the value of y.
CASE WHEN a THEN ...: This checks whether the value of a is true or false.
The problem in the UPDATE statement is that there is a value (resolved_name_full) directly after the CASE, so the value of resolved_name_full is compared with the value of the expression resolved_name_full is not null and resolved_name_full != '', and this comparison always fails because resolved_name_full never happens to be 0 or 1.
Just use the second form of the CASE expression:
update resovled_chrom_counts set genus =
case
when resolved_name_full is not null and resolved_name_full != ''
then substr(resolved_name_full,0,instr(resolved_name_full,' ')-1)
else
substr(original_name,0,instr(original_name,' ')-1)
end;
SQLFiddle

pl sql decode function for use to check two values

In the select statement i need to return a value based on the data on other two colomns. For example,
select clarify, clarify_rece_date, clarify_process_date
from test_db;
So the clarify should return yes if both clarify_rece_date and clarify_process_date is not null and if they null clarify should return No.
I could have use decode here if i want to check only onle column as below,
select decode(clarify_rece_date, null,'Yes','No') clarify, clarify_rece_date, clarify_process_date
from test_db;
But how can i check for both columns in this scenario?
You could have a rather complex DECODE. However, it would make far more sense to use a CASE statement
SELECT (CASE WHEN clarify_rece_date IS NOT NULL AND
clarify_process_date IS NOT NULL
THEN 'Yes'
ELSE 'No'
END) clarify,
clarify_rece_date,
clarify_process_date
FROM test_db

SQL: Not equal operator Problem

I am using a not equal operator <> in my sql statement but it doesn't retrieve any record which is not equal to the selected date.
CODE:
Command = New SqlCommand("SELECT * FROM [Products] WHERE [ParkingStartDate] <> #StartDate", myConn)
Command.Parameters.AddWithValue("#StartDate", StartDate1)
This won't return anything if either of the following is true:
StartDate1 is a NULL
ParkingStartDate for all values is a NULL or equal to StartDate1 (obvious one)
Check that you are passing a non-NULL value in StartDate1 and there are records satisfying your condition.
If the values are null you would have to do
Command = New SqlCommand("SELECT * FROM [Products] WHERE [ParkingStartDate] <> #StartDate OR ParkingStartDate is null", myConn)
Command.Parameters.AddWithValue("#StartDate", StartDate1)
First stop using that <> operator.
Use instead != (NOT EQUAL)
run this statement in sql. it will return zero results. to illustrate my point.
select '1' where NULL <> 0
instead use
where columname != #startdate or columnname is null
One important thing to take into consideration when dealing with querying based on date is that the date in SQL Server is treated as exact as the date you send in. So, if you pass in a full date/time, like 2011-10-24 14:35:29, it will return all dates that are not that exact date. If you are looking for a particular portion of that date to be selected against, you need to only give that portion of the date. Using the DATEPART command will help here also.
If the value is undefined, it is not included in <> or != clause.
Along with these you can use sql function 'COALESCE()' to include rows having undefined cells.
"SELECT * FROM [Products] WHERE COALESCE([ParkingStartDate],'') <> #StartDate OR ParkingStartDate is null"
Hope it will help you.
My recommendation would be to try with NULLIF operator. Modify your query to be like :
SELECT * FROM [Products] WHERE NULLIF([ParkingStartDate], #StartDate) IS NOT NULL OR ParkingStartDate is NULL
Hope this helps.

Resources