How to use REPLACE in Select Query? (SQL SERVER) - sql-server-2008

Can I use REPLACE function conditionally?
e.g. I have a query:
SELECT diag_type FROM DIAGNOSIS
It returns AXIS, AXISI, AXISII, AXIS-C, etc. I want if it returns a result that has AXIS, it makes it AXISIII. Can it be possible?
Thanks in advance.

;with DIAGNOSIS(diag_type) as
(
select 'AXIS' union all
select 'AXISI' union all
select 'AXISII' union all
select 'AXIS-C'
)
select
case diag_type
when 'AXIS' then 'AXISIII'
else diag_type
end as diag_type
from DIAGNOSIS
Result:
diag_type
---------
AXISIII
AXISI
AXISII
AXIS-C

If all you want is it to return AXISIII you can just write:
SELECT 'AXISIII' FROM DIAGNOSIS WHERE diag_type = 'AXIS'
You can also use a CASE-statement to add more options (if you need).
SELECT CASE(diag_type)
WHEN 'AXIS' THEN 'AXISIII'
WHEN 'AXIS-C' THEN'AXISIII-C'
WHEN 'AXISII' THEN'AXISII'
ELSE 'Something'
END
FROM DIAGNOSIS

Related

Flexible search Query for stringset?

Please find the below query:
Table Name : B
query : select * from {B}
ID comp
1 d,e,f
I want to check if the value 'f' is present in comp, or not, using an SQL/Flexible search Query.
Is it possible to write a sql query for this scenario?
Update :
SELECT DISTINCT {b:pk} FROM {A AS a left join B as B on {a:ncode} = {b:ncode} and {a:qCode} = {b:qCode}}
WHERE
{a:compID} IN ()
Assume a:compID is "f"
What should be my subquery after the IN operator to achieve my requirement?
SELECT * FROM {B} where {comp} LIKE '%,f,%'
You can search in string in mysql:
//full search:
SELECT * FROM A WHERE comp LIKE '%f%'
// start with f
SELECT * FROM A WHERE comp LIKE '%f'
// end with f
SELECT * FROM A WHERE comp LIKE 'f%'
This will work:
SELECT FIND_IN_SET('f',(SELECT comp FROM t));
Run this code on Fiddle
If you want to check present or not present then:
SELECT case when FIND_IN_SET('f',(SELECT comp FROM t))=0 then'not present'
else 'present' end from dual;
Start with f
LIKE 'f%'
End with f
LIKE '%f'

How to query a SQL statement which depends on other values of same table?

I have a table with 3 columns( name, objectroot_dn, distinguishedname). Here distinguishedname is like a parent to objectroot_dn. I have to find whether for each objectroot_dn is there a child exists or not?
I can do this using the query below. It will return True if there is a child, False if there is not. But my problem is when the total dataset gets increased it takes lots of time.
For example, If the total number of row is 50,000 then it takes 10 mins for this query to complete.
Since I'm using a framework for different database, I can't index the columns.
SELECT
name,
objectroot_dn,
distinguishedname,
CASE
WHEN (SELECT count(*)
FROM (SELECT name
FROM elaoucontainergeneraldetails
WHERE objectroot_dn = dn.distinguishedname
LIMIT 1) AS tabel1) > 0
THEN 'True'
ELSE 'False'
END
FROM elaoucontainergeneraldetails AS dn
WHERE objectroot_dn = 'SOME_VALUE';
Please let me know how can I increase the speed of this query.
Thanks in advance. Appreciate all help.
You can have the same solution using left join or exists:
SELECT
dn.name,
dn.objectroot_dn,
dn.distinguishedname,
CASE
WHEN dn_in.objectroot_dn is not null
THEN 'True'
ELSE 'False'
END
FROM elaoucontainergeneraldetails AS dn
LEFT JOIN elaoucontainergeneraldetails dn_in on dn_in.objectroot_dn = dn.distinguishedname
WHERE objectroot_dn = 'SOME_VALUE';
EXISTS(subquery) yields a boolean value:
SELECT dn.name
, dn.objectroot_dn
, dn.distinguishedname
, EXISTS (SELECT *
FROM elaoucontainergeneraldetails nx
WHERE nx.objectroot_dn = dn.distinguishedname
) AS truth_value
FROM elaoucontainergeneraldetails AS dn
WHERE dn.objectroot_dn = 'SOME_VALUE'
;

