Replace function in db2 - sql

I'm having some issues with a simple replace function. I need to replace a , with | for the point_of_contact column but I'm not sure why I'm receiving a -104 error. I have researched what I believed to be the correct syntax and tried a case statement and replace function but it is not working for me. I'm using DB2 and would appreciate your help.
SELECT RowNumber() over (PARTITION BY F13.DIM_PROJECT_ID ORDER BY F13.PROJECT_NAME),
F13.DIM_PROJECT_ID,
F2P.NAME_LAST,
F2P.NAME_FIRST,
--F2P.POINT_OF_CONTACT,
--CASE WHEN F2P.POINT_OF_CONTACT like '%,%' THEN Replace(F2P.POINT_OF_CONTACT,',','|') ELSE F2P.POINT_OF_CONTACT,
REPLACE(F2P.POINT_OF_CONTACT, ',', '|') AS F2P.POINT_OF_CONTACT,
F13.PROJECT_NAME,
F13.TITLE,
F2H.CREATION_DATE,
F13.FIELD A,
F2H.AMOUNT,
F2H.BUILDING_NAME,
F2H.PERCENTAGE,
F2H.ABILITY,
F2SB.HOURS16,
F2SB.HOURS33,
F2SB.HOURS100
FROM FACT_TABLE AS F13
INNER JOIN PERSONNEL AS F2P ON F13.DIM_PROJECT_ID = F2P.DIM_PROJECT_ID
LEFT JOIN JOB AS F2SB ON F13.DIM_PROJECT_ID = F2SB.DIM_PROJECT_ID
LEFT JOIN HOURS AS F2H ON F13.DIM_PROJECT_ID = F2H.DIM_PROJECT_ID

On your column alias, remove the table alias F2P.
REPLACE(F2P.POINT_OF_CONTACT, ',', '|') AS POINT_OF_CONTACT,

Related

SQL replace function error while using it to replace one string

I have event table in which there are two fields named as sport, event_name .
This was values such as:
{sport:"Athletic"; event_name:"Athletic 100 meter"}
What I want is to use replace function to replace the string in event_name that matches string in sport with nothing.
so the final output will be such :
{sport:"Athletic"; event_name:"100 meter"}
And I was also joined it with other table so only ID that are to be replace are also present in other table
so I used in this way in following code. But it should an error : "Expected item: < result-column > " . Thank you
SELECT
ae.id ,
ae.city AS event_city,
ae.sport,
REPLACE(ae.event,ae.sport,' ') AS event_name ,
FROM
athlete_events ae
inner join
players_personalinfo pp on
pp.id=ae.id
You need to define the table aliases:
SELECT ae.id AS event_id, ae.city AS event_city, ae.sport,
REPLACE(ae.event, ae.sport, ' ') AS event_name ,
ae.event
FROM athlete_events ae JOIN
players_personalinfo pp
ON pp.id = ae.id;
I would also advise you to trim the result:
TRIM(REPLACE(ae.event, ae.sport, ' ')) AS event_name,
This will remove leading and trailing spaces.
The REPLACE function is case sensitive. Try to check the data to make sure that the capitalization of each is the same.
The prior answers work, but you need to modify one of the field names in your query. In your description, you mentioned the field name is "event_name", but in your query, you reference just "event" (ae.event).
Also, I'm a little surprised that an event_id would join to a player's profile id. Seems a bit odd.
At any rate, I confirmed this SQL works in both postgres and oracle databases...
SELECT
ae.id AS event_id,
ae.city AS event_city,
ae.sport,
ae.event_name as event_name_original
REPLACE(ae.event_name,ae.sport,' ') AS event_name_kinda_ugly,
TRIM(REPLACE(ae.event_name,ae.sport,' ')) AS event_name_clean
FROM
athlete_events ae
inner join
players_personalinfo pp on pp.id=ae.id

What is the syntax problem here using this subquery inside where clause

SELECT p.pnum, p.pname
FROM professor p, class c
WHERE p.pnum = c.pnum AND c.cnum = CS245 AND (SELECT COUNT(*) FROM (SELECT MAX(m.grade), MAX(m.grade) - MIN(m.grade) AS diff
FROM mark m WHERE m.cnum = c.cnum AND m.term = c.term AND m.section = c.section AND diff <= 20)) = 3
Incorrect syntax near ')'. Expecting AS, FOR_PATH, ID, or QUOTED_ID.
Consider the following example:
SELECT COUNT(1)
FROM SYSCAT.TABLES T
WHERE
(
-- SELECT COUNT(1)
-- FROM
-- (
SELECT COUNT(1)
FROM SYSCAT.COLUMNS C
WHERE C.TABSCHEMA=T.TABSCHEMA AND C.TABNAME=T.TABNAME
-- )
) > 50;
The query above works as is. But the problem is, that if you uncomment the commented out lines, you get the following error message: "T.TABNAME" is an undefined name. and least in Db2 for Linux, Unix and Windows.
You can't push external to the sub-select column references too deeply.
So, your query is incorrect.
It's hard to correct it, until you provide the task description with data sample and the result expected.
I can see a potential syntax errors: It seems that CS245 refers to a value c.cnum may take and not a column name. If that is the case, it should be enclosed in single quotes.

Group_concat is not a built in function - HeidiSQL

