SQL Error - The identifier that starts with..........is too long. Maximum lenght is 128 - sql

Below is my SQL query which is returning back error message
The identifier that starts with 'CASE WHEN PX_PAXWEB.COUNTRY = 'AUT' THEN >vw_POSTCODE.POSTCODE
WHEN PX_PAXWEB.COUNTRTY <> 'AUT' THEN vwPxPaxWe'
is too long. Maximum length is 128.
Msg 103, Level 15, State 4, Procedure vwRBARpt_AJ, Line 168
The identifier that starts with 'CASE WHEN AG_AGENTS.SALES_AREA_DESC = 'Int. – Inbound >Then vwPxPaxweb.SALES_AREA' is too long. Maximum length is 128."
Here's the SQL query:
CASE
WHEN AG_AGENTS.SALES_AREA_DESC = 'Dom. - NAT'
THEN [CASE
WHEN PX_PAXWEB.COUNTRY = 'AUT' THEN vw_POSTCODE.POSTCODE
WHEN PX_PAXWEB.COUNTRTY <> 'AUT' THEN vwPxPaxWeb.SALES_AREA
ELSE COALESCE (vw_POSTCODE.POSTCODE, vwPxPaxWeb.SALES_AREA, AG_AGENTS.SALES_AREA_DESC) ]
ELSE
[CASE WHEN AG_AGENTS.SALES_AREA_DESC = Int. – Inbound
THEN vwPxPaxweb.SALES_AREA
ELSE COALESCE(vwPxPaxweb.SALES_AREA, AG_AGENTS.SALES_AREA_DESC)]
END AS SALES_AREA_DESC_2

It took me formatting your code to realize it, but you need parentheses and not square brackets surrounding your cases. As you have it, it's trying to find a column/table/whatever that's named "[...your code...]", which is certainly too long.

Try doing it like below. Use () to separate the CASE. [] is used for identifires; like if you are using reserve word as column names or if your column names have spaces in it then to delimit them.
CASE WHEN AG_AGENTS.SALES_AREA_DESC = 'Dom. - NAT' THEN
(CASE WHEN PX_PAXWEB.COUNTRY = 'AUT' THEN vw_POSTCODE.POSTCODE
WHEN PX_PAXWEB.COUNTRTY <> 'AUT' THEN vwPxPaxWeb.SALES_AREA
ELSE COALESCE (vw_POSTCODE.POSTCODE,vwPxPaxWeb.SALES_AREA,AG_AGENTS.SALES_AREA_DESC)
)
ELSE
(CASE WHEN AG_AGENTS.SALES_AREA_DESC = 'Int. – Inbound' Then vwPxPaxweb.SALES_AREA
ELSE COALESCE( vwPxPaxweb.SALES_AREA,AG_AGENTS.SALES_AREA_DESC)
)
END AS SALES_AREA_DESC_2

Related

Case expression with Boolean from PostgreSQL to SQL Server

