I use a case when it works perfectly fine on MySQL but not MS SQL please help.
It seems the equal is not accepted -- if not what then will work
SELECT A FROM TABLE A
WHERE
CASE WHEN COUNT = 2 THEN GOAL = 2 ELSE GOAL = 3 END
Msg 156, Level 15, State 1, Line 3 Incorrect syntax near the keyword
'CASE'.
You need to change your query to WHERE columnName = value.
The following query is what you are expecting:
SELECT A.*
FROM TABLE A
WHERE GOAL = CASE WHEN COUNT = 2 THEN 2 ELSE 3 END
Related
I need a SQL query having WHERE CASE WHEN statement with a in list. I am not sure what part of my query is wrong,
First I need to make a KEY out of coldelist and IDD,IDP and then since my codelists' lenght are 5 or 6 so need to use CASE WHEN to check if their length are 5 or 6 then search in separate lists (each almost 200 or more) because my aim is to compare some tables' data using EXCEPT
My SQL statement is like:
SELECT
codelist = CASE
WHEN LEN(codelist) = 5
THEN CONCAT(CONCAT(SUBSTRING(codelist, 1, 5), IDD), IDP)
WHEN LEN(codelist) = 6
THEN CONCAT(CONCAT(SUBSTRING(codelist, 1, 6), IDD), IDP)
ELSE codelist
END,
codelist
FROM
[table1]
WHERE
(codelist = CASE
WHEN LEN(codelist) = 5
THEN (SELECT 'FEV00', 'FEV64', 'FEV97', 'FEV90')
WHEN LEN(codelist) = 6
THEN (SELECT 'FEV208', 'FEV227', 'FEV308', 'FEV326')
END)
EXCEPT
SELECT
....
I inherited the following query from a previous application. I'm having a hard time understanding the "Case" in the "Select" and "Where" clause also.
SELECT J1.AC_CODE, J1.PERIOD, J1.JRNAL_NO, J1.DESCRIPTN, - J1.AMOUNT ,
J1.ANAL_T3,
CASE 1
WHEN 1 THEN 'A'
ELSE J1.ACCNT_CODE
END ,
J1.JRNAL_LINE
FROM dbo.JSource J1
WHERE 1=1
AND 1=1
AND NOT ('A' LIKE '%Z%'
AND J1.JRNAL_SRCE IN ('B/F',
'CLRDN')
AND J1.JRNAL_NO = 0)
AND CASE 1
WHEN 1 THEN 'A'
ELSE J1.AC_CODE
END ='A'
AND J1.AC_CODE='156320'
AND J1.PERIOD BETWEEN 2014001 AND 2014012
AND J1.ANAL_T3='ANAL001'
ORDER BY 1,2,3,4,5,6,7,8
I'm not sure If I understand the following clauses correctly:
1st Clause:
CASE 1
WHEN 1 THEN 'A'
ELSE J1.AC_CODE
END
I understood as: If column 1 is true, then choose literal A ortherwise choose J1.AC_CODE.
2nd clause:
WHERE 1=1
AND 1=1
AND NOT ('A' LIKE '%Z%'
AND J1.JRNAL_SRCE IN ('B/F',
'CLRDN')
AND J1.JRNAL_NO = 0)
AND CASE 1
WHEN 1 THEN 'A'
ELSE J1.AC_CODE
END ='A'
AND J1.AC_CODE='156320'
AND J1.PERIOD BETWEEN 2014001 AND 2014012
AND J1.ANAL_T3='ANAL001'
I'm totally lost with this "Where" clause.
Can you help explain this query and write a better version for this whole query?
I'm running this query on SQL Server 2008 (R2)
I understood as: If column 1 is true, then choose literal A ortherwise
choose J1.AC_CODE.
No, it is comparing the value 1 with the value 1 and if that is true the case returns an A and that is of course always true so the case statement will always return A.
Your where clause does not do anything at all.
1=1
AND 1=1
will always be true and the case will always be true and 'A' LIKE '%Z%' will always be false and that makes the entire AND NOT 'A' LIKE '%Z%' .... expression to always be true.
A simpler version of your query would look like this.
SELECT J1.AC_CODE,
J1.PERIOD,
J1.JRNAL_NO,
J1.DESCRIPTN,
- J1.AMOUNT,
J1.ANAL_T3,
'A',
J1.JRNAL_LINE
FROM dbo.JSource J1
WHERE J1.AC_CODE='156320' AND
J1.PERIOD BETWEEN 2014001 AND 2014012 AND
J1.ANAL_T3='ANAL001'
ORDER BY 1,2,3,4,5,6,7,8
Without knowing the history of this query, I am guessing that this was written with testing/debugging in mind and some of that code has been left in place. The case statement in the select line could (and I repeat could as this is my guess from looking at the query) have had other with clauses during creation of the query used for testing and these would have been switched between by changing the value after the CASE (example SELECT ..... CASE 1 WHEN 1 THEN 'A' WHEN 2 THEN 'some value' WHEN 3 'some other value' ELSE J1.ACCNT_CODE).
As for the where 1 = 1, I have seen this used during query creation/testing - mainly because it means each of the true conditions can easily be commented/uncommented or cut & pasted as the first where condition is always true. I've not seen AND 1 = 1 before. Not sure what that line was intended for, but I'd still think came about from testing/debugging and was not taken out the query.
I'm attempting to convert a MySQL query to a T-SQL query and the IF statement that's enclosed within a SUM statement is tripping me up. Any suggestions?
SELECT
CMTS_RQ.[Dated],
CMTS_RQ.CMTS_Name,
Count(CMTS_RQ.CMTS_Name) AS emat_count,
Sum(if(CMTS_RQ.US_Pwr>=37 and CMTS_RQ.US_Pwr<=49)) AS us_pwr_good
FROM
CMTS_RQ
GROUP BY
CMTS_RQ.CMTS_Name,
CMTS_RQ.[Dated]
But I get an error:
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'if'.
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ')'.
T-SQL doesn't have a "inline" IF statement - use a CASE instead:
SELECT
CMTS_RQ.[Dated],
CMTS_RQ.CMTS_Name,
Count(CMTS_RQ.CMTS_Name) AS emat_count,
Sum(CASE
WHEN CMTS_RQ.US_Pwr >=37 AND CMTS_RQ.US_Pwr <= 49
THEN 1
ELSE 0
END) AS us_pwr_good
FROM
CMTS_RQ
GROUP BY
CMTS_RQ.CMTS_Name,
CMTS_RQ.[Dated]
So if the value of CMTS_RQ.US_Pwr is >= 37 AND <= 49 then add 1 to the SUM - otherwise 0. Does that give you what you're looking for?
In SQL Server 2012 and newer, you can use the new IIF function:
SUM(IIF(CMTS_RQ.US_Pwr >= 37 AND CMTS_RQ.US_Pwr <= 49, 1, 0)) AS us_pwr_good
Trying to run a cross-server update:
UPDATE ASILIVE.CustomerManagementSystem.dbo.Sessions
SET ASILIVE.CustomerManagementSystem.dbo.Sessions.VarianceAmount=Variances.VarianceAmount
FROM ASILIVE.CustomerManagementSystem.dbo.Sessions
INNER JOIN Variances
ON ASILIVE.CustomerManagementSystem.dbo.Sessions.SessionGUID = Variances.SessionGUID
WHERE ASILIVE.CustomerManagementSystem.dbo.Sessions.VarianceAmount <> Variances.VarianceAmount
Gives the error:
Msg 117, Level 15, State 2, Line 5
The number name 'ASILIVE.CustomerManagementSystem.dbo.Sessions' contains
more than the maximum number of prefixes. The maximum is 3.
What gives?
See also
SQL Server Error: "maximum number of prefixes. The maximum is 3" with subselect syntax
(Deals with sub-select syntax; this question deals with join syntax)
Unimportant research:
i tried randomly aliasing things to s:
UPDATE ASILIVE.CustomerManagementSystem.dbo.Sessions s
SET s.VarianceAmount=Variances.VarianceAmount
FROM ASILIVE.CustomerManagementSystem.dbo.Sessions s
INNER JOIN Variances
ON s.SessionGUID = Variances.SessionGUID
WHERE s.VarianceAmount <> Variances.VarianceAmount
But that doesn't work:
Msg 117, Level 15, State 2, Line 5
The number name 'ASILIVE.CustomerManagementSystem.dbo.Sessions' contains
more than the maximum number of prefixes. The maximum is 3.
Hamlin suggested added brackets:
UPDATE [ASILIVE].[CustomerManagementSystem].dbo.Sessions
SET [ASILIVE].[CustomerManagementSystem].dbo.Sessions.DisciplineVarianceAmount=DisciplineVariances.VarianceAmount
FROM [ASILIVE].[CustomerManagementSystem].dbo.Sessions
INNER JOIN DisciplineVariances
ON [ASILIVE].[CustomerManagementSystem].dbo.Sessions.SessionGUID = DisciplineVariances.SessionGUID
WHERE [ASILIVE].[CustomerManagementSystem].dbo.Sessions.DisciplineVarianceAmount <> DisciplineVariances.VarianceAmount
but that doesn't work:
Msg 117, Level 15, State 2, Line 5
The number name 'ASILIVE.CustomerManagementSystem.dbo.Sessions' contains
more than the maximum number of prefixes. The maximum is 3.
Often times, you need to add brackets, at a minimum, surrounding your Linked Server Name.
[ASILIVE].[CustomerManagementSystem].dbo.Sessions
EDIT - Try this in addition
UPDATE S
SET DisciplineVarianceAmount = Variances.VarianceAmount
FROM [ASILIVE].[CustomerManagementSystem].dbo.Sessions as S
INNER JOIN Variances ON S.SessionGUID = Variances.SessionGUID
WHERE S.VarianceAmount <> Variances.VarianceAmount
Do you really like a lot of typing? :-)
UPDATE s
SET s.DisciplineVarianceAmount = v.VarianceAmount
FROM [ASILIVE].[CustomerManagementSystem].dbo.Sessions AS s
INNER JOIN dbo.Variances AS v
ON s.SessionGUID = v.SessionGUID
AND s.VarianceAmount <> v.VarianceAmount;
Take note that you may want to describe what to do here if either variance amount is currently NULL.
I have this query that spawns the following error:
SELECT * FROM Quota
WHERE LEFT(QtLabel, LEN(QtLabel)-2) IN (
'1032',
'3300',
'9682'
)
Msg 536, Level 16, State 5, Line 1
Invalid length parameter passed to the SUBSTRING function.
Am I doing something wrong? It tends to show up when I use the LEN() function. Might it be a datatype issue?
Is it possible that LEN(QtLabel) <= 2 ?
Are you sure that each QtLabel field is longer than 2 characters?
LEFT probably uses SUBSTRING internally. What happens if the length of QtLabel is <= 2?
It's because some QtLabel values don't contain > 2 characters, so you end up trying to do a LEFT() with a negative value as the number to restrict to.
In your scenario, you're assuming all QtLabel values are 6 characters, so uou should do:
SELECT * FROM Quota
WHERE LEN(QtLabel) = 6
AND LEFT(QtLabel, LEN(QtLabel)-2) IN (
'1032',
'3300',
'9682'
)
SELECT LEFT('MyText', -2)
will throw the same error