I have researched everywhere but still can't seem to fix a simple error:
Running Microsoft SQL server:
UPDATE copyprogmaster
SET active =
CASE
WHEN active = 1 THEN active = 0
WHEN active = 0 THEN active = 1
ELSE active
END
WHERE source = 'Mass_Mail'
my error is :
Line 4: Incorrect syntax near '='.
Remove the = after the THEN, so:
UPDATE copyprogmaster
SET active =
CASE
WHEN active = 1 THEN 0
WHEN active = 0 THEN 1
ELSE active
END
WHERE source = 'Mass_Mail'
You already have active = after the SET on the second line.
You do not need to repeat "active =" after THEN
UPDATE copyprogmaster
SET active =
CASE
WHEN active = 1 THEN 0
WHEN active = 0 THEN 1
ELSE active
END
WHERE source = 'Mass_Mail'
Here's an example from the documentation at http://msdn.microsoft.com/en-us/library/ms181765.aspx
USE AdventureWorks2008R2;
GO
SELECT ProductNumber, Category =
CASE ProductLine
WHEN 'R' THEN 'Road'
WHEN 'M' THEN 'Mountain'
WHEN 'T' THEN 'Touring'
WHEN 'S' THEN 'Other sale items'
ELSE 'Not for sale'
END,
Name
FROM Production.Product
ORDER BY ProductNumber;
GO
Based on your query, I assume the active field is bit or int (assuming that int field has only values 0, 1, or NULL). In that case, I believe that you can write the query as following:
UPDATE dbo.copyprogmaster
SET active = active ^ 1
WHERE source = 'Mass_Mail'
Notice that the query can handle NULL values and also the rows #1, #4 and #6 in the screenshot are unchanged. Screenshot #1 shows table structure and screenshot #2 displays sample execution of the above query.
Hope that helps.
Screenshot #1:
Screenshot #2:
Related
I have 2 SQL tables one holding stock information then a style table which details how the stock is used. For some jobs the information is contained with a print file so the value in the stock table would be BIN, if not it would contain a template name which would cause a query to a Styles table which then shows how the stock is used. There a 10 positions that a stock could be applied but the template table wouldn't have records for where a stock couldn't be used (on the back of a simplex job for example).
I have a stored procedure which works to provide the detail based on whether the logic value is BIN or not but for the records in the style table that have no value I get NULL back which I need to suppress but just putting ISNULL around the column name in the subquery isn't having any effect. Am I just missing something as I'd rather not nest in another CASE statement to check if the query would produce NULL
WITH STYLES AS
(
SELECT
#JOBNAME AS JobName, Logic AS StyleName
FROM
[dbo].[CON_Tbl_511_DigitalStocks]
WHERE
JOBNAME = #JOBNAME
)
SELECT TOP 1
[Logic],
[S1F], [S1B], [S2F], [S2B],
[S3F], [S3B], [S4F], [S4B],
[S5F], [S5B],
CASE
WHEN b.stylename = 'BIN'
THEN --checks if there is a style for this job
CASE
WHEN S1F = '' THEN ''
ELSE '1'
END -- if a stock code is specified then return the bin name ("1")
ELSE (SELECT PAGE
FROM [dbo].[CON_Tbl_512_DigitalLogic]
WHERE STYLENAME = B.StyleName
AND STOCKREF = 'S1F')
END AS S1F_LOGIC, -- If a style is used return the style instruction for this bin and side
CASE
WHEN b.stylename = 'BIN' -- repeat this for all bins/sides
THEN
CASE WHEN S1B = '' THEN ''
ELSE '1'
END
ELSE (SELECT PAGE
FROM [dbo].[CON_Tbl_512_DigitalLogic]
WHERE STYLENAME = B.StyleName AND STOCKREF = 'S1B')
END AS S1B_LOGIC,
CASE WHEN b.stylename = 'BIN' THEN
CASE WHEN S2F = '' THEN ''
ELSE '2' END
ELSE (SELECT PAGE FROM [dbo].[CON_Tbl_512_DigitalLogic] WHERE STYLENAME = B.StyleName AND STOCKREF = 'S2F') END AS S2F_LOGIC -- this one returns NULL as there is no instruction required for "2SF"
FROM
[CON_Tbl_511_DigitalStocks] A
JOIN
STYLES B ON A.JOBNAME = B.JOBNAME
WHERE
A.JobName = #JobName
This code works fine until the last one as there is no value in the stockref column that says 'S2F' but putting SELECT ISNULL(PAGE, '') still returns NULL
Because this query is not finding any records:
SELECT PAGE FROM [dbo].[CON_Tbl_512_DigitalLogic] WHERE STYLENAME = B.StyleName AND STOCKREF = 'S2F'
and you want it to return something, you can change it to:
SELECT coalesce(min(PAGE),'') FROM [dbo].[CON_Tbl_512_DigitalLogic] WHERE STYLENAME = B.StyleName AND STOCKREF = 'S2F'
This will return the minimum value (min) of the found records, which will return NULL when noting found. The coalesce will turn this NULL into the empty string: ''
P.S.: see also SQL - Difference between COALESCE and ISNULL?
For testing in Toad, I have the following code
select ...
from ...
where term_code = :termcode AND
(
case
when :theSubject is not null then SUBJ_CODE = :theSubject
else 1 = 1
end
)
AND ptrm_code <> 8
In short: If theSubject is not entered (is null) I want to display all the courses, otherwise I want to display only those where subject_code is the same as the one entered in the variable window in Toad.
But I get an error:
[Error] Execution (77: 68): ORA-00905: missing keyword
in here:
when :theCourse is not null then sect.SSBSECT_SUBJ_CODE = theCourse
You can use boolean logic:
where
term_code = :termcode
and (:theSubject is null or subj_code = :theSubject)
I am trying to create an update statement on SQL Server to set one of the cards as default and all the others as not, however, if I have 3 cards and card 1 set to default and now I want to set card 2 as default I get all 3 cards set as updated, when I should only set cards 1 and 2. I am currently using the following query:
UPDATE Card
SET
ModifiedDateTimeUtc = #ModifiedDateTimeUtc,
IsDefault = CASE WHEN Id = #CardId THEN 1 ELSE 0 END
WHERE CustomerId = #CustomerId;
I need to modify this query so only the cards with values updated get to set a new modified date/time.
PS, I cannot use triggers on this database (unfortunately) so I need to find a solution using a "simple" SQL statement.
Maybe it could be write in other ways but you can do like this:
UPDATE Card
SET
ModifiedDateTimeUtc =
CASE WHEN Id = #CardId THEN
CASE WHEN IsDefault = 1 THEN ModifiedDateTimeUtc ELSE #ModifiedDateTimeUtc END
ELSE
CASE WHEN IsDefault = 0 THEN ModifiedDateTimeUtc ELSE #ModifiedDateTimeUtc END
END,
IsDefault = CASE WHEN Id = #CardId THEN 1 ELSE 0 END
WHERE CustomerId = #CustomerId;
What should do this?
If the Id is the same with the parameter and the old value, before update, is 1, datetime will be the same, no update will be made, else (means old value = 0) then it will be updated. And the same for old value= 0...
Is this what you want?
UPDATE Card
SET ModifiedDateTimeUtc = #ModifiedDateTimeUtc,
IsDefault = (CASE WHEN Id = #CardId THEN 1 ELSE 0 END)
WHERE CustomerId = #CustomerId AND
IsDefault <> (CASE WHEN Id = #CardId THEN 1 ELSE 0 END);
Usually, I wouldn't repeat a CASE expression in the WHERE. In this case, though, this is the new value for the column, so I think it is a clean expression of the logic.
I am using Visual Studio 2008 and am in a report trying to create a query that has a WHERE statement that will return all records, if the #parameter = 0, otherwise I want the query to return only records that have the #paramete.
Here is the statement I envision, but can't seem to get correct:
WHERE (ADVISOR.staffPersonID = CASE WHEN #advisor = 0 THEN **'Do NOT
filter by staffpersonID'** ELSE #advisor END)
I have tried the following code which doesn't work:
CASE WHEN #advisor = 0 THEN ADVISOR.staffPersonID ELSE #advisor END
ADVISOR.staffPersonID is the field I would filter on, if the #parameter had a value other than 0. From what I've researched online, using the field itself as the THEN in the statement should work. It does, unless the table (advisor) has no records that match in it at all.
WHERE
(#advisor <> 0 AND ADVISOR.staffPersonID = #advisor)
OR
(#advisor = 0 )
try this:
WHERE ADVISOR.staffPersonID = #advisor OR #advisor = 0
WHERE ((ADVISOR.staffPersonID IS NULL AND #advisor = 0)
OR (ADVISOR.staffPersonID = CASE WHEN #advisor = 0 THEN
ADVISOR.staffPersonID
ELSE
#advisor END
))
WHERE CONDITION1='ABC'
AND Status =
CASE #Option
WHEN 1 THEN 'True'
WHEN 2 THEN 'False'
WHEN 3 THEN NULL
WHEn 4 THEN **IN ('True', 'False', NULL)**
END
How do I write a query where my first options match directly using = but my last option needs an IN
The above query gives error, but I want something similar to it, which I am not able to find out.
A CASE statement can't return a set of values... but this query should give you the same results:
WHERE CONDITION1='ABC'
AND Status =
CASE
WHEN 1 THEN 'True'
WHEN 2 THEN 'False'
WHEN 3 THEN NULL
WHEN 4 THEN Status
END
Also, note that unless you have ANSI_NULLS OFF, Status will never = NULL... you would need to use IS NULL for this comparison, and you'd need to forgo the CASE statement altogether.
Skip the CASE statement and use OR. And as per ANSI standard don't compare with NULL:
WHERE CONDITION1='ABC'
AND ((#Option = 1 AND Status = 'True') OR
(#Option = 2 AND Status = 'False') OR
(#Option = 3 AND Status IS NULL) OR
(#Option = 4 AND (Status IS NULL OR Status IN ('True', 'False'))))