Parsing Database string in SQL Select command - asp.net

So basically I have this column called Signature inserted in the database
Id = 1
Value = John, Micheal, Sara
Id = 2
Value = Mike, Steve, John
.
.
Now in asp.net I'm not sure how can I do select command and know if the value
John, Micheal, Sara
Has "Micheal" in it

To answer you question:
CHARINDEX() searches for a substring within a larger string, and returns the position of the match, or 0 if no match is found
SELECT * FROM [Table] WHERE CHARINDEX(Signature, 'Michael') > 0
Or
You can just use wildcards in the query (after IF, WHERE or ON):
SELECT * FROM [Table] WHERE Signature LIKE '%' + 'Michael' + '%'
But really you should be storing this data in a separate related table.

No. Don't.
Add a new table with rows for each signature name, and use a row for each signatory. That is the point of relational databases.
1 John
1 Michael
1 Sara
2 Mike
2 Steve
2 John
Or better still, add two new tables - one for the signatories, and one for the relation between that and your initial table
Signatories
1 John
2 Michael
ItemSignatures
ItemID SignatoryID
1 1
1 2
2 1

select *
from Signature
where value like '%Micheal%'

Related

SQLite select order by alphabet first, then all other characters

I have a SQLite db with a table, containing rows with different names. For example:
id
name
1
antony
2
%
3
10
4
stackoverflow
5
john
I get the data from this table with
SELECT * FROM table WHERE 1 ORDER BY name Asc LIMIT ?, ?
And it returns
id
name
2
%
3
10
1
antony
5
john
4
stackoverflow
But i want it to return names in alphabetical order first, then all other names which starts with non letters in the right order too. So i want to get:
id
name
1
antony
5
john
4
stackoverflow
2
%
3
10
How can i achieve that?
Use the operator GLOB to check if the name starts with a letter in the ORDER BY clause:
SELECT *
FROM tablename
ORDER BY name GLOB '[A-Za-z]*' DESC, name
See the demo.
Thanks to #forpas, just wish to add
If you wish to make Case insensitive sorting, you may try as below
SELECT *
FROM tablename
ORDER BY name GLOB '[A-Za-z]*' DESC, Upper(name)

Build a query to join multiple rows for same user on a single row (Access 2010)

Apologies if this has already been answered, I searched and searched but could not find this exact scenario.
I have a table of users, and a table with activities (add or remove). I would like to combine these into a single result via a query, with AddDate and RemoveDate as columns in the query.
Here is a sample of my data, and what I'm looking for.
Users
==============
ID User
--------------
1 John Doe
2 Jane Doe
3 John Smith
Activities
===========================================
ID UserID ActivityType ActivityDate
-------------------------------------------
1 1 Add 1/1/2017
2 2 Add 1/3/2017
3 3 Add 2/2/2017
4 1 Remove 2/6/2017
This is what I'd like my query to return
User AddDate RemoveDate
=====================================
John Doe 1/1/2017 2/6/2017
Jane Doe 1/3/2017
John Smith 2/2/2017
After looking at it again fresh this morning, I figured it out. Posting the answer for when I inevitably forget a year from now and have to Google for an answer!
SELECT [User],
[AddDate],
[RemoveDate]
FROM (( [Users]
LEFT JOIN ( SELECT [UserID],
[ActivityDate] AS AddDate
FROM [Activities]
WHERE [ActivityType] = "Add" ) AS [A1]
ON [Users].[ID] = [A1].[UserID])
LEFT JOIN ( SELECT [UserID],
[ActivityDate] AS RemoveDate
FROM [Activities]
WHERE [ActivityType] = "Remove" ) AS [A2]
ON [Users].[ID] = [A2].[UserID])

delete all rows if a field contains a value from another record

I've got a table like this:
Name Code
-------------------
John 1235
John 1235/11
John 1236/12
Mary 2500
Mary 2500/8
Mary 3600
Mary 3600/9
I want to delete all the rows where the value of code is contained in another row.
In the example I want to delete these records:
Name Code
-------------------
John 1235
Mary 2500
Mary 3600
Here is one method:
delete from t
where exists (select 1
from t t2
where t2.value like t1.value || '/%'
);
If you don't want to actually delete the records, but just want a query to not return them:
select *
from t
where not exists (select 1
from t t2
where t2.value like t1.value || '/%'
);
These assume that (as in the example), "is contained" really means "starts with before the "/".
Delete From tablename t
Where Exists
(Select * from table
Where charIndex(t.Code, Code) !=0)

sqlite Query getting name from another table by id from first

This is a beginner question (I am not to good with databases at the moment)
But here goes I have 2 tables
Table 1
User
id - name - status_id
0 John 0
1 Jim 1
2 Stan 0
Table 2
Status
id - name
0 employed
1 Not employed
I want to make a query that gives me Status name from User id.
Join the two tables on User.status_id and Status.id. Then select the user you are interested in.
select s.name
from User as u,
Status as s
where u.status_id = s.id
and u.id = 1;

Filtering in Oracle based on a group of values contained in a list of values

I have following two tables:
ID_PERSON NAME
-----------------
1 John
2 Joe
3 Peter
ID_PERSON ID_SPECIALIZATION
------------------------------
1 5
1 6
1 7
2 5
2 1
3 6
3 10
I need to filter data based on group of ids ID_SPECIALIZATION that will be provided. For example
I want to display only those persons who has specialization in 5 and 6 so it will return only first person. In ASP.NET Web form there will be two listboxes, left and right button, in first LB there will be all possible specializations and user will choose some of them to second LB as filtering options. I have no idea how to put this filtering condition in sql query. Thanks for help.
You could use the following:
SQL> SELECT p.id_person, p.NAME
2 FROM person p
3 JOIN person_spe s ON p.id_person = s.id_person
4 WHERE id_specialization IN (5, 6)
5 GROUP BY p.id_person, p.NAME
6 HAVING COUNT(*) = 2;
ID_PERSON NAME
---------- -----
1 John
One way to do it:
SELECT
ID_PERSON
, NAME
FROM
Person AS p
WHERE EXISTS
( SELECT *
FROM
PersonSpecialization AS ps
WHERE ps.ID_PERSON = p.ID_PERSON
AND ps.ID_SPECIALIZATION = 5
)
AND EXISTS
( SELECT *
FROM
PersonSpecialization AS ps
WHERE ps.ID_PERSON = p.ID_PERSON
AND ps.ID_SPECIALIZATION = 6
)
SELECT d1.id_person, d1.name FROM tbl_table1 d1
INNER JOIN tbl_table2 d1
ON d1.ID_PERSON=d2.ID_PERSON
WHERE ID_SPECILIZATION = ?
Theres the query but I'm not sure how asp.net works and passing in the value. It might be work looking up bind variables which allows you to use place holders in the sql which oracle then caches the query and just uses the values that you pass in at run tuime using EXECUTE IMMEDIATE.

Resources