SQL SELECT with LEFT and LEN - sql

I have a PRJ_NAME result of "Authoriazations - XXXXXX-XXXXXX", but all I want is "Authorizations", so I use this query with complete success.
SELECT ISNULL(LEFT(PRJ_NAME, LEN(PRJ_NAME)-16),'') AS 'Folder Name'
FROM MHGROUP.PROJECTS
WHERE PRJ_ID = '3626747'
Now, when I use the following query with a basic join, I can't use this logic within this query.
SELECT m.[DOCNAME],
m.[SUBCLASS_ALIAS],
u.FULLNAME,
m.[CDATE3] as 'File Date',
m.[DECLAREWHEN] as 'Declared',
ISNULL(LEFT(p.PRJ_NAME, LEN(p.PRJ_NAME)-16),'') as 'Folder Name',
m.ENTRYWHEN
FROM ([Imanage].[MHGROUP].[DOCMASTER] as m (nolock)
JOIN [Imanage].[MHGROUP].[DOCUSERS] as u (nolock) on u.USERID = m.OPERATOR),
[Imanage].[MHGROUP].[PROJECTS] as p (nolock),
[Imanage].[MHGROUP].[PROJECT_ITEMS] as i (nolock)
WHERE i.ITEMTYPE = 'D' and
i.ITEM_ID = m.DOCNUM and
i.PRJ_ID = p.PRJ_ID
ORDER BY u.FULLNAME, p.PRJ_NAME
I get the following error...
Msg 537, Level 16, State 2, Line 2 Invalid length parameter passed to
the LEFT or SUBSTRING function.
I can't seem to think of what could be causing this. Any assistance or guidance would be greatly appreciated.

There is value in PRJ_NAME column that is less than 16 characters.
same error seen with below simple query
select LEFT('abc', LEN('abc')-16)
Check if there is any such name and handle it with case statement
case when len(PRJ_NAME) > 16
then LEFT(p.PRJ_NAME, LEN(p.PRJ_NAME)-16)
ELSE prj_name
END

Related

Case When + IN combination

I'm a bit stuck in my coding here... I have this extense and complex code, but I'm actually failling by the end of it. I'm getting the SQL Error [42804]: ERROR: argument of CASE/WHEN must be type boolean, not type character varying
The thing, is: when "bairro" matches a row from "SUB_COUNTRY_DB", get psd.name, else get z.name. Any tips on how I could accomplish this?
select distinct
aa.mes,
--aa.modalidade,
--to_date(aa.created_date, 'yyyy-mm-dd') data_captacao,
ucl.ja_comprou_lf as comprou_lf,
case when bairro in (select sub_country from sub_country_DB)
then psd.name
else z.name
end loja,
count (distinct aa.customer_uid) qtde_socios,
count (distinct aa.assinatura_uid) qtde_assinaturas
from assinaturas_ativas aa
left join ultima_compra_loja_fisica ucl on (aa.customer_uid = ucl.customer_uid
and aa.mes = ucl.mes)
left join zip_code z on (aa.customer_uid = z.customer_uid
and aa.mes = z.mes)
left join SUB_COUNTRY_DB psd
on (psd.district = aa.bairro)
group by 1,2,3--,4
Try variants like:
moving condition to an inner query
CASE WHEN EXISTS (SELECT DISTINCT sub_country FROM sub_country_DB WHERE sub_country = barrio)
ANY (PostgreSQL only)
CASE WHEN bairro = ANY(ARRAY_AGG(select sub_country from sub_country_DB))

IF THEN in SQL Select Statement

