Pass substring of values inside Where clause conditions - sql

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

Related

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

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'

Split with and get Results from T-SQL

I have some T-SQL code that pulls information from a SQL Server table. I need to parse a column and display according to following result set. I'm bew to SQL, it would be great if you don't mark as duplicate and show me how to do it.
Can you please help?
SELECT Account, CTSFirm, AccountName, BOCodeGMI
FROM StagingEDFACRRBO
BOCodeGMI column contains:
e=01:c=KW:m=10000
c=C-:e=01:m=10000
c=S-:e=01:m=10000
c=06:e=01:m=10
c=07:e=01:m=100
c=W-:e=01:M=10000
Logic to split BOCodeGMI and display two separate columns BOCodeGMI_1 & BOCodeGMI_2:
If string contains e= then display BOCodeGMI_1 as its corresponding value (ex: 01), if string doesn't contain e=, then display BOCodeGMI_1 as NULL
If string contains c= then display BOCodeGMI_2 as its corresponding value (ex: C-), if string doesn't contain c= then display BOCodeGMI_2 as NULL
Finally this is how it suppose to show -
BOCodeGMI BOCodeGMI_1 BOCodeGMI_2
-----------------------------------------------------
e=01:c=KW:m=10000 01 KW
c=C-:e=01:m=10000 01 C-
c=S-:e=01:m=10000 01 S-
Try the next query via using CASE, CHARINDEX & SUBSTRING
SELECT
BOCodeGMI,
CASE
WHEN CHARINDEX('e=', BOCodeGMI) > 0
THEN SUBSTRING (BOCodeGMI, CHARINDEX('e=', BOCodeGMI) + 2 , 2)
END as BOCodeGMI_1,
CASE
WHEN CHARINDEX('c=', BOCodeGMI) > 0
THEN SUBSTRING (BOCodeGMI, CHARINDEX('c=', BOCodeGMI) + 2, 2)
END as BOCodeGMI_2
FROM
tableName
CASE to go through conditions and return a value.
CHARINDEX to search for a substring in a string, and returns the position.
SUBSTRING to extract some characters from a string.

SQL Server: How to select rows which contain value comprising of only one digit

I am trying to write a SQL query that only returns rows where a specific column (let's say 'amount' column) contains numbers comprising of only one digit, e.g. only '1's (1111111...) or only '2's (2222222...), etc.
In addition, 'amount' column contains numbers with decimal points as well and these kind of values should also be returned, e.g. 1111.11, 2222.22, etc
If you want to make the query generic that you don't have to specify each possible digit you could change the where to the following:
WHERE LEN(REPLACE(REPLACE(amount,LEFT(amount,1),''),'.','') = 0
This will always use the first digit as comparison for the rest of the string
If you are using SQL Server, then you can try this script:
SELECT *
FROM (
SELECT CAST(amount AS VARCHAR(30)) AS amount
FROM TableName
)t
WHERE LEN(REPLACE(REPLACE(amount,'1',''),'.','') = 0 OR
LEN(REPLACE(REPLACE(amount,'2',''),'.','') = 0
I tried like this in place of 1111111 replace with column name:
Select replace(Str(1111111, 12, 2),0,left(11111,1))

Postgres query to calculate matching strings

I have following table:
id description additional_info
123 XYZ XYD
And an array as:
[{It is known to be XYZ},{It is know to be none},{It is know to be XYD}]
I need to map both the content in such a way that for every record of table I'm able to define the number of successful match.
The result of the above example will be:
id RID Matches
1 123 2
Only the content at position 0 and 2 match the record's description/additional_info so Matches is 2 in the result.
I am struggling to transform this to a query in Postgres - dynamic SQL to create a VIEW in a PL/pgSQL function to be precise.
It's undefined how to deal with array elements that match both description and additional_info at the same time. I'll assume you want to count that as 1 match.
It's also undefined where id = 1 comes from in the result.
One way is to unnest() the array and LEFT JOIN the main table to each element on a match on either of the two columns:
SELECT 1 AS id, t.id AS "RID", count(a.txt) AS "Matches"
FROM tbl t
LEFT JOIN unnest(my_arr) AS a(txt) ON a.txt ~ t.description
OR a.txt ~ t.additional_info
GROUP BY t.id;
I use a regular expression for the match. Special characters like (.\?) etc. in the strings to the right have special meaning. You might have to escape those if possible.
Addressing your comment
You should have mentioned that you are using a plpgsql function with EXECUTE. Probably 2 errors:
The variable array_content is not visible inside EXECUTE, you need to pass the value with a USING clause - or concatenate it as string literal in a CREATE VIEW statement which does not allow parameters.
Missing single quotes around the string 'brand_relevance_calculation_‌​view'. It's still a string literal before you concatenate it as identifier. You did good to use format() with %I there.
Demo:
DO
$do$
DECLARE
array_content varchar[]:= '{FREE,DAY}';
BEGIN
EXECUTE format('
CREATE VIEW %I AS
SELECT id, description, additional_info, name, count(a.text) AS business_objectives
, multi_city, category IS NOT NULL AS category
FROM initial_events i
LEFT JOIN unnest(%L::varchar[]) AS a(text) ON a.text ~ i.description
OR a.text ~ i.additional_info'
, 'brand_relevance_calculation_‌​view', array_content);
END
$do$;

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