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
Related
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?];
I'm trying to query the first letter of a last name in MySQL. I want to skip the first name and query the a certain letter in the last name. Thanks
In SKU_data, which SKU has a buyer whose last name begins with 'M'?
*/
select *
from sku_data
where buyer ;
In your where clause, search on WHERE buyer.LastName LIKE 'M%'. This will return all results that start with M.
You need to use a combination of the substring method and the substring_index method in your sql query.
Your select query should look something like this
SELECT SUBSTRING(last_name,1,1)
I'm assuming that your name field has both first and last name in it, instead of separate fields for first and last name. Use the substring_index method to figure out what last_name should be:
SUBSTRING_INDEX(full_name,' ',-1)
Combining this SQL, we have:
SELECT SUBSTRING(SUBSTRING_INDEX(buyer,' ',-1),1,1) as first_letter
Now you can use the first_letter field in a where clause to get your desired result:
SELECT *, SUBSTRING(SUBSTRING_INDEX(buyer,' ',-1),1,1) as first_letter
from sku_data
where first_letter = 'M' ;
Assuming that you have a field like say "full_name" which has first name and last name in the same column.
Lets say the full_name in table employee has a value "JEFF MOSTI"
You can simply use the following statement to get what you need
select * from employee where full_name regexp ' M';
I am assuming you are looking for the last name to start with 'M' and that there is a space (' ') between the first and last name.
I have inherited a simple table from a client, that stores two addresses (shipping address and business address) in just two fields shipping_address and business_address. There is a field called ship_to with enum('shipping','business'), and a field for a full name. The client wants an output as:
full name: address
Where "address" column should be picked by the ship_to value. If ship_to = shipping then use shipping_address column. If ship_to = business then use business_address column.
For instance:
Joe Smith: Roach Street #10, Rats Village
How I can use CONCAT with IF condition? I'm guessing something like this, which obviusly is just an idea:
SELECT CONCAT( full_name, ':', IF (ship_to=='shipping', shipping_address, business_address ) AS contact FROM `table`
But, what's the right syntax for doing this?
Lacking the closing )
SELECT
CONCAT( full_name, ':', IF (ship_to='shipping', shipping_address, business_address )) as contact
FROM TableName
I can't seem to get the substring_index() to work:
I have created a simple table as follows:
CREATE TABLE ContactList(
cont_id int(11) NOT NULL AUTO_INCREMENT,
last_name varchar(30),
first_name varchar(20),
interests varchar(100),
PRIMARY KEY(cont_id));
I then populated the ContactList table as follows:
INSERT INTO ContactList (last_name, first_name, interests)
VALUES
('Murphy', 'Dave', 'Golf, Pets, Basketball'),
('Murphy', 'Ben', 'Pets, Gym, Basketball'),
('Finn', 'Belinda', 'Pets, Tennis, Knitting'),
('Murphy', 'Steve', 'Pets, Archery, Fishing');
I ran a quick SELECT to ensure the data was entered correctly:
SELECT * FROM ContactList;
Then I ran the following query:
SELECT * FROM ContactList
WHERE last_name = 'Murphy'
AND SUBSTRING_INDEX(interests, ',' ,1) = 'Pets';
I was expecting to get two records back (which I did for Ben & Steve), however, for the 'Interests' column I was assuming I should only get one interest back if it equaled 'pets' (due to the substring_index) however, I got all interests back. How can I use the SUBSTRING_INDEX() to run the query and only get the first interest listed back for each record if it says 'Pets'?
BTW I am using MySQL Version 5.5.24 and I know the Interests would be best suited in their own table - I just want to see why substring_index is not picking the first item from the list if it equals 'pets'.
Thanks for any input,
Andy R ;-)
You're using SUBSTRING_INDEX in the WHERE clause, which determines which rows to include. That's good, but you also need to use it in the SELECT clause, which determines which columns to include.
Try this:
SELECT
last_name,
first_name,
SUBSTRING_INDEX(interests, ',' ,1) AS FirstInterestInList
FROM ContactList
WHERE last_name = 'Murphy'
AND SUBSTRING_INDEX(interests, ',' ,1) = 'Pets';
Although substring_index() will work for the first element, you really want find_in_list():
SELECT last_name, first_name, SUBSTRING_INDEX(interests, ',' ,1) AS FirstInterestInList
FROM ContactList
WHERE last_name = 'Murphy' and
find_in_set('pets', interests) = 1
The advantage of find_inset() is that it will work for arbitrary positions.
Just as a note, though, your delimiter is ', '. For find_in_set() to work best, you should have no space after the column.
Also, if you are doing queries like this, you should fix your data structure. It really wants a table called something like ContactInterests which contains one row for each contact and each interest.
I'm having a bit of trouble with something which I think must be pretty simple.
I have a table containing:
CREATE TABLE cities(
city_id INT PRIMARY KEY,
city_article TEXT,
city_name TEXT
);
So, the city_name is a simple field with for example name like 'PARIS' or 'NEW YORK'.
city_article is mostly empty, except for some cities (like french cities) who need an article in front of them like 'LA ROCHELLE' which is set in the database as
city_article = 'LA'
city_name = 'ROCHELLE'
I'd like to create a new column containing the concatenation of city_article and city_name, separated with a '-' and replace every space with the same '-'.
So if we take something like 'THE NEW YORK' separated in
city_article = 'THE'
city_name = 'NEW YORK'
the third column would be
city_concatenated = 'THE-NEW-YORK'
the trick is that since not all cities have an article, I don't want cities like 'PARIS' to become '-PARIS' after the operation.
Is there such a way to do that easily ?
TL;DR: how do I create a new column containing the result of 2 other columns whith one which can be NULL and the other one which must have every space replaced with '-' without putting a '-' where it's not needed due to the absence of the first field.
I am not sure whether or not you could have an empty string in City_article, but I've assumed this could happen:
SELECT CONCAT(CASE WHEN City_Article = '' OR City_Article IS NULL THEN '' ELSE CONCAT(City_Article, '-') END, REPLACE(city_name, ' ', '-'))
FROM Cities;
In terms of adding this as a column, well I wouldn't. Since MySQL does not allow computed columns I'd recommend creating a view that stores the definition of this column. This ensures the full name is always up to date and an additional column doesn't require updating if the article or the city name are updated:
CREATE VIEW Cities_Ext
AS
SELECT City_ID,
City_Article,
City_Name,
CONCAT(CASE WHEN City_Article = '' OR City_Article IS NULL THEN '' ELSE CONCAT(City_Article, '-') END,REPLACE(city_name, ' ', '-')) AS FullName
FROM Cities;
Example on SQL Fiddle
Put the first dash inside a COALESCE statement:
SELECT COALESCE(city_article + '-','') + REPLACE(city_name, ' ', '-')