Case statement don't work correctly - sql-server-2012

SQL SERVER 2012
In my case server conflict with '='.
why it is not working?
here is my code:
SELECT
TM.CODE_3 as Account,
TM.NAME_3,
TM.CODE_5,
case TM.Line
when TM.CODE_5 = '9491'--and RS.SUB1Sel = ('05.11 Penalties and fines')
then 'R0820-5'
else TABLE_MAIN_new.Line
end
FROM TABLE_Main_new as TM
left join Danone_Main as DM
on TM.CODE_3 = DM.CODE_3
left join Rep_Struct_2012_N as RS
on TM.CODE_3 = RS.CODE_3
Incorrect syntax near '='.

Try this
case
when TM.CODE_5 = '9491'--and RS.SUB1Sel = ('05.11 Penalties and fines')
then 'R0820-5'
else TABLE_MAIN_new.Line
End

This should work:
CASE TM.CODE_5
WHEN '9491'--and RS.SUB1Sel = ('05.11 Penalties and fines')
THEN 'R0820-5'
ELSE TABLE_MAIN_new.Line
END
When you use CASE expression, the WHEN clause should only be a value (true/false depending on your CASE expression). If no CASE expression is used then the WHEN should be a boolean expression. More info HERE.

Related

Postgres SQL state: 22P02 - invalid input syntax for integer

I'm using a sql query to export a database from my company's program.
Everything seems to be fine till I change the date on the "where" statement with a previous one.
Please find below the code:
SELECT p."Index", p."PSN" || CAST(p."PNR"as int) AS ID,
p."PSN" AS Serie, cast(p."PNR"as int) AS Numar,
pr."PINDate" AS r_gdate,
CASE WHEN pr."AsigEID"='10' THEN pr."PrimSUM" ELSE
pr."PrimSUM"*valuta1."EXCValue" END AS r_prima_lei,
CASE WHEN pr."AsigEID"='2'
THEN pr."PrimSUM"
ELSE CASE WHEN pr."AsigEID"='10' THEN pr."PrimSUM"/valuta2."EXCValue"
ELSE pr."PrimSUM"*valuta1."EXCValue"/valuta2."EXCValue"
END
END AS r_prima_eur,
CASE WHEN pr."AsigEID"='10' THEN pr."AsigSUM" ELSE
pr."AsigSUM"*valuta1."EXCValue" END as r_sa_lei,
CASE WHEN pr."AsigEID"='2'
THEN pr."AsigSUM"
ELSE CASE WHEN pr."AsigEID"='10' THEN pr."AsigSUM"/valuta2."EXCValue"
ELSE pr."AsigSUM"*valuta1."EXCValue"/valuta2."EXCValue"
END
END AS r_sa_eur,
pr."AsigStart", pr."AsigEnd", risc."Code", plink."Index"
FROM "PolsRisc" AS pr
LEFT JOIN "Pols" as p ON p."Index" = pr."PID"
LEFT JOIN "Riscs" as risc ON pr."RID" = risc."Index"
LEFT JOIN "PRLNK" plink ON plink."PTID" = p."PTID" AND plink."RID" = risc."Index"
LEFT JOIN "EXCValues" valuta1 ON valuta1."AtDate" = pr."AsigStart" AND valuta1."EID" = pr."AsigEID"
LEFT JOIN "EXCValues" valuta2 ON valuta2."AtDate" = pr."AsigStart" AND valuta2."EID"='2'
WHERE pr."PINDate" > '2020-08-01' AND pr."IsRezil" = 'false';
When I'm using '2020-08-01' the query works well. When I try to change it to a previous one eg. '2010-01-01' a get an error:
ERROR: invalid input syntax for integer: ""
SQL state: 22P02
I was looking for a solution on the previous posts but I didn't manage to solve this issue.
It looks like it is returning "" or a null value into one of the columns you are using integer logic for. The date change is just filtering out the data that would crash it.
You may need to use coalesce to reassign the nulls as 0 and then cast it back into being an int
select
cast(coalesce(table.column, 0) as int) as result
from table
I would advice to read the chapter http://www.postgresql.org/docs/current/interactive/sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS
. It's a brief and informative read.The cause for the error message is that '' is an empty string that has no representation in a numeric type like integer