I'm writing a report to return vendor names from journal transactions. Here are the basics of the query below.
I am using data from two views:
Journal detail view = pa_journal_detail
Vendor detail view = ap_vendor
Match on Vendor Number:
The vendor number is contained in the following fields:
ap_vendor.a_vendor_number
pa_journal_detail.jl_ref1 [under certain criteria shown below] ONLY WHEN the journal source code is "API" or "APM"
The source code is stored in the field pa_journal_detail. jl_source_code
The vendor name is stored in the field ap_vendor.a_vendor_name
This is the query I had started with. It is returning incorrect syntax errors when I attempt to run.
SELECT
CASE
WHEN pa_journal_detail. jl_source_code = ‘API’
OR pa_journal_detail. jl_source_code = ‘APM’
THEN(
SELECT a_vendor_name
FROM ap_vendor
INNER JOIN pa_journal_detail
ON pa_journal_detail.jl_ref1 = ap_vendor.a_vendor_number)
ELSE 0
END as a_vendor_name, *
FROM pa_journal_detail
Here is the full query below. I have also tried it with the "TOP 1" included as well. I am now getting the error that there is incorrect syntax near the keyword AS.
SELECT
pa_journal_detail.a_project
,pa_journal_detail.jl_seg2
,pa_journal_detail.jl_seg3
,pa_journal_detail.jl_seg4
,pa_journal_detail.jl_source_code
,pa_journal_detail.jl_ref1
,pa_journal_detail.jl_gl_org
,pa_journal_detail.jl_gl_obj
,pa_journal_detail.jl_line_num
,pa_journal_detail.jl_journal_num
,pa_journal_detail.jl_jnl_year_period
,pa_journal_detail.jl_journal_number
,pa_journal_detail.jl_journal_seq
,(SELECT(CASE
WHEN pa_journal_detail. jl_source_code = 'API'
OR pa_journal_detail. jl_source_code = 'APM'
THEN(
SELECT TOP 1 a_vendor_name
FROM ap_vendor
RIGHT JOIN pa_journal_detail
ON pa_journal_detail.jl_ref1 = ap_vendor.a_vendor_number
)
ELSE 0
END as 'a_vendor_name', *
FROM pa_journal_detail))
FROM pa_journal_detail
This is what I ended up with and its working now! Thank you all!
SELECT
pa_journal_detail.a_project
,pa_journal_detail.jl_seg2
,pa_journal_detail.jl_seg3
,pa_journal_detail.jl_seg4
,pa_journal_detail.jl_source_code
,pa_journal_detail.jl_ref1
,pa_journal_detail.jl_gl_org
,pa_journal_detail.jl_gl_obj
,pa_journal_detail.jl_line_num
,pa_journal_detail.jl_journal_num
,pa_journal_detail.jl_jnl_year_period
,pa_journal_detail.jl_journal_number
,pa_journal_detail.jl_journal_seq
,iif((pa_journal_detail.jl_source_code = 'API'
OR pa_journal_detail.jl_source_code = 'APM')
,(SELECT TOP 1 a_vendor_name
FROM ap_vendor
RIGHT JOIN pa_journal_detail
ON pa_journal_detail.jl_ref1 = ap_vendor.a_vendor_number)
,0) as 'a_vendor_name'
FROM pa_journal_detail
for string compare you need to use single quote
CASE
WHEN pa_journal_detail. jl_source_code = 'API'
OR pa_journal_detail. jl_source_code = 'APM'
THEN(
SELECT top 1 a_vendor_name -- here you need limit or top 1
FROM ap_vendor
INNER JOIN pa_journal_detail
ON pa_journal_detail.jl_ref1 = ap_vendor.a_vendor_number
)
ELSE 0
END as a_vendor_name, *
FROM pa_journal_detail
There is also iif(). I use it far more often than I should, I just like have a tiny little if for simple conditional work instead of big ole' Case statement.
select iif(1 = 1,'True','False')
-- any series that results in a boolean
select iif((1 = 1 and 0 = 0) and (5 / 1 = 5 and 5 % 10 = 5),'True','False')
for your query
SELECT
pa_journal_detail.a_project
,pa_journal_detail.jl_seg2
,pa_journal_detail.jl_seg3
,pa_journal_detail.jl_seg4
,pa_journal_detail.jl_source_code
,pa_journal_detail.jl_ref1
,pa_journal_detail.jl_gl_org
,pa_journal_detail.jl_gl_obj
,pa_journal_detail.jl_line_num
,pa_journal_detail.jl_journal_num
,pa_journal_detail.jl_jnl_year_period
,pa_journal_detail.jl_journal_number
,pa_journal_detail.jl_journal_seq
,iif(pa_journal_detail.jl_source_code = 'API' OR pa_journal_detail.jl_source_code = 'APM',(SELECT TOP 1 a_vendor_name
FROM ap_vendor
RIGHT JOIN pa_journal_detail
ON pa_journal_detail.jl_ref1 = ap_vendor.a_vendor_number)
,0)
'a_vendor_name', *
FROM pa_journal_detail))
FROM pa_journal_detail
I think a case expression may be the wrong tool for the job. If you want to join a table sometimes, a left join may be easier:
SELECT p.*, a.a_vendor_name
FROM pa_journal_detail p
LEFT JOIN ap_vendor a ON p.jl_ref1 = a.a_vendor_number AND
p.jl_source_code IN ('API', 'APM')

Feature Summary using Rollup-

