I have a column empname in a table, the data stored is Lastname,Firstname.
For ex:
empname:
Martin,Ricky
Ford,Henry.
How to do i via select mysql query get the o/p as:
First name Last name
Ricky Martin
Henry Ford
Try this:
SELECT SUBSTRING_INDEX(empname, ',', -1) AS First_Name,
SUBSTRING_INDEX(empname, ',', 1) AS Last_Name
FROM table
Though I would prefer to do this on the application doing the output.
Related
I need to produce the following:
Comp No Year Purchased
=========================
11111 2008
22222 2007
33333 2008
44444 2006
But I need to exclude some results based on a concatenated name. And I can't have the name listed in the result. This is my code:
SELECT
Comp_Num,
YEAR (Comp_PurchaseDate) AS 'Year Purchased',
CONCAT(Emp_First, ' ', Emp_Last) as 'Name'
FROM Computer
JOIN Hire using (Comp_Num)
JOIN employee using (Emp_Num)
ORDER BY Comp_Num;
It produces:
Comp No Year Purchased Name
================================
11111 2008 AAA
22222 2007 BBB
33333 2008 CCC
44444 2006 DDD
The concatenated name is used to filter out results, eg:
WHERE ('Name' <> 'AAA' AND
Name' <> 'DDD')
How do I create the concatenated name to filter out results without displaying the column? The concatenate doesn't work without "as 'name' ".
And how do I use the concatenated name to filter? Can I still use Where? Or is there another clause?
You can simply define the expression in the WHERE clause instead:
SELECT
Comp_Num,
YEAR (Comp_PurchaseDate) AS `Year Purchased`
FROM Computer
JOIN Hire using (Comp_Num)
JOIN employee using (Emp_Num)
WHERE (CONCAT(Emp_First, ' ', Emp_Last) <> 'AAA' AND
CONCAT(Emp_First, ' ', Emp_Last) <> 'DDD')
ORDER BY Comp_Num;
Also Read: When to use single quotes, double quotes, and back ticks in MySQL
A simpler alternative to Madhur's two CONCAT() calls is to use NOT IN () and only concatenate once.
SELECT Comp_Num,
YEAR (Comp_PurchaseDate) AS `Year Purchased`
FROM Computer
JOIN Hire USING Comp_Num
JOIN employee USING Emp_Num
WHERE CONCAT(Emp_First, ' ', Emp_Last) NOT IN ('AAA','DDD')
ORDER BY Comp_Num
I'm trying to create a query that lists staffID, staffName and staffDOB, but only of staff that first and last names begin with the same letter. So I have both staffFirst and staffLast as individual columns, will join them together. I will not be customising staffID and staffDOB. I would like it to return the name of staff like adam apple = a apple, so the output would look like:
staffID | staffName | staffDOB
------------------------------
1 | A Apple | 12/10/99
.... | .... | ....
All columns are in the same table "N_Staff". I am using HeidiSQL which I believe uses MySQL. I know how to grab the data of each column, though it is selecting the first letters of both first and last names and comparing them which is confusing me as it is not an specific letter I am looking for but any letter that is common on both tables of index [0].
Thus far:
SELECT staffID FROM N_Staff,
SELECT staffFirst, staffLast AS staffName
FROM N_Staff WHERE ... , --perhaps should be using LEFT ?
SELECT staffDOB from N_Staff;
How about:
SELECT staffID, CONCAT(LEFT(staffFirst,1), ' ', staffLast) AS staffName, staffDOB
FROM N_Staff
WHERE LEFT(staffFirst,1) = LEFT(staffLast,1)
use this:
SELECT * FROM NS_WORDS;
mani
nikhil
sugandh
mining
_lkdnsad
_lkdndsadnjas
_lk
_ja
_ls
_lsa
nikhil nikhil
nikhil name
SELECT * FROM NS_WORDS
where not( to_char(SUBSTR(a,1,1))=to_char(substr(a,instr(a,' ',1,1),1)));
output:
nikhil nikhil
nikhil name
your where will go like:
where not( to_char(SUBSTR(staffName ,1,1))=to_char(substr(staffName
,instr(staffName ,' ',1,1),1)));
my mysql table name is: student having 3 fields id(pk, auto increment), name(varchar 200) and student_id(varchar 200)
values are lik
id name student_id
1 abc 123-234
2 efg 2345678
3 xyz 1234-9
4. pxy 323-289
now i want to select those students whose id like 3-2
SELECT * FROM student
WHERE student_id like '%3-2%'
unfortunately the query return zero result.
any help is highly appreciable.
SELECT * FROM student
WHERE student_id like '%3-2%'
This works alright when I tested on MySql using PhpMyadmin.
If you use some other SQL, then try replacing single quotes to double quotes (' ' to " ").
Try firing the following query.
SELECT * FROM student
WHERE student_id like "%3-2%"
I have a database field "name" that contains both first name and last name of the user.
For Example:
id name
1 Martha Ron
2 Ryan Knapp
3 John Mithchell
4 Scott Mathews
5 Dean Johns
.....
.....
How can i get all users order by last name?
Note: I can not create a another field in the database as it is a production database.
Try this SUBSTRING_INDEX
SELECT
* FROM `table`
ORDER BY
SUBSTRING_INDEX(`name`, ' ', -1);
See fiddle demo
Try this ... ref http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substring-index
SELECT *, SUBSTRING_INDEX(`name`, ' ', -1) as sortname
from tablename order sortname;
I wanted to write a t-sql query which finds values within a column of a sql server table.
Example,
CREATE TABLE Transactions (Details varchar(max));
Details Column has below type strings stored in it
ID=124|NAME=JohnDoe|DATE=020620121025|ISPRIMARY=True|
TRANSACTION_AMOUNT=124.36|DISCOUNT_AMOUNT=10.00|STATE=GA|
ADDR1=test|ADDR2=test22|OTHER=OtherDetailsHere
ID=6257|NAME=michael|DATE=050320111255|ISPRIMARY=False|
TRANSACTION_AMOUNT=4235.00|DISCOUNT_AMOUNT=33.25|STATE=VA|
ADDR1=test11|ADDR2=test5|OTHER=SomeOtherDetailsHere
Objective is to write query which gives below output
Name | Transaction Amount | Discount
-------------------------------------------
JohnDoe | 124.36 | 10.00
michael | 4235.00 | 33.25
Any help would be highly appreciated.
Thanks,
Joe
Why are you storing your data pipe delimited in a single column -- these fields should be added as columns to the table.
However, if that isn't an option, you'll need to use string manipulation. Here's one option using a couple Common Table Expressions, along with SUBSTRING and CHARINDEX:
WITH CTE1 AS (
SELECT
SUBSTRING(Details,
CHARINDEX('|NAME=', DETAILS) + LEN('|NAME='),
LEN(Details)) NAME,
SUBSTRING(Details,
CHARINDEX('|TRANSACTION_AMOUNT=', DETAILS) + LEN('|TRANSACTION_AMOUNT='),
LEN(Details)) TRANSACTION_AMOUNT,
SUBSTRING(Details,
CHARINDEX('|DISCOUNT_AMOUNT=', DETAILS) + LEN('|DISCOUNT_AMOUNT='),
LEN(Details)) DISCOUNT_AMOUNT
FROM Transactions
), CTE2 AS (
SELECT
SUBSTRING(NAME,1,CHARINDEX('|',NAME)-1) NAME,
SUBSTRING(TRANSACTION_AMOUNT,1,CHARINDEX('|',TRANSACTION_AMOUNT)-1) TRANSACTION_AMOUNT,
SUBSTRING(DISCOUNT_AMOUNT,1,CHARINDEX('|',DISCOUNT_AMOUNT)-1) DISCOUNT_AMOUNT
FROM CTE1
)
SELECT *
FROM CTE2
SQL Fiddle Demo