I have a stored procedure which has a insert statement.The procedure executes an insert statement when the user provides all the values as 0. If the user provides differnt values then a error statement should be returned.
case when #test1=0,#test2=0,#test3=0 then
{Insert statement}
Else
Case when #test1=0,#test2=0,#test3=1
{select “All cases should be 0”}
Else
Select “Please provide data”
In this case I thought case conditions works fine but after reading documentation and some other links it seems syntactically false to write such a statement.
Anyother possible way to solve the problem.
For control flow in T-SQL, use if, not case. Something like this:
if #test1=0 and #test2=0 and #test3=0
begin
{Insert statement}
end;
Else if #test1=0 and #test2=0 and #test3=1
begin
{select “All cases should be 0”}
end
Else
begin
Select “Please provide date”
end;
You can use IF and to check each variable with 0 value then COALESCE and NULLIF are helpful as below:
IF (#test1 = 0 AND #test2 = 0 AND #test3 = 0)
BEGIN
INSERT INTO tmpTable(val1)
SELECT val1 FROM <table_name>
END
ELSE IF COALESCE(NULLIF(#test1, 0), NULLIF(#test2, 0), NULLIF(#test3, 0)) IS NOT NULL
BEGIN
SELECT 'All cases should be 0'
END
ELSE
BEGIN
SELECT 'Please provide data'
END
I have written a stored procedure that accepts one parameter.
Request_ID int = 0
Later, in my WHERE clause, I need to accomplish the following.
I don't know how to describe it, but you can see what I'm trying to do with this completely made up pseudo-code:
WHERE isNull(a.Target_Department, '') <> ''
AND isNull(a.Resolved_Date, '') = ''
AND isNull(c.Request_Archived, '') <> 'Y'
AND IF #Request_ID = 0
THEN Request_ID > 0
ELSE Request_ID = #Request_ID
END
Basically, if a specific request_ID is supplied, I want the WHERE clause to narrow to only that specific Request_ID. Otherwise, I want all of them.
Any ideas on how to accomplish this?
Try this -->
WHERE
(#Request_ID IS NULL OR Request_ID=#RequestID)
OR
WHERE
(#Request_ID=0 OR Request_ID=#RequestID)
and isnull(nullif(#RequestId,0),RequestId)=RequestId
Hi I have an update query where if one field is null then I want value goes to another field
here is an example of what I would like to do:
UPDATE Table SET
CASE WHEN Column1 is NULL
THEN Column2 = #Update
ELSE Column1 = #Update
END;
Now this does not work as I get an error on the word case but is there a way to do what I am try to accomplish? I know it could be done if I used two update statements with a where but was wondering if it could be done in one query?
You are using incorrect syntax for CASE in SQL statements. CASE must return an expression, it cannot contain a statement in pure SQL.
update Table
Set Column1 = Case when Column1 is NULL Then NULL Else #Update END,
Column2 = Case when Column1 is NULL Then #Update Else Column2 END
My query like this
case when statement1 = statement2 then offer1
if offer1 is have value means then i need to display offer1 value will be 'Yes'
How to write the query for this?
You can nest multiple CASE expressions like so:
CASE
WHEN statement1 = statement2
THEN
CASE WHEN offer1 IS NOT NULL THEN 'Yes' ELSE ... END
END
You can use Stored procedures and return a value depending on the conditions you need, in the stored procedures you can design your conditions using normal if statements, take a look at this example from here:
Create procedure dbo.Prc
#Value varchar(50),
#Result bit OUTPUT
AS
Begin
If exists (select 1 from YourTable where Field=#Value)
set #Result=1
Else
set #Result=0
End
I tried to ask this question before but I don't think I explained myself very well. So here it is: asp.net 2.0 app hitting a SQL 2008 backend. This seems simple but I can't get it. 1 table. The user selects a status. The query should return all records = the chosen status only. If the user select "All Status", then ALL records should be returned, including those with a status = null (which is the part that is hosing me).
Ex:
CASE 1: User selects Status = "Satisfied"; ONLY satisfied records are return
CASE 2: User selects All Status = everything is returned, satisfied AND nulls and anything else
I tried passign in a wildcard but this doesn't return nulls. I tried dynamically buildign the query but I would like to avoid it.
For the case where you want them all, how about: (am I missing something?)
SELECT * FROM <tablename>
SELECT * FROM <tablename> WHERE Status = '*' OR Status IS NULL
...or '%' or whatever wildcard your SQL implementation uses.
...or if you really have no other conditions following this, just select *...
Try this in a stored procedure.
--Pass in #status as a parameter
DECLARE #Status varchar(100)
IF #Status = 'All Status'
BEGIN
SELECT * FROM tablename
END
ELSE
BEGIN
SELECT * FROM tablename where statusfield = #Status
END
One option is
declare #status varchar(50)
SELECT * FROM <tablename> WHERE (#status is null) or (Status = #status)
if you pass null in for the #status parameter then it will return all records. If you pass 'satisfied' or whatever then it will return just those matchng records.
If doing this in SQL 2008, be sure you have SP1 and Cumulative Update 5 installed. Further, I would recommend adding the WITH RECOMPILE option to the procedure. Under those conditions it will be as performant as embedded SQL or even using unions.
See the following article for an indepth discussion of the myriad of ways to perform searching in SQL 2008: Dynamic Search Conditions in T-SQL
The problem is that SQL's = operator always returns NULL when one of the operands is NULL, so using status = '%' indeed doesn't work. The best method is to just not include a condition on status if you want all of them. You can add extra NULL tests to the query, but that again is building it dynamically, I don't see a way to avoid that...
basically your statement will be for 'Statisfied'
SELECT * FROM testtab WHERE
COALESCE(statuscolumn, '') LIKE '%Statified'
for 'All Status' it will be
SELECT * FROM testtab WHERE
COALESCE(statuscolumn, '') LIKE '%'
you could use this statment and if selection is 'All Status' then pass a '' for the #status from your UI
SELECT * FROM testtab WHERE
COALESCE(statuscolumn, '') LIKE '%' || #status
Or you can use this one and when you pass the selection from UI make sure it has a '%' (wild char) appended to your status when it not 'All Status'. When its 'All Status' just pass '%' for the #status
SELECT * FROM testtab WHERE
COALESCE(statuscolumn, '') LIKE #status
oh your db is mssql? :) then you will need to replace the collace(statuscolumn, '') with isnull(statuscolumn, '').
Just skip the where clause or the part that is about the status field, example:
SELECT * from table_1 Where status = 'Satisfied'
and
SELECT * from table_1
When you want all records, you have to exclude STATUS from your WHERE clause (or use a UNION and a select statement where STATUS IS NULL).
Depending on what version of SQL you are using, you might be able to use an IF..ELSE... statement.
IF Status='ALL' THEN
... A SELECT statement where STATUS is NOT included in the WHERE
ELSE
... A SELECT statement that has a WHERE with only the status you are looking for
SELECT * FROM <tablename> WHERE isnull(Status,'*') = '*'
Assuming you're passing a variable to an SP:
SELECT * FROM Table WHERE Status CASE #status WHEN 'All status' THEN Status ELSE #status END
Otherwise, you need to concatenate in the selected value within quotes at both places where it currently says #status
The idea is what to do when the users chooses 'All Status'. By setting the param to NULL, you can use the isnull and then each [status] field just needs to equal itself. I've used ISNULL to set to '' to avoid having NULL = NULL>
declare #param_choice varchar(25)
if #param_choice = 'All Status'
Begin
#param_choice = NULL
End
-- get your results
Select * from Some_Table
Where IsNull([Status], '') = IsNull(#param_choice, IsNull([Status], ''))
You'll get the best performance from:
IF #status IS NULL
BEGIN
SELECT t.*
FROM TABLE t
END
ELSE
BEGIN
SELECT t.*
FROM TABLE t
WHERE t.status = #status
END
The next option is to use:
SELECT t.*
FROM TABLE t
WHERE (#status IS NULL OR t.status = #status)
...but that is not sargable.
I do not believe that anyone suggested using a UNION.
SELECT t.*
FROM TABLE t
WHERE (#status IS NULL)
UNION ALL
SELECT t.*
FROM TABLE t
WHERE (t.status = #status)
If #status is NULL, then the first query in the union is executed, and t.status = #status is clearly always false, so the second query in the union is not executed at all.
If #status is not null, then the first query in the union is not exected at all, and the second one is.
Importantly, since ISNULL, COALESCE or a function are not used on t.status or on #status, then if there is an index on status, it can be used. That is, the predicate is SARGABLE.
And I used UNION ALL (instead of UNION) to prevent a SORT and DISTINCT operation that can be very slow.