I am trying to modify a rollup query I found to use a select statement instead of a view. the rollup is:
select case when (grouping(lut_XXX_type_name) = 1) then 'ALL'
else ISNULL(lut_XXX_type_name, 'unknown')
end as lut_XXX_type_name,
case when (GROUPING(lut_YYY_status_name) = 1)then 'ALL'
ELSE ISNULL(lut_YYY_status_name, 'UNKNOWN')
end as lut_YYY_status_name,
SUM (ZZZ_quantity) as qtySum
from database.dbo.View_permitted
group by lut_XXX_type_name,lut_YYY_status_name with rollup
I want to replace the 'from database.dbo.View_permitted' with the following so I put it in Parenthesis:
SELECT dbo.WWW.WWW_id, dbo.WWW.WWW_grantee, dbo.YYY_entity.YYY_entity_apn, dbo.lut_YYY_status.lut_YYY_status_name,
dbo.lut_entity_type.lut_entity_type_name, dbo.lut_XXX_type.lut_XXX_type_name, dbo.county.county_name, dbo.ZZZ.ZZZ_quantity,
dbo.YYY.YYY_city, dbo.YYY.YYY_term, dbo.YYY.YYY_date_executed
FROM dbo.WWW INNER JOIN
dbo.YYY ON dbo.WWW.WWW_id = dbo.YYY.YYY_WWW_id INNER JOIN
dbo.lut_YYY_status ON dbo.YYY.YYY_lut_YYY_status_id = dbo.lut_YYY_status.lut_YYY_status_id INNER JOIN
dbo.ZZZ ON dbo.WWW.WWW_id = dbo.ZZZ.ZZZ_WWW_id INNER JOIN
dbo.lut_XXX_type ON dbo.ZZZ.ZZZ_lut_XXX_type_id = dbo.lut_XXX_type.lut_XXX_type_id INNER JOIN
dbo.YYY_entity ON dbo.YYY.YYY_id = dbo.YYY_entity.YYY_entity_YYY_id INNER JOIN
dbo.lut_entity_type ON dbo.YYY_entity.YYY_entity_lut_entity_type_id = dbo.lut_entity_type.lut_entity_type_id INNER JOIN
dbo.county ON dbo.WWW.WWW_county_id = dbo.county.county_id
WHERE (dbo.ZZZ.ZZZ_lut_XXX_type_id IN (20, 21, 22, 23, 24, 65, 66)) AND (dbo.county.county_name IN ('County1', 'county2')) AND
(dbo.YYY_entity.YYY_entity_WWW <> 0) AND (dbo.lut_YYY_status.lut_YYY_status_name IN ('Active', 'permitted’))
when I execute, I get the following error:
Msg 156, Level 15, State 1, Line 22
Incorrect syntax near the keyword 'group'.
Msg 319, Level 15, State 1, Line 22
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
I have read threads regarding use of semicolon, I modified the end to be:
group by rollup(lut_XXX_type_name,lut_YYY_status_name)
but it is still not working...
Thanks for your help-
Try this:
group by rollup(lut_XXXXX_type_name, lut_YYYYY_status_name)
with rollup is used in some other databases.

why sql case condition gives error

I do something like this which works fine :
select nameOfCols
from
FACost
inner join FAT as D on d.nid=a.atypeid
and
d.nid in (select item from SplitString('1,2,3,',','))
But when i use case to handle a situation where user dynamically may enter '' instead of '1,2,3,'. Then it gives error in my case condition
declare #selectedAssetTypeID varchar(50)='1,2,3,'
select nameOfCols
from
FACost
inner join FAT as D on d.nid=a.atypeid
and
case when #selectedAssetTypeID<>'' then d.nid in (select item from SplitString( #selectedAssetTypeID,',')) else d.nid=1 end //error causing lines
errors are:
Msg 156, Level 15, State 1, Line 33
Incorrect syntax near the keyword 'in'.
Msg 156, Level 15, State 1, Line 33
Incorrect syntax near the keyword 'else'.
Use and and or conditions instead of a case expression. The case expression as you have it is assigning a value (else d.nid=1) or checking for a true/false condition (d.nid in (select item from SplitString( #selectedAssetTypeID,','))).
and (
(#selectedAssetTypeID <>'' and d.nid in (select item from SplitString( #selectedAssetTypeID,',')) )
or (d.nid=1)
)
you cannot use in clause with case statement. because Case has to return one value per statement (either true or false)
either you can split your queries in two blocks or you can use "OR" clause.
IF #selectedAssetTypeID = " "
BEGIN
select nameOfCols
from FACost
inner join FAT as D
on (d.nid = a.atypeid)
where d.nid = 1
END
ELSE
BEGIN
select nameOfCols
from FACost
inner join FAT as D
on (d.nid = a.atypeid)
where d.nid IN
(select item from SplitString( #selectedAssetTypeID,','))
END
You can also use "OR" clause
select nameOfCols
from FACost
inner join FAT as D
on (d.nid = a.atypeid)
where ((#selectedAssetTypeID <>'' and d.nid in (select item from SplitString(#selectedAssetTypeID,',')))
or (d.nid=1))
link for the discussion about the similar issue is below
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/bc8a7a0b-1980-4481-a2df-6a5fde38f362/in-clause-in-case-statement?forum=sqlgetstarted

Update table with date conversion failing on Multi-Part Identifier

Thanks to the guys with suggestions on date conversion yesterday, I now have a working SELECT script.
Unfortunately, when I try to turn it into an UPDATE script, I get the dreaded
Msg 4104, Level 16, State 1, Line 25
The multi-part identifier "mbrProject.ID" could not be bound.
The entire script should insert the date from mbrProject into ProjectDates, for each matching ID (mbrProject.[ID] = ProjectDates.[Project_ID]), whilst resolving mixed US & UK format dates in mbrProject, due to users having incorrect country settings when updating the tables (not changeable from my end).
Update ProjectDates
SET ProjectDates.[Start_Date] =
(
select right([Start_Date],4) +
case
when len([Start_Date])=10 then
substring([Start_Date],4,2) + left([Start_Date],2)
else right('0' + left([Start_Date],firstIndex-1),2) +
right('0' + substring([Start_Date],firstIndex+1,secondIndex - firstIndex-1),2)
end
from
(
select mp.[Start_Date], charindex('/',mp.[Start_Date],1) firstIndex,
charindex('/',mp.[Start_Date],charindex('/',mp.[Start_Date],1)+1) secondIndex
from mbrProject mp
join ProjectDates pd
on mp.ID = pd.Project_ID
)A
where mbrProject.[ID] = ProjectDates.Project_ID
)
If I run ONLY the SELECT statement, it works fine, returning the correct values.
i.e Everything from
select right([Start_Date],4) +
case
down to
from mbrProject mp
join ProjectDates pd
on mp.ID = pd.Project_ID
)A
However, as soon as I try to wrap it up in the UPDATE statement, I get the error.
I know that this is down to syntax, but wherever I try to move stuff, I can't quite figure it out.
Any ideas please?
Thanks
Craig
Try this:
UPDATE PR
SET PR.[Start_Date] = A.[Corrected_Start_Date]
FROM ProjectDates PR
JOIN
(
SELECT
[Id],
RIGHT([Start_Date],4) +
CASE
WHEN len([Start_Date])=10
THEN substring([Start_Date],4,2) + LEFT([Start_Date],2)
ELSE RIGHT('0' + LEFT([Start_Date],firstIndex-1),2) +
RIGHT('0' + substring([Start_Date],firstIndex+1,secondIndex - firstIndex-1),2)
END [Corrected_Start_Date]
FROM
(
SELECT
[Id],
[Start_Date],
charindex('/',[Start_Date],1) firstIndex,
charindex('/',[Start_Date],charindex('/',[Start_Date],1)+1) secondIndex
FROM mbrProject
) S
) A
ON A.[ID] = PR.[Project_ID]
(
select mp.[Start_Date], charindex('/',mp.[Start_Date],1) firstIndex,
charindex('/',mp.[Start_Date],charindex('/',mp.[Start_Date],1)+1) secondIndex
from mbrProject mp
join ProjectDates pd
on mp.ID = pd.Project_ID
)A
At the end of the last line here, the names mp, pd, mbrProject and (the inner ProjectDates) no longer exist as table names or aliases. The only name applicable to this row set from here on is A, the alias you've provided.
So you need to include mp.ID in your select list, and then use A.ID in your outer comparison.
Also, not knowing what you're trying to do, I'd just want to make sure you're aware that ProductDates (the table being updated) and pd are referring to two separate instances of that table, an inner one and an outer one. I'm hoping that the final WHERE should be:
where A.[ID] = ProjectDates.Project_ID
The problem is that doing
Update ProjectDates
SET ProjectDates.[Start_Date] = (select ...)
you are trying to set one field of one row to the results of the sub-select statement, which, I presume, is returning multiple values.