"Subquery must return only one column" error in postgresql

I have got an error "ERROR: subquery must return only one column " when I am runing this query:
INSERT INTO details (id, object_id, detail)
(
SELECT
CASE
WHEN (SELECT * FROM details WHERE NOT EXISTS(SELECT 1 FROM main_base WHERE main_base.id = details.id))
THEN
concat(SUBSTRING(main_base.id, '(\d+.\d+.)'), n.counted :: TEXT, 'A')
ELSE
concat( SUBSTRING (main_base.id, '(\d+.\d+.)'), n.counted :: TEXT)
END AS id,
main_base.object_id,
main_base.details
FROM main_base
CROSS JOIN LATERAL
generate_series(1, COALESCE ((string_to_array(main_base.id, '-')) [2] :: INT, 1)) AS n (counted)
WHERE main_base.id LIKE '%-%' AND NOT main_base.details ~ '^\.\d+|\(\.\d+\)'
);
I have not clue what is wrong. I've read some topic that people had the same problem but still dont know how to fix it.
I think the problem is that:
SELECT * FROM details WHERE NOT EXISTS(SELECT 1 FROM main_base WHERE main_base.id = details.id)
Can return more than one row, so causes problems in the WHEN statement. It can return more than one row, as the subquery will return 1 every time the condition is met.
If you want to trigger the case statement based on when there exists some records in this set, could you use:
(SELECT COUNT(*) FROM details WHERE NOT EXISTS(SELECT 1 FROM main_base WHERE main_base.id = details.id)) > 1

Select query in mysql using COALESCE

I want to write a select statement that should filter data based on wildcard characters. I have written something like this but it doesn't serve my purpose:
Select r.CompanyID,r.Description,c.BusinessUnitID,c.BusinessSourceID as BusinessSrcID,
c.Description as BusinessDesc from RCompanyTable r
join CBusinessUnitTable c on r.CompanyID=c.CompanyID
WHERE r.CompanyID like CASE WHEN COALESCE('Regexp(*)', '') = '' THEN r.CompanyID ELSE 'Company2' END
But in this it always executes the else part.
What i am looking for is it should give me all data when i pass * to the condition.
Since in my RCompanyTable i have two records Company1 and Company2, I want that if i pass * in that query then it should return me both company1 and company2 data but if i pass regexp(any1) it should provide me Comapany1 Data and if both conditions are not true then it should go to else part displaying Company2 data
Looking forward to your answer.
Thanks in advance
I don't know exactly what you want with * and regexp(any1), but assuming that they are constant strings, then this query should work:
SET #parameter = '';
Select r.CompanyID
, r.Description
, c.BusinessUnitID
, c.BusinessSourceID AS BusinessSrcID
, c.Description AS BusinessDesc
FROM RCompanyTable r
JOIN CBusinessUnitTable c ON r.CompanyID = c.CompanyID
WHERE (#parameter LIKE '%*%' AND r.CompanyID IN ('Company1', 'Company2'))
OR (#parameter LIKE 'regexp(any1)' AND r.CompanyID = 'Company1')
OR (#parameter NOT IN ('%*%', 'regexp(any1)') AND r.CompanyID = 'Company2')
The #parameter is what you are going to pass in the query.
WHERE r.CompanyID = CASE WHEN r.CompanyId LIKE '%*%' THEN r.CompanyID ELSE 'Company2' END
This where clause returns all records where companyID has a * in it, or the companyID is Company2. Is this what you are after?

SQL: How to use SELECT results as function input parameters

Here is my query:
SELECT
CASE
WHEN hbn.users.showDistance = 'T'
THEN hbn.distance(u2.lat, u2.lon, hbn.users.lat, hbn,users,lon)
ELSE 0
END as distance,
hbn.users.id,
hbn.users.username,
From hbn.users,
(select hbn.users.lat, hbn.users.lon from hbn.users where hbn.users.id = '1') AS u2
where hbn.users.Id = '8';
This does not work!
I need to use output of the second select statement as input for distance() function.
It looks like you have commas instead of full-stops in the last parameter to hbn.distance?