How to remove the first character from the join statement in where clause of the SQL query - sql

I want to remove the first character returning from the cvdl.C statement in the below SQL query and that value should be match up with the ccp.B value.
For an example if the real value return by the cvdl.C statement is 4500, I want to remove 4 and take only the 500 part to match with the value in the ccp.B value. Also I need to pass a input parameter value for the query.
How can I modify below SQL query to achieve this objective?
SELECT ccp.A
FROM ccp, cvdl
WHERE cvdl.J = 'Example' and ccp.B = cvdl.C

you should use the ansi-syntax to join
select ccp.A,
from ccp
inner join cvdl on ccp.B = substr(cvdl.C, 2)
where cvdl.J = 'Example'

Related

compare absolute value with double value present in table using PostgreSQL

select id from records where (mean_logratio = -4.81)
-4.810215473175049 value is present in the table which supposed to be fetched
-4.810215473175049 only exact value is accepted not absolute value, in search query for = and != condition
for absolute value like -4.81, not getting expected results
You can go with either approach:
If you want to compare after rounding off upto two decimal place.
select distinct(workflowid)
from cyto_records r join cyto_record_results rr on (r.recordid = rr.recordid)
where (round(rr.mean_logratio::numeric,2) = -4.81)
If you want to truncate upto two decimal and compare then use below mentioned query:
select distinct(workflowid)
from cyto_records r join cyto_record_results rr on (r.recordid = rr.recordid)
where (trunc(rr.mean_logratio::numeric,2) = -4.81)
In case of data type mismatch error, you may need to cast you data.

Case in Where Clause SQL (Oracle)