I have a query with a group_concat in it but it returns an error that the group_concat is not a built in function. Please help me to resolve this. My query is like this:
SELECT cast(user_id as varchar(255)) AS member_id,skincareproductbarcode.barcode, skincareproduct.SCP_id, skincabinet.beautybox_id,
skincabinet.skpid, skincareproduct.photos, skincabinet.create_date, GROUP_CONCAT(MirrorProfile.serial_number, '') As Serial_Number
from skincabinet join skincareproduct on skincareproduct.scp_id = skincabinet.skpid
full join skincareproductbarcode on skincabinet.skpid = skincareproductbarcode.scp_id
full join membermirrorprofile on skincabinet.user_id = membermirrorprofile.member_id
full join mirrorprofile on MemberMirrorProfile.mirror_id = mirrorProfile.mirror_id
where skincareproduct.approval_flag = 'N' and skincareproduct.photos != ''
and substring(photos,52,3) = 'scp' order by skincareproduct.SCP_id group by skincareproduct.scp_id desc
How can I use group_concat or is there any other way around?
the concat of fields should not be the output of the SQL query. It is not made for that. Use the tool which receive the data to make this action.

Error in select statement, with union all in a subquery

In Oracle 11g, I came across an error for a query and cannot figure why it is erroring on me. Here is the query:
select
main_data.issue_number,
main_data.transaction_number
from
(
select
p1.payment_date,
p1.media_number,
p1.payment_amount,
p1.issue_number,
p1.advice_na_number,
name.name_address_line_1,
name.name_address_line_2,
name.name_address_line_3,
name.name_address_line_4,
name.name_address_line_5,
name.name_address_line_6,
name.name_address_line_7,
name.name_address_city,
name.state_code,
name.address_country_code,
name.zip_code,
name.tax_id_number,
p1.output_tx_number_prin,
p1.output_tx_number_int,
'' as "transaction_number",
p1header.check_account_number
from
p1
left join name on p1.name_address_number = name.name_address_number
left join p1header on p1.issue_number = p1header.issue_number
UNION ALL
select
check.date_of_payment,
check.media_number,
check.payment_amount,
check.issue_number,
check.payee_na_number,
name.name_address_line_1,
name.name_address_line_2,
name.name_address_line_3,
name.name_address_line_4,
name.name_address_line_5,
name.name_address_line_6,
name.name_address_line_7,
name.name_address_city,
name.state_code,
name.address_country_code,
name.zip_code,
name.tax_id_number,
'' as "output_tx_number_prin",
'' as "output_tx_number_int",
check.transaction_number,
check.dda_number as "check_account_number"
from check
left join name on check.payee_na_number = name.name_address_number
) main_data
Selecting individual fields like above will give me an "invalid identifier error". If I do select * then it gives me back the data without any error. What am I doing wrong here? Thank you.
The old quoted identifier problem... see point 9 in the database object naming documentation, and note that Oracle does not recommend using quoted identifiers.
You've put your column alias as lower case inside double-quotes. That means that any references to it also have to be quoted and exactly match the case. So this would work:
select
main_data.issue_number,
main_data."transaction_number"
from
...
But unless you have a burning need to have that alias like that - and I doubt you do as all the identifier names from the actual table columns are not quoted - it would be simpler to remove the double quotes from the inner selects:
select
main_data.issue_number,
main_data.transaction_number
from
(
select
...
'' as transaction_number,
p1header.check_account_number
...
UNION ALL
select
...
'' as output_tx_number_prin,
'' as output_tx_number_int,
check.transaction_number,
check.dda_number as check_account_number
...
You don't actually need to alias the columns in the second branch of the union; the column identifiers will all be taken from the first branch.

SQL Server Compact won't allow subselect but inner join with groupby not allowed on text datatype

I have the following sql syntax that I used in my database query (SQL Server)
SELECT Nieuwsbrief.ID
, Nieuwsbrief.Titel
, Nieuwsbrief.Brief
, Nieuwsbrief.NieuwsbriefTypeCode
, (SELECT COUNT(*) AS Expr1
FROM NieuwsbriefCommentaar
WHERE (Nieuwsbrief.ID = NieuwsbriefCommentaar.NieuwsbriefID
AND NieuwsbriefCommentaar.Goedgekeurd = 1)) AS AantalCommentaren
FROM Nieuwsbrief
I'm changing now to sql-server-ce (compact edition) which won't allow me to have subqueries like this. Proposed solution : inner join. But as I only need a count of the subtable 'NieuwsbriefCommentaar', I have to use a 'group by' clause on my base table attributes to avoid doubles in the result set.
However the 'Nieuwbrief.Brief' attribute is of datatype 'text'. Group by clauses are not allowed on 'text' datatype in sql-server-ce. 'Text' datatype is deprecated, but sql-server-ce doesn't support 'nvarchar(max)' yet...
Any idea how to solve this? Thx for your help.
I think that the solution could be easier. I don't know exactly how is your metadata but I think that this code could fit your requirements by simply using LEFT JOIN.
SELECT Nieuwsbrief.ID
, Nieuwsbrief.Titel
, Nieuwsbrief.Brief
, Nieuwsbrief.NieuwsbriefTypeCode
, COUNT(NieuwsbriefCommentaar.NieuwsbriefID) AS AantalCommentaren
FROM Nieuwsbrief
LEFT JOIN NieuwsbriefCommentaar ON (Nieuwsbrief.ID = NieuwsbriefCommentaar.NieuwsbriefID)
WHERE NieuwsbriefCommentaar.Goedgekeurd = 1
Edited: 2ndOption
SELECT N.ID, N.Titel, N.Brief, N.NieuwsbriefTypeCode, G.AantalCommentaren FROM Nieuwsbrief as N LEFT JOIN (SELECT NieuwsbriefID, COUNT(*) AS AantalCommentaren FROM NieuwsbriefCommentaar GROUP BY NieuwsbriefID) AS G ON (N.ID = G.NieuwsbriefID)
Please, let me know if this code works in order to find out another workaround..
regards,