When I use the "Or" operator (Secretary_Job_Title like '%Secretary%' or Secretary_Job_Title like '%Assistant%') I'm returning too many values.
How can I best use Like statement for Secretary and Assistant in the following query? Thanks in advance!!
SELECT STUFF((SELECT ';' + secretary
FROM [HandshakeProd].[dbo].[sp_attysecrel]
WHERE attorney = 'HC\' + Rtrim(p.EMPLOYEE_CODE)
AND secretary_job_title LIKE '%Secretary%'
FOR XML PATH('')), 1, 1, '') AS [Assistants]
Perhaps this is what you want?
SELECT STUFF((SELECT ';' + secretary
FROM [HandshakeProd].[dbo].[sp_attysecrel]
WHERE attorney = 'HC\' + Rtrim(p.EMPLOYEE_CODE)
AND (secretary_job_title LIKE '%Secretary%' OR secretary_job_title LIKE '%Assistant%')
FOR XML PATH('')), 1, 1, '') AS [Assistants]
Related
Hi everyone I'm trying convert this code so it works in postgres. I figured I will use to_char(dateinquar:: date, 'mm/dd/yyyy') instead of convert(varchar,dateinquar,101). But I'm not sure what can be used instead of FOR XML PATH? I don't need to generate any XML document. How would I get multiple dates in a single row separated by comma for the case below?
Case d.Closing WHEN 0 THEN NULL WHEN 1 THEN (
substring((SELECT convert(varchar,dateinquar,101) + ', ' FROM PDT_tblAReasons sub
WHERE sub.IDnumber = d.IDnumber and dateoutquar IS NULL
GROUP BY IDnumber,dateinquar FOR XML PATH('')),0,len((
SELECT convert(varchar,dateinquar,101) + ', ' FROM PDT_tblAReasons sub
WHERE sub.IDnumber = d.IDnumber and dateoutquar IS NULL
GROUP BY IDnumber,dateinquar FOR XML PATH('')))-0)) end As [Closing Date]
Thank you
I have this schema in fiddle
My code:
SELECT
MUID, weekcounter,
STUFF((SELECT ',' + Category
FROM tb EE
WHERE EE.MUID = E.MUID AND Ranknum <= 3
FOR XML PATH, TYPE).value(N'.[1]', N'nvarchar(max)'), 1, 1, N'') AS listStr
FROM tb E
GROUP BY E.MUID, E.weekcounter
I am getting wrong output like this:
I am expecting this output :
I don't have option to use string_aggr() in SQL Server 2014.
I believe if you want to get the desired output, you'd have to use the two columns you want to group by in the correlated subquery (in the STUFF part), too.
Try this code:
SELECT
MUID, weekcounter,
STUFF((SELECT ',' + Category
FROM tb EE
WHERE EE.MUID = E.MUID
AND EE.weekcounter = E.weekcounter
AND Ranknum <= 3
FOR XML PATH, TYPE).value(N'.[1]', N'nvarchar(max)'), 1, 1, N'') AS listStr
FROM
tb E
GROUP BY
E.MUID, E.weekcounter
I am using sequel server management studio 2014, and I am wondering how I can parse out what is extraneous data for a select query. I do not wish to modify the data, only to grab what I need from it for a SSRS data source, to automate a query. The query would look like what is listed below.
An example of a value in the comments field is listed below
'Based on PO #105680 - Thomas Test GRPO Reciept Validation query test'
Ideally I only want the value '105680' so I can use it as a lookup reference. Any help would be appreciated as always.
SELECT
[DocEntry]
,[DocNum]
,[DocType]
,[CANCELED]
,[Comments]
FROM [Billy].[dbo].[OPDN]
You can use CHARINDEX to find the PO # in the text and then SUBSTRING to parse out the number as long as the format is consistent.
;WITH TEMP AS (SELECT 'Based on PO #105680 - Thomas Test GRPO Reciept Validation query test' AS comment )
SELECT comment,
CASE WHEN TEMP.comment LIKE '%PO #%' THEN SUBSTRING(TEMP.comment, CHARINDEX('PO #', TEMP.comment) + 4, CHARINDEX('PO #', TEMP.comment, CHARINDEX(' ', TEMP.comment) + 1) - 4) END AS PO_NUM
FROM TEMP
Your query would be something like
SELECT
[DocEntry]
,[DocNum]
,[DocType]
,[CANCELED]
,[Comments]
,CASE WHEN Comments LIKE '%PO #%' THEN SUBSTRING(Comments, CHARINDEX('PO #', Comments) + 4, CHARINDEX('PO #', Comments, CHARINDEX(' ', Comments) + 1) - 4) END AS PO_NUM
FROM [Billy].[dbo].[OPDN]
I got stuck during database migration on PostgreSQL and need your help.
I have two tables that I need to join: drzewa_mateczne.migracja (data I need to migrate) and ibl_as.t_adres_lesny (dictionary I need to join with migracja).
I need to join them on replace(drzewa_mateczne.migracja.adresy_lesne, ' ', '') = replace(ibl_as.t_adres_lesny.adres, ' ', ''). However my data is not very regular, so I want to join it on first good match with the dictionary.
I've created the following query:
select
count(*)
from
drzewa_mateczne.migracja a
where
length(a.adresy_lesne) > 0
and replace(a.adresy_lesne, ' ', '') = (select substr(replace(al.adres, ' ', ''), 1, length(replace(a.adresy_lesne, ' ', ''))) from ibl_as.t_adres_lesny al limit 1)
The query doesn't return any rows.
It does successfully join empty rows if ran without
length(a.adresy_lesne) > 0
The two following queries return rows (as expected):
select replace(adres, ' ', '')
from ibl_as.t_adres_lesny
where substr(replace(adres, ' ', ''), 1, 16) = '16-15-1-13-180-c'
limit 1
select replace(adresy_lesne, ' ', ''), length(replace(adresy_lesne, ' ', ''))
from drzewa_mateczne.migracja
where replace(adresy_lesne, ' ', '') = '16-15-1-13-180-c'
I'm suspecting that there might be a problem in sub-query inside the 'where' clause in my query. If you guys could help me resolve this issue, or at least point me in the right direction, I'd be very greatful.
Thanks in advance,
Jan
You can largely simplify to:
SELECT count(*)
FROM drzewa_mateczne.migracja a
WHERE a.adresy_lesne <> ''
AND EXISTS (
SELECT 1 FROM ibl_as.t_adres_lesny al
WHERE replace(al.adres, ' ', '')
LIKE (replace(a.adresy_lesne, ' ', '') || '%')
)
a.adresy_lesne <> '' does the same as length(a.adresy_lesne) > 0, just faster.
Replace the correlated subquery with an EXISTS semi-join (to get only one match per row).
Replace the complex string construction with a simple LIKE expression.
More information on pattern matching and index support in these related answers:
PostgreSQL LIKE query performance variations
Difference between LIKE and ~ in Postgres
speeding up wildcard text lookups
What you're basically telling the database to do is to get you the count of rows from drzewa_mateczne.migracja that have a non-empty adresy_lesne field that is a prefix of the adres field of a semi-random ibl_as.t_adres_lesny row...
Lose the "limit 1" in the subquery and substitute the "=" with "in" and see if that is what you wanted...
I already have query to concatenate
DECLARE #ids VARCHAR(8000)
SELECT #ids = COALESCE(#ids + ', ', '') + concatenatedid
FROM #HH
but if I have to do it inline how can I do that? Any help please.
SELECT sum(quantity), COALESCE(#ids + ', ', '') + concatenatedid from #HH
Thanks.
Use the XML PATH trick. You may need a CAST
SELECT
SUBSTRING(
(
SELECT
',' + concatenatedid
FROM
#HH
FOR XML PATH ('')
)
, 2, 7999)
Also:
Join characters using SET BASED APPROACH (Sql Server 2005)
Subquery returned more than 1 value