I want to filter a query in the Where Statement. I want to compare IDs with 'IN' but only if an input has value.
If this text comes empty I don't want to compare. Prevent the error query
EXAMPLE:
StringProductIDs = ''
WHERE
({Issue}.[ServiceRequestId] IN (#StringServiceRequestIDs))
AND ({ServiceRequest}.[Id] IN (#StringServiceRequestIDs))
AND ({Product}.[Id] IN (#StringProductIDs) OR #StringProductIDs = '') /* Is this one that i want to prevent*/
AND EXTRACT(MONTH FROM {Sprint}.[CompleteDate]) = #Month
AND EXTRACT(YEAR FROM {Sprint}.[CompleteDate]) = #Year
In this case, I just want to filter things.id if the value passed as a parameter has value. If it comes empty I don't want to compare and avoid the 'AND' line
Or try another approach:). There is a way to run a query only if this input has value? If it has no value, it returns nothing. If the input has value run normally –
Many thanks
You need OR :
WHERE (things.active = 1 AND things.state = 2) AND
(things.id IN (#INPUT) OR #INPUT IS NULL);

Pass substring of values inside Where clause conditions

I have a requirement to pass substring of parameter values inside where clause statement
Parameter Value is pmtr: CN
Query:
I want to filter CAN country from the below table using one column but my parameter value holds one 2 digits (CN) but I have to filter records for Canada country from database which holds 3 digits (CAN). I am trying to get the substring of 1st position of parameter value and then appends with hardcoded char 'A' then again take substring of second position of parameter value in below query.
Select a,b
from tab
where a='pmtr[1,1]':'A':'pmtr[2,1]'
Tried with substring function it didn't work giving no result. Is there any way to achieve in filter condition using sql?
If you want to filter only Canada (CN):
Select a,b
from tab
where CASE WHEN pmtr = 'CN' and a= 'CAN' THEN 1 ELSE 0 END =1
Note: This has a hard coded condition.
Try with using a variable:
declare #var =concat(pmtr[1,1],'A',pmtr[2,1])
Select a,b
from tab
where a=#var

Using the '?' Parameter in SQL LIKE Statement

I'm accessing a Firebird database through Microsoft Query in Excel.
I have a parameter field in Excel that contains a 4 digit number. One of my DB tables has a column (TP.PHASE_CODE) containing a 9 digit phase code, and I need to return any of those 9 digit codes that start with the 4 digit code specified as a parameter.
For example, if my parameter field contains '8000', I need to find and return any phase code in the other table/column that is LIKE '8000%'.
I am wondering how to accomplish this in SQL since it doesn't seem like the '?' representing the parameter can be included in a LIKE statement. (If I write in the 4 digits, the query works fine, but it won't let me use a parameter there.)
The problematic statements is this one: TP.PHASE_CODE like '?%'
Here is my full code:
SELECT C.COSTS_ID, C.AREA_ID, S.SUB_NUMBER, S.SUB_NAME, TP.PHASE_CODE, TP.PHASE_DESC, TI.ITEM_NUMBER, TI.ITEM_DESC,TI.ORDER_UNIT,
C.UNIT_COST, TI.TLPE_ITEMS_ID FROM TLPE_ITEMS TI
INNER JOIN TLPE_PHASES TP ON TI.TLPE_PHASES_ID = TP.TLPE_PHASES_ID
LEFT OUTER JOIN COSTS C ON C.TLPE_ITEMS_ID = TI.TLPE_ITEMS_ID
LEFT OUTER JOIN AREA A ON C.AREA_ID = A.AREA_ID
LEFT OUTER JOIN SUPPLIER S ON C.SUB_NUMBER = S.SUB_NUMBER
WHERE (C.AREA_ID = 1 OR C.AREA_ID = ?) and S.SUB_NUMBER = ? and TI.ITEM_NUMBER = ? and **TP.PHASE_CODE like '?%'**
ORDER BY TP.PHASE_CODE
Any ideas on alternate ways of accomplishing this query?
If you use `LIKE '?%', then the question mark is literal text, not a parameter placeholder.
You can use LIKE ? || '%', or alternatively if your parameter itself never contains a LIKE-pattern: STARTING WITH ? which might be more efficient if the field you're querying is indexed.
You can do
and TP.PHASE_CODE like ?
but when you pass your parameter 8000 to the SQL, you have to add the % behind it, so in this case, you would pass "8000%" to the SQL.
Try String Functions: Left?
WHERE (C.AREA_ID = 1 OR Left(C.AREA_ID,4) = "8000")

Problem with MySQL Select query with "IN" condition

I found a weird problem with MySQL select statement having "IN" in where clause:
I am trying this query:
SELECT ads.*
FROM advertisement_urls ads
WHERE ad_pool_id = 5
AND status = 1
AND ads.id = 23
AND 3 NOT IN (hide_from_publishers)
ORDER BY rank desc
In above SQL hide_from_publishers is a column of advertisement_urls table, with values as comma separated integers, e.g. 4,2 or 2,7,3 etc.
As a result, if hide_from_publishers contains same above two values, it should return only record for "4,2" but it returns both records
Now, if I change the value of hide_for_columns for second set to 3,2,7 and run the query again, it will return single record which is correct output.
Instead of hide_from_publishers if I use direct values there, i.e. (2,7,3) it does recognize and returns single record.
Any thoughts about this strange problem or am I doing something wrong?
There is a difference between the tuple (1, 2, 3) and the string "1, 2, 3". The former is three values, the latter is a single string value that just happens to look like three values to human eyes. As far as the DBMS is concerned, it's still a single value.
If you want more than one value associated with a record, you shouldn't be storing it as a comma-separated value within a single field, you should store it in another table and join it. That way the data remains structured and you can use it as part of a query.
You need to treat the comma-delimited hide_from_publishers column as a string. You can use the LOCATE function to determine if your value exists in the string.
Note that I've added leading and trailing commas to both strings so that a search for "3" doesn't accidentally match "13".
select ads.*
from advertisement_urls ads
where ad_pool_id = 5
and status = 1
and ads.id = 23
and locate(',3,', ','+hide_from_publishers+',') = 0
order by rank desc
You need to split the string of values into separate values. See this SO question...
Can Mysql Split a column?
As well as the supplied example...
http://blog.fedecarg.com/2009/02/22/mysql-split-string-function/
Here is another SO question:
MySQL query finding values in a comma separated string
And the suggested solution:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set