How to find match number from comma separated numbers using sql query? - sql-server-2008

I have a SQL Server table with comma separated number as datatype varchar(200) like this:
Now I am passing parameter like comma separated number to my query for get a output like below:
Id MatchNumber
1 286
1 133
1 338
3 152
8 161
8 133
10 144
10 139
10 177
I have tried this SQL Query:
DECLARE #Ids varchar(50);
SET #Ids = '286,133,338,215,152,378,161,144,139,177';
select * from TblMMGameCard where Id IN (#Ids);
I want a output like above table. How can I do?

SQL Server does not support macro substitution. One option is to parse the search string and then test for the hit via a JOIN
Example
DECLARE #Ids varchar(50) = '286,530,338,215';
Select A.ID
,MatchNumber = B.RetVal
From TblMMGameCard A
Join (
Select RetVal = ltrim(rtrim(B.i.value('(./text())[1]', 'varchar(50)')))
From (values (cast('<x>' + replace(#IDs,',','</x><x>')+'</x>' as xml)))A(x)
Cross Apply x.nodes('x') AS B(i)
) B on charindex(','+RetVal+',',','+[cardNumbers]+',')>0

Related

concat type int for 2 col in presto sql

I have 2 columns of type int
and I want to make a concat to two columns in presto syntax. it is possible?
for example:
id:
345
997
age:
23
55
new_col:
34523
99755
I was trying to use the array function but it is not working :/
thanks to everyone!
As presto can not convert automatically
CONCAT(cast(id as varchar),cast(age as varchar))
You concatenate strings.
You calculate with integers.
So, multiply one column by 100, and add the other. And the result is another integer, not a string.
Data Types matter. And integers are way faster than strings.
WITH
-- your input, one table, don't use in final query ...
id(id,idcol) AS (
SELECT 1, 345
UNION ALL SELECT 2, 997
)
,
-- your input, other table, don't use in final query ...
age(id,agecol) AS (
SELECT 1, 23
UNION ALL SELECT 2, 55
)
-- real query starts here ...
SELECT
id.id
, idcol * 100 + agecol AS new_col
FROM id JOIN age USING(id)
ORDER BY 1
-- out id | new_col
-- out ----+---------
-- out 1 | 34523
-- out 2 | 99755

Compairing two strings in MYSQL

I have a sequence of 20 numbers from 0 to 2, I want to compare this string with other sequences saved in my database, the problem is that the lenght of the strings saved on the database fluctuates.Also the comparison needs to be done from the end to the start.
Example of what I want:
20 digits string:
'1,1,2,1,2,1,0,1,2,1,2,1,0,1,2,1,1,1,2,1'
couple of strings saved in the database:
1 - '1,1,2,1'
2 - '2,1,2,2,2,2'
3 - '2,1'
4 - '1,1,2,1,2,1'
In this case the query would return the 1 and 3 only
create table mytable ( s varchar(60) );
insert into mytable values
('1,1,2,1'),
('2,1,2,2,2,2'),
('2,1'),
('1,1,2,1,2,1');
set #x = '1,1,2,1,2,1,0,1,2,1,2,1,0,1,2,1,1,1,2,1';
select s from mytable
where right(#x, length(s)) = s;
Output:
s
1,1,2,1
2,1
Fiddle: https://www.db-fiddle.com/f/r5m2hPbnmUu5VQfYvMVtir/0
You could use a LIKE trick here. For example, to check for the first string 1,1,2,1:
SELECT *
FROM yourTable
WHERE ',1,1,2,1,2,1,0,1,2,1,2,1,0,1,2,1,1,1,2,1,' LIKE '%,1,1,2,1,%';

Finding count of unique value before a character

I have a some entries in database table rows as follows.
101 - 1
101 - 2
101 - 3
102 - 1
102 - 2
102 - 3
103
I need to get the result of SELECT Query for count as '3' since there are 101 and 102 are the only number before the -.
So is there any way to find the unique value in db table columns before a character?
EDIT : I have entries even without the - .
In case your entries have always the format you have provided us, you just have to find the position of the '-' character, split the values, get the first n characters and count the distinct values
This works for SQL Server, otherwise informs us about what DBMS you are using or replace the functions with the ones of your DBMS on your own
SELECT COUNT(DISTINCT SUBSTRING(val,0,CHARINDEX('-', val))) from YourTable
create table T1
(
id int primary key identity,
col1 varchar(20)
)
insert into T1 values('101 - 1'),('101 - 2'),('101 - 3'),('102 - 1'),('102 - 2'),('102 - 3')
select SUBSTRING(col1,0,CHARINDEX(' ',col1)) as 'Value',count(*) as 'Count' from T1 group by SUBSTRING(col1,0,CHARINDEX(' ',col1))

Concatenating 2 values from 2 consecutive recordsets | MySQL

I want to get the data from 1 SQL query of a table with 2 values
select c from tmp;
c
foo
bar
2 rows in set (0.00 sec)
The returned data I need is <foo,bar>
Concat does not do this nor does any string function I can find. I can SUM integers from 2 lines. Why can't I retrieve the string values likewise?
you can use GROUP_CONCAT() function to get values together.
This will combine all the string values separated by comma
Perhaps you need a group_concat() in MYSQL.
Noticing that you need in MYSQL here a sample:
* SQLFIDDLE demonstration
Select department, group_concat(name,',') as nameList
from foo
group by department
;
Results:
Department NameList
D1 John, Mary
D2 Tim, Dan, Jack
D3 Kate, Felix
Following is a method to use in TSQL:
You can try the following sample code and adjust it for your table/columns:
SELECT department, namelist = STUFF(
(SELECT ','+ Name FROM foo B
WHERE b.department = a.department FOR XML PATH('')) , 1 , 1 , '' )
FROM foo A
Or else you may do a CTE.

mysql lookup match

I have a database column like this:
id
50
55
56
62
63
64
65
68
70
72
80
etc...
I want to iterate through the id column with the following formula to find if the result of the formula is an id number in the same column. I want to compute all the possible combinations of the set of basically 3 records in the id column.
First loop:
Does ((second_id_number - first_id_number) * variable decimal) + second_id_number equal a number in the id column?
Per the formula, the first loop is
(55-50)*2.00(as an example of variable decimal) + 55 = 65. 65 is in the list => 65 is tagged with the 2 records which equal it
Second loop:
Does ((third_id_number - first_id_number) * variable decimal) + second_id_number equal a number in the id column?
(56-50)*2.00(as an example of variable decimal) + 56 = 78. 78 is not in the list => 78 is not tagged
Third loop:
Does ((fourth_id_number - first_id_number) * variable decimal) + second_id_number equal a number in the id column?
etc...
I want the results to show all the tagged records. A tagged record is the set of the 3 records where the third record is the result from the formula.
Anyone got any ideas? Is it possible in mysql?
Thank you
If I'm understanding your requirements properly, it sounds like you'd want to use a self-join on the table, e.g.
SELECT ...
FROM yourtable AS parent
LEFT JOIN yourtable AS child ON
FLOOR((parent.second_id_number - parent.first_id_number) * variable) + parent.second_id) = child.id
You could potentially carry something like this forward, which satisfies your first "loop"
select a.id as first_id_number
, b.id as second_id_number
, ((b.id - a.id) * 2) + b.id as third_id_number
from my_table as a
join my_table as b on a.id = (select max(id) from my_table where id < b.id)
where ((b.id - a.id) * 2) + b.id in (select id from my_table)
According to your description and test data, this would show 65 as "tagged" with first_id_number 50 and 62.
Warning: done on SQL Server using what I think is fairly standard syntax. I would understand if some would rather phrase this as a cross join with the select max... bit in the where clause rather than in the join predicate.