I am translating a query from PostgreSQL to SQL Server. I didn't write the query in PostgreSQL and it's quite complicated for my knowledge so i don't understand every piece of it.
From my understand: we are trying to find the max version from p_policy and when insurancestatus = 7 or 14 / transactiontype = CAN, we compare two dates (whose format are BIG INT).
This is the PG Query:
SELECT *
FROM BLABLABLA
WHERE
pol.vnumber = (
SELECT MAX(pol1.vnumber)
FROM p_policy pol1
AND ( CASE WHEN pol1.insurancestatus IN (7,14)
or pol1.transactiontype IN ('CAN')
-- ('CAN','RCA')
THEN pol1.veffectivedate = pol1.vexpirydate
ELSE pol1.veffectivedate <> pol1.vexpirydate
END
)
AND pol1.vrecordstatus NOT IN (30,254)
etc.
I am used to have a where statement where I compare it to a value. I understand here from the Case statement we will have a boolean, but still that must be compared to something?
Anyway the main purpose is to make it work in SQL, but I believe SQL can't read a CASE statement where THEN is a comparison.
This is what I tried:
SELECT *
FROM BLABLABLA
WHERE pol.vnumber =
(
SELECT MAX(pol1.vnumber)
FROM p_policy pol1
WHERE sbuid = 4019
AND ( CASE WHEN pol1.insurancestatus IN (7,14)
or pol1.transactiontype IN ('CAN')
THEN CASE
WHEN pol1.veffectivedate = pol1.vexpirydate THEN 1
WHEN pol1.veffectivedate <> pol1.vexpirydate THEN 0
END
END
)
AND pol1.vrecordstatus NOT IN (30,254)
etc.
And then I get this error from SQL Server (which directly the last line of the current code - so after the double case statement)
Msg 4145, Level 15, State 1, Line 55
An expression of non-boolean type specified in a context where a condition is expected, near 'AND'.
Thank you !Let me know if it is not clear
I think you want boolean logic. The CASE expression would translate as:
(
(
(pol1.insurancestatus IN (7,14) OR pol1.transactiontype = 'CAN')
AND pol1.veffectivedate = pol1.vexpirydate
) OR (
NOT (pol1.insurancestatus IN (7,14) OR pol1.transactiontype = 'CAN')
AND pol1.veffectivedate <> pol1.vexpirydate
)
)
There are 2 main issues with your snippet, SQL Server-syntax-wise.
SELECT * FROM BLABLABLA WHERE
pol.vnumber = /* PROBLEM 1: we haven't defined pol yet; SQL Server has no idea what pol.vnumber is here, so you're going to get an error when you resolve your boolean issue */
(
SELECT MAX(pol1.vnumber)
FROM p_policy pol1
WHERE sbuid = 4019
AND ( CASE WHEN pol1.insurancestatus IN (7,14)
or pol1.transactiontype IN ('CAN')
THEN CASE
WHEN pol1.veffectivedate = pol1.vexpirydate THEN 1
WHEN pol1.veffectivedate <> pol1.vexpirydate THEN 0
END
END
) /* PROBLEM 2: Your case statement returns a 1 or a 0..
which means your WHERE is saying
WHERE sbuid = 4019
AND (1)
AND pol1.vrecordstatus NOT IN (30,254)
SQL Doesn't like that. I think you meant to add a boolean operation using your 1 or 0 after the parenthesis.
like this: */
= 1
AND pol1.vrecordstatus NOT IN (30,254)

SQL Query Error with CASE Statement?

I'm attempting to run this query using Simba's ODBC SFDC driver but the log shows me an error near the case statement. I'm not totally convinced its an error with the CASE statement but I don't see where my error is. Someone please help!!!!
SELECT
Account_Group__c,
Hospital_Sales_Teammate__c,
Name,
StageName,
CloseDate,
Yr_Credited__c,
Probability,
Census__c,
Credit__c,
Related_VSA__c,
AB_Hospital_Relationship_Type__c,
CASE
WHEN Age_In_Stage__c >0 and Age_In_Stage__c <= 30 THEN '<30'
WHEN Age_In_Stage__c >30 and Age_In_Stage__c <= 60 THEN '31-60'
WHEN Age_In_Stage__c >60 and Age_In_Stage__c <= 90 THEN '61-90'
ELSE '>90' END AS Age_Bucket,
CASE
WHEN (Type = "Existing Business - Renewal" OR Type = 'Existing Business - Amendment')
AND (Account_HHV_Segment__c='A' OR Account_HHV_Segment__c='B')
AND AB_Hospital_Relationship_Type__c<>'N/A'
AND (RecordType='012300000000PWuAAM'
OR RecordType='01250000000DcJkAAK'
OR RecordType='01250000000DpV4AAK'
OR RecordType='01250000000Dxd7AAC'
OR RecordType='01250000000DoFPAA0'
OR RecordType='01250000000DuuEAAS') THEN 'Hosp'
WHEN Name LIKE '%AB Hospital Loss%' THEN 'Hosp'
ELSE '' END AS Hospital_Eligible,
CASE
WHEN RecordType='01250000000DpV4AAK'
AND Type LIKE '%Acquisition%'
THEN 'Acq'
ELSE '' END AS Acquisition_Eligible,
CASE
WHEN RecordType='01250000000Dxd7AAC'
AND (Business_Unit__c="Full Conversion" OR Business_Unit__c="Partial Conversion")
THEN 'BGC'
ELSE '' END AS Conversion_Eligible,
CASE
WHEN RecordType='01250000000DuuEAAS'
AND Type_of_Agreement__c ="MDA" OR Type_of_Agreement__c ="Joinder" OR Type_of_Agreement__c ="JV"
THEN 'Incr Doc'
ELSE '' END AS Incr_Doc_Eligible
FROM
Opportunity
WHERE
Eligible__c<>'No'
AND NOT Name LIKE '%test%'
AND NOT Name LIKE '%Test%'
AND NOT Name LIKE '%TEST%'
ORDER BY
Account_Group__c ASC
Business_Unit__c="Full Conversion" (and other places as well): You are using double quotes instead of single quotes (as you do in the rest of the query). I bet that's the problem...
Also, this is a case expression, not a statement.
Why are you using double quotes?
(Type = "Existing Business - Renewal" OR Type = 'Existing Business - Amendment')
You should change it to
(Type = 'Existing Business - Renewal' OR Type = 'Existing Business - Amendment')

Case in Select Query has syntax error

I want to return a boolean according to a condition on one of the column of my table. I tested it in SQL Server 2014 and it works, but I have no experience in Access.
This is the query I have at the moment, using Access 2007.
SELECT (CASE WHEN Type = 'C' THEN 1 ELSE 0 END) AS EstContrat
FROM Historique_EnTete
Type has the Text type.
I have translated the error message to :
"Syntax error (missing operator) in the expression << (CASE WHEN Type = 'C' THEN 1 ELSE 0 END) >>"
What am I missing?
In access you have IIF
SELECT IIF(Type = 'C', 1, 0 ) AS EstContrat
FROM Historique_EnTete
As Lamak say you also have SWITCH

how to use > = condition in sql case statement?

I am trying to place a >= condition in statement, but it is not allowing this condition.
case
when ct.CRS_CAREER_LVL_CD = 'G' then 'Graduate '
when ct.CRS_CAREER_LVL_CD = 'L' then 'Law '
when convert(int, left(ct.CRS_CATLG_NO, 4) > = 99 then 'Upper Division'
else 'Lower Division'
end as courseLevelName
Is there any other way to do this ?
when convert(int,left(ct.CRS_CATLG_NO,4)
^ ^ ^ ^
OPEN OPEN CLOSE CLOSE ?
Guessing the problem is that the left 4 characters of are not always integers, if you're using SQL Server 2012 or newer you can use TRY_CONVERT():
case when ct.CRS_CAREER_LVL_CD = 'G' then 'Graduate '
when ct.CRS_CAREER_LVL_CD = 'L' then 'Law '
when TRY_CONVERT(int,left(ct.CRS_CATLG_NO,4)) > = 99 then 'Upper Division'
else 'Lower Division'
end as courseLevelName
Edit: Looks like you were missing a closing ), if that wasn't a typo then that's likely the issue, if still getting an error then might be non-INT.

Nhibernate Criteria Conditional Where

Im working on a NHibernate criteria wich i graduatly builds upp depending on input parameters.
I got some problem with the postal section of these paramters.
Since we got a 5 number digit zipcodes the input parameter is a int, but since we in database also accept foreign zipcodes the database saves it as string.
What im trying to replicate in NHibernate Criteria/Criterion is the following where clause.
WHERE
11182 <=
(case when this_.SendInformation = 0 AND dbo.IsInteger(this_.Zipcode) = 1 then
CAST(REPLACE(this_.Zipcode, ' ', '') AS int)
when this_.SendInformation = 1 AND dbo.IsInteger(this_.WorkZipcode) = 1 then
CAST(REPLACE(this_.WorkZipcode, ' ', '') AS int)
when this_.SendInformation = 2 AND dbo.IsInteger(this_.InvoiceZipcode) = 1 then
CAST(REPLACE(this_.InvoiceZipcode, ' ', '') AS int)
else
NULL
end)
What we do is to check where the member contact (this_) has preferenced to get information sent to, then we check the input zipcode as integer against three different columns depending on if the column is convertable to int (IsInteger(expr) function) if column is not convertable we mark the side as NULL
in this case we just check if the zipcode is >= input parameter (reversed in sql code since paramter is first), the goal is to do a between (2 clauses wrapped with 'AND' statement), >= or <=.
UPDATE
Got a hint of success.
Projections.SqlProjection("(CASE when SendInformation = 0 AND dbo.IsInteger(Zipcode) = 1 then CAST(REPLACE(Zipcode, ' ', '') AS int) when SendInformation = 1 AND dbo.IsInteger(WorkZipcode) = 1 then CAST(REPLACE(WorkZipcode, ' ', '') AS int) when SendInformation = 2 AND dbo.IsInteger(InvoiceZipcode) = 1 then CAST(REPLACE(InvoiceZipcode, ' ', '') AS int) else NULL END)"
, new[] { "SendInformation", "Zipcode", "WorkZipcode", "InvoiceZipcode" },
new[] { NHibernateUtil.Int32, NHibernateUtil.String, NHibernateUtil.String, NHibernateUtil.String });
Throw my whole clause in a Projections.SqlProjection, however when i run my code some of my projection is cut (" AS int) else NULL END)" is cut from the end) and makes the sql corrupt.
Is there some kind of limit on this ?
Got it working yesterday.
Projections.SqlProjection worked, however if you don't name the projection as a column it some how cuts some of the TSQL code.
(Case
when x = 1 then 'bla'
when x = 2 then 'bla_bla'
else NULL
END) as foo
when using the last part (as foo) and naming the entire case syntax it works and dont cut anything.
However i dont know why but i could not manage to use the aliases from the other part of the criteria.