SQL Like advanced cmd - ms-access

I am using Microsoft Access 2013
I need to make a query so that the program can grab the last name of the ACTOR'S NAME when the ACTOR'S name has their full name for example Kelly Garman, and Weird Al Yankovic. Yankovic has 3 values and Garman has 2 values. Any help?
There are over 10,000 records with tons of different names, so I need to have it so the user will be asked to input the last name and once they do, the database will query by the last name.
Its also a query where the user inputs something in when asked.
Columns in the database are in the SELECT statement
SELECT [RECORD #], [ACTOR'S NAME], [PRODUCTION NAME]
FROM Actors AS A
WHERE [A.ACTOR'S NAME] = [What's the last name?]
The Database file is here. https://drive.google.com/open?id=0B19CRcQGkXoVTTZpaldDWnFuS28

You can use REVERSE along with SUBSTRING as below to get the last name.
REVERSE( SUBSTRING( REVERSE([ACTOR'S NAME]), 1, INSTR(' ', REVERSE([ACTOR'S NAME])) - 1))

To find the last name you need to get the word after the last space. You can't do this directly in MS Access. Instead, you can find the position of the last space with InstrRev(Name, ' '). Then use Right to get everything to the right of that, but that takes a position from the end of the string. To get that, subtract what you got from InstrRev from the length of the name.
SELECT Right(Name, Len(Name) - InstrRev(Name, ' ')) as 'Last Name'
FROM Actors
WHERE Right(Name, Len(Name) - InstrRev(Name, ' ')) = 'Yankovic';
Unfortunately I do not have a copy of MS Access to test this.

You can use a pattern match in the WHERE clause.
PARAMETERS [What's the last name?] Text ( 255 );
SELECT A.[Record #], A.[ACTOR'S NAME]
FROM Actors AS A
WHERE A.[ACTOR'S NAME] ALike '% ' & [What's the last name?];

Related

count multiple occurence substrings in MySql

I'm pulling data from Twitter API into my DB. There is a column 'hashtags' which stores a list of hashtags used in the tweet.
Table name: brexittweets
Column: hashtags varchar(500)
I want to count the number of hashtags. For example
Hashtags
Tweet1: ['EUref', 'Brexit', 'poll']
Tweet2: ['Brexit', 'Blair']
Tweet3: ['Brexit', 'Blair', 'EUref']
Result should be:
hashtag count(hashtag)
Brexit 3
EUref 2
Blair 2
poll 1
What I was thinking of doing:
Tried to take substring between quotes ' ', but it occurs multiple times in the same row.
Tried using strpos to find instances of ' ', but it returns only the first instance.
Is there a way to do this with queries? I was thinking of trying out a procedure, but it gets complicated because I need to print these results on a web page using PHP.
If only you've normalized your table such that each tag in a tweet gets stored on its own row, your problem would be solved easily by using COUNT with GROUP BY.
Assuming all the tags are separated by ', ', you can do the following:
SELECT
hashtags,
ROUND (
(
LENGTH(hashtags)
- LENGTH( REPLACE ( hashtags , "', '", "") )
) / 4) + 1
AS count
from brexittweets
Here's the SQL Fiddle.

How to remove certain string of charactors in mySQL?

Let me start off be saying I am new to trasactsql and query so please be kind.
I have a DB with about 1700 records for my product file, and one field (name) has the product code, a space and then the name of my product. I have another field (pcode) with just the product code that has numbers and possibly a letter or two that matches the beginning of the "name" field. I need to remove all characters in front of the space in 'name" including the space. Is there a way to remove the characters if they match what is in "pcode"?
Here is an example.
What I have;
pcode | name
1234 | 1234 widget
What I want;
pcode | name
1234 | widget
There are some names with spaces in them. ie. "Left Handle Button side" or "Control "Lever"
Thanks in advance.
Michael
In a select, you can do:
select pcode, substring_index(name, ' ', -1)
. . .
In an update:
update table t
set name = substring_index(name, ' ', -1)
where name like '% %';
If you want to change the data in place (using an update), check the logic (or copy the table) before doing the update. This might not do what you expect, for instance, if the name itself has a space in it.
Try to export it to MS Excel for example and do it manually. Next time learn about 12 gold rules when creating SQL database.
http://en.m.wikipedia.org/wiki/Codd%27s_12_rules

Concatenate first and last name with last name all in uppercase

I need to make a mysql query that displays all first names concatenated with the last name in uppercase, with a space between first and last name.
What I've got atm:
SELECT CONCAT_WS(" ", `First_Name`, `Last_Name`) AS Name from tblDetails
I'm new to mysql and I can't figure out where to put UPPER, doesn't seem to work anywhere.
Also I need to do the same as above but add a prefix of Mr or Ms depending on the gender.
you can use case and concat to add prefix based on gender and upper can be applied to both First_Name, Last_Name column values and then use concat_ws to append them with a space in between.
SELECT CONCAT ( (case when gender ='M'
then 'Mr '
else 'Ms '
end
),
CONCAT_WS(" ", UPPER(First_Name), UPPER(Last_Name))
)
AS Name
from tblDetails

Mysql Order by last name when full name for column

I have the following:
SELECT * FROM users LEFT JOIN user_info ON users.id=user_info.user_id
WHERE
((user_info.tester != 1) OR (user_info.tester is null)) AND
id in (SELECT explicituser_id FROM user_login WHERE (created < '2012-12-17' OR created >= date_add('2012-12-17', interval 1 day))) AND
id IN (SELECT participte_id FROM roster WHERE roster_id IN (6))
order by
substring_index(users.name, ' ', -1)
I'm simply trying to sort by the users' last name.
However, while it can sort by the first name, the last name is buggy. If the user has quotes around their name (ie. "Abigail Martinez" it will make the sorting incorrect. If the user provides only one name, and it's a nickname (ie. Juan), then it will also make it incorrect. And then there's middle initials (ie. Tiffany S Villa or Steve de la Makinov). Unfortunately, this uses only one column for the full name (users.name).
Any help is appreciated. Thanks!
substring_index(TRIM(users.name), ' ', -1) Adding TRIM will remove trailing spaces. After that, sorting occurs as expected.
if it is the case try to get all the rows and then may try with mysql_real_escape_string. so that you will not be having quotes. it'll be something like this.
$row['new_name'] = mysql_real_escape_string($row['name']);
I'm not too sure performance wise whether its good to go for it or not..
Hope this may helps

Taking only characters from the left and right of a specific character in mySQL

I have a list table with where one of the variables is Player and if has a players first name then an "_" and there last name like this:
Mike_Gonzalez
I would like to create two new variables from the player variables. The first variable would be firstName, so I would want all the characters to the left of the "". The second variable would be lastName, and it would be all the characters to the right of the "".
I've tried using LEFT(Player, LOCATE('_', Player)), but when I do, the new variable includes the _ .
How can I run the code where I would be able to jus get the first and last names without the _ ?
Thanks for any help.
Besides combining LEFT() and RIGHT() with LOCATE(), you can also use SUBSTRING_INDEX():
SELECT
SUBSTRING_INDEX(Player, '_', 1) AS FirstName
, SUBSTRING_INDEX(Player, '_', -1) AS LastName
LEFT(Player, (LOCATE('_', Player) - 1))
You just basicly decrease the value of the second parameter in LEFT by 1 to exclude the _
select distinct left(HOST,locate (':',HOST)-1) CONNECTED_HOST
from information_schema.processlist
where User != 'root'
order by 1;