CASE WHEN SQL Syntax Error

I am attempting to us the CASE statement for the first time and I cannot understand why I am getting a syntax error on the last WHEN and ELSE. I am trying to extract a substring if a value starts with a specific set of characters. My Code is below:
SELECT
CASE
WHEN LEFT ([RCode],2) = 'BB' THEN SUBSTRING([RCode],3,LEN([RCode]))
WHEN LEFT ([RCode],4) = 'APT-' THEN SUBSTRING([RCode],5,LEN([RCode])
WHEN LEFT ([RCode],4) = 'PS-' THEN SUBSTRING([RCode],4,LEN([RCode])
ELSE [RCode]
END
FROM [Xperdyte].[dbo].[tJCLines]
Any guidance would be appreciated.
This won't (necessarily) fix your syntax problem, but I would recommend that you use like for the comparisons:
SELECT (CASE WHEN RCode LIKE 'BB%' THEN SUBSTRING([RCode], 3, LEN([RCode]))
WHEN RCode LIKE 'APT-%' THEN SUBSTRING([RCode], 5, LEN([RCode]))
WHEN RCode LIKE 'PS-%' THEN SUBSTRING([RCode], 4, LEN([RCode]))
ELSE [RCode]
END)
FROM [Xperdyte].[dbo].[tJCLines];
Then you don't have to count characters -- and the third condition will match.
Missed off two right parenthesis.
SELECT CASE WHEN LEFT([RCode],2) = 'BB'
THEN SUBSTRING([RCode],3,LEN([RCode]))
WHEN LEFT([RCode],4) = 'APT-'
THEN SUBSTRING([RCode],5,LEN([RCode]))
WHEN LEFT([RCode],4) = 'PS-'
THEN SUBSTRING([RCode],4,LEN([RCode]))
ELSE [RCode]
END
FROM [Xperdyte].[dbo].[tJCLines]

Can't pinpoint error near the LIKE keyword

I'm running into an error with a sql inquery near my LIKE statement. Am I formatting the LIKE clause incorrectly within the WHEN clause of a CASE? Here is the code.
SELECT
substring (Cases.FileNumber,4,6) AS [FileNumber]
,Charge.ChargeCode
,Cases.BookedLastName
,Cases.BookedFirstName
,Cases.ArrestDate
,Cases.BookedDOB
,Cases.BookedAge
,Charge.OffenseToDate
,Cases.BookedRace
,Cases.BookedSex
,Charge.OffenseStreetAddress1
,Charge.OffenseCity
,Charge.OffenseState
,Charge.OffenseZipCode
,Charge.ChargeDescription
,(SELECT CASE
WHEN charge1.ChargeDescription LIKE N'%heroin%' THEN 'Heroin'
ELSE 'Not Heroin'
END
FROM tblCsCharge AS charge1
INNER JOIN tblCsCases AS cases1
ON charge1.FileNumber = cases1.FileNumber
WHERE
charge1.BookedLastName = cases1.BookedLastName
AND charge1.BookedLastName = cases1.BookedLastName
AND charge1.BookedDOB = Cases.BookedDOB
AND cases2.ChargeCode IN (N'579.015-001Y201735')) AS HeroinYN
FROM
tblCsCases AS Cases
INNER JOIN tblCsCharge AS Charge
ON Cases.FileNumber = Charge.FileNumber
WHERE Cases.IssuedDate >= 01/01/2017
AND
Cases.IssuedDate <= #EndDate
AND
Charge.ChargeCode IN (N'579.015-001Y201735')
AND
Cases.BookedLastName NOT IN (N'Bogus')
ORDER By
Cases.BookedLastName
This is a sub-query within a bigger query focused on building a column that will simply output Heroin or Not Heroin based off of a large text field in our database.
EDITs:
Despite the changes that should make the code compile, the following error now occurs:
Invalid column name 'BookedLastName'.
Invalid column name 'BookedLastName'.
Invalid column name 'BookedDOB'.
The multi-part identifier "cases2.ChargeCode" could not be bound.
You have two errors in your code, both of which were pointed out, though not in the same answer.
Your case statement should be without the column name in parenthesis - I commented it out in the copy of your code below)
You're missing a closing parenthesis at the end of the query, right before the alias - I added it below.
.
,(SELECT CASE--(charge1.ChargeDescription)
WHEN charge1.ChargeDescription LIKE N'%heroin%' THEN 'Heroin'
ELSE 'Not Heroin'
END
FROM tblCsCharge AS charge1
INNER JOIN tblCsCases AS cases1
ON charge1.FileNumber = cases1.FileNumber
WHERE
charge1.BookedLastName = cases1.BookedLastName
AND charge1.BookedLastName = cases1.BookedLastName
AND charge1.BookedDOB = Cases.BookedDOB
AND cases2.ChargeCode IN (N'579.015-001Y201735')) AS HeroinYN
This seems to be the issue..
AND cases2.ChargeCode IN (N'579.015-001Y201735') AS HeroinYN
Since this is part of a sub-query, you need another parenthese
AND cases2.ChargeCode IN (N'579.015-001Y201735')) AS HeroinYN
ALSO
You can't put the column name after a CASE statement when you are using an equality operator.
This is acceptable
select
case columnName
when 'X' then 1
when 'Y' then 0
end
This is not
select
case columnName
when = 'X' then 1
when columnName like '%Y%' then 0
end
CORRECTED SCRIPT
,(SELECT CASE
WHEN charge1.ChargeDescription LIKE N'%heroin%' THEN 'Heroin'
ELSE 'Not Heroin'
END
FROM tblCsCharge AS charge1
INNER JOIN tblCsCases AS cases1
ON charge1.FileNumber = cases1.FileNumber
WHERE
charge1.BookedLastName = cases1.BookedLastName
AND charge1.BookedLastName = cases1.BookedLastName
AND charge1.BookedDOB = Cases.BookedDOB
AND cases2.ChargeCode IN (N'579.015-001Y201735')) AS HeroinYN
The proper syntax for that is:
CASE
WHEN charge1.ChargeDescription LIKE N'%heroin%' THEN 'Heroin'
ELSE 'Not Heroin'
END
Don't put the field name after CASE in this syntax.

Postgresql Syntax error at or near " THEN "

I'm trying to create a view in Postgresql , but when I run this code appears this error:
syntax error at or near " THEN "
CREATE OR REPLACE VIEW VW_MONITOR_DEVICE AS
SELECT
P.POSIZIONE_DEVICE_ID AS MONITOR_DEVICE_ID,
P.VALID AS VALID,
[...]
IF (VALID == FALSE THEN 'Valid' ELSE P.REASON_FOR_INVALID) AS DESCRIPTION,
[...]
FROM public.TA_POSIZIONI_DEVICE P
JOIN ...
TA_POSIZIONI_DEVICE
VALID (Boolean not null)
You should use CASE
The SQL CASE expression is a generic conditional expression, similar
to if/else statements in other programming languages
CASE WHEN condition THEN result
[WHEN ...]
[ELSE result]
END
So,
CREATE OR REPLACE VIEW VW_MONITOR_DEVICE AS
SELECT
P.POSIZIONE_DEVICE_ID AS MONITOR_DEVICE_ID,
P.VALID AS VALID,
[...]
CASE WHEN VALID = false THEN 'Valid'
ELSE P.REASON_FOR_INVALID
END AS DESCRIPTION,
[...]
FROM public.TA_POSIZIONI_DEVICE P
JOIN ...
you can use case
case when VALID = FALSE THEN 'Valid' ELSE P.REASON_FOR_INVALID end DESCRIPTION,
IF (VALID == FALSE) THEN 'Valid' ELSE P.REASON_FOR_INVALID END IF AS DESCRIPTION
Try this ! You've got a parenthensis issue in you condition

Use Case Statement in Join

Hi every one i want to use case statement in join using this query and got error
Select CONVERT(VARCHAR(10), SII.SIDATE,103)DATE,SII.SALEID,SII.ItemName,SI.TenancyID
FROM F_SALESINVOICEITEM SII
INNER JOIN F_SALESINVOICE SI ON SI.SALEID=SII.SALEID
INNER JOIN #TempTableSearch ts ON CASE
WHEN ts.ACCOUNTTYPE = '1' THEN ts.ACCOUNTID=SI.TENANCYID
WHEN ts.ACCOUNTTYPE='2' THEN ts.ACCOUNTID=SI.EMPLOYEEID
WHEN ts.ACCOUNTTYPE='3' THEN ts.ACCOUNTID=SI.SUPPLIERID
WHEN ts.ACCOUNTTYPE='4' THEN ts.ACCOUNTID=SI.SALESCUSTOMERID
Error
Incorrect syntax near '='.
Please help me to solve this error.
IT should be,
ON
ts.ACCOUNTID = CASE
WHEN ts.ACCOUNTTYPE = '1' THEN SI.TENANCYID
WHEN ts.ACCOUNTTYPE = '2' THEN SI.EMPLOYEEID
WHEN ts.ACCOUNTTYPE = '3' THEN SI.SUPPLIERID
WHEN ts.ACCOUNTTYPE = '4' THEN SI.SALESCUSTOMERID
END
Instead of using CASE, I'd much rather do this:
Select CONVERT(VARCHAR(10), SII.SIDATE,103)DATE,SII.SALEID,SII.ItemName,SI.TenancyID
FROM F_SALESINVOICEITEM SII
INNER JOIN F_SALESINVOICE SI ON SI.SALEID=SII.SALEID
INNER JOIN #TempTableSearch ts ON
(ts.ACCOUNTTYPE='1' AND ts.ACCOUNTID=SI.TENANCYID)
OR (ts.ACCOUNTTYPE='2' AND ts.ACCOUNTID=SI.EMPLOYEEID)
OR (ts.ACCOUNTTYPE='3' AND ts.ACCOUNTID=SI.SUPPLIERID)
OR (ts.ACCOUNTTYPE='4' AND ts.ACCOUNTID=SI.SALESCUSTOMERID)
To explain why the query didn't work for you: the syntax of the CASE requires an END at the end of the clause. It would work, as the other solutions proposed suggest, but I find this version to be more convenient to understand - although this part is highly subjective.
you can do this, so you have no chance to misspell something (note that ACCOUNTTYPE and ACCOUNTID used only when needed, you don't have to copy-paste it)
select
convert(varchar(10), SII.SIDATE,103) as DATE,
SII.SALEID, SII.ItemName, SI.TenancyID
from F_SALESINVOICEITEM as SII
inner join F_SALESINVOICE as SI on SI.SALEID = SII.SALEID
outer apply (
'1', SI.TENANCYID
'2', SI.EMPLOYEEID
'3', SI.SUPPLIERID
'4', SI.SALESCUSTOMERID
) as C(ACCOUNTTYPE, ACCOUNTID)
inner join #TempTableSearch as ts on
ts.ACCOUNTTYPE = C.ACCOUNTTYPE and ts.ACCOUNTID = C.ACCOUNTID
You have syntax error. You are missing END there.
You must understand that CASE ... END block is NOT equivalent to IF { } from C-like languages. Much rather this is equivalent to elaborate version of ... ? ... : ... operator from C-like languages. What it means that the WHOLE CASE block must essentially evaluate to single value and that this value has to be the same type no matter which case of the block is executed. This means that:
CASE
WHEN ts.ACCOUNTTYPE = '1' THEN ts.ACCOUNTID=SI.TENANCYID ...
END
Is fundamentally incorrect unless you work on a version of database that will allow you bool value as a value (SQL Server won't allow it for example but I think some of MySQL version used to allow it - not sure about this). You probably should write something like:
CASE
WHEN ts.ACCOUNTTYPE = '1' AND ts.ACCOUNTID=SI.TENANCYID THEN 1
WHEN ts.ACCOUNTTYPE='2' AND ts.ACCOUNTID=SI.EMPLOYEEID THEN 1
WHEN ts.ACCOUNTTYPE='3' AND ts.ACCOUNTID=SI.SUPPLIERID THEN 1
WHEN ts.ACCOUNTTYPE='4' AND ts.ACCOUNTID=SI.SALESCUSTOMERID THEN 1
ELSE 0
END = 1
Notice how the whole CASE block evaluates to 1 or 0 and then it is compared to 1. Of course instead of 4 WHEN's you could use one WHEN with combination of AND's, OR's and ( ) brackets. Of course in this particular case answer by #ppeterka 66 is correct as CASE is not suited for what you really wanted to do - I'm just trying to clarify what CASE really is.