I am needing to sort a field on everything after a space usig SQL. In the example below, I would like it to sort (ascending) beginning with the last name.
USA-J. Doe
USA-M. Mouse
USA-A. Mouse
USA-D. Duck
USA-P. Panther
USA-T. Bird
I need it to sort the entire string, but on the last name. If there are two last names that are identical, I would like for it to take the initial of the first name into account. The result would be:
USA-T. Bird
USA-J. Doe
USA-D. Duck
USA-A. Mouse
USA-M. Mouse
USA-P. Panther
I will need to use this code in both SQL Server and MS Access.
I hope that someone can fully answer this question. For whatever reason, someone has scored me a -1 on this question. I cannot figure out why. I have been as specific as I know to be and I wasn't able to find an answer to the final piece--sorting by first letter if the last name is the same.
Thank you guys for responding. The information helped. I had to add brackets around "name" because the name of the field was similar to the name of the actual table.
This depends on the database. The following is how you might do this in SQL Server:
order by substring(name, charindex(' ', name) + 1, len(name)))
Similar logic works in other databases but the functions are different.
For instance, in Oracle:
order by substr(name, instr(name, ' ') + 1)
And, in MySQL, you could use similar logic, but this is simpler:
order by substring_index(name, ' ', -1)
And in MS Access:
order by mid(name, instr(name, ' ') + 1)
For sorting by lasname, firstname you need to use like this in SQL server:
order by substring(name, charindex(' ', name) + 1, len(name))), substring(name, charindex('-', name) + 1, 1))
In Access
order by mid(name, instr(name, ' ') + 1), order by mid(name, instr(name, '-') + 1, 1)
Related
SQL: What are phone number formatting options?
I query my SQL Database for a phone number:
816-123-4567
I want to output it is as:
816 123-4567.
What steps do I take to achieve this result in an SQL environment?
A standard SQL solution would be:
select substring(phone, 1, 3) || ' ' || substring(phone, 5, 8)
There may be simpler solutions in other databases. For instance, in SQL Server:
select stuff(phone, 4, 1, ' ')
And in MySQL:
select insert(phone, 4, 1, ' ')
Note: These are specific the the format you have provided in your question. If you have other formats, then you might need more complicated logic.
Use SUBSTRING and CHARINDEX functions.
SUBSTRING gets a part of the string according to given indexes.
CHARINDEX gets the index of the character that triggers your String separation.
Below is an MSSQL Server query.
According to your DBMS, the function names can vary, but the logic will is the same.
Query :
SELECT
SUBSTRING('816-123-4567', 0, CHARINDEX('-', '816-123-4567') ) +
' ' +
SUBSTRING('816-123-4567', CHARINDEX('-', '816-123-4567') + 1 , LEN('816-123-4567') -
CHARINDEX('-', '816-123-4567') )
And the result :
816 123-4567
When we put your field and table name instead of static values :
SELECT
SUBSTRING(YourPhoneField, 0, CHARINDEX('-', YourPhoneField) ) +
' ' +
SUBSTRING(YourPhoneField, CHARINDEX('-', YourPhoneField) + 1 , LEN(YourPhoneField) -
CHARINDEX('-', YourPhoneField) )
FROM YourTableName
I am having trouble with my SQL Query to return all entries in a column which have occurred more than twice.
I have looked at other StackOverflow answers for trying to do this, and whenever I apply the same query to mine it just returns entries where the first character in the entry is the same.
For example, there are around 6 entries in the database which start off with a '-' and it returns all of them even though they are not identical matches.
Would someone be able to tell me where my query is going wrong?
I would have thought that looking for duplicates would be a standard procedure.
Here is the query I am using:
SELECT name FROM subs GROUP BY name HAVING (COUNT(name) > 1);
Here is a sample of the output:
-Johnny-
-Lady_Gaga-
-Randy_Marsh-
AJWesty
All_CAPS
NB: I am looking to find all the usernames in the database that occur more than once.
Thanks in advance!
You could try stripping the dashes off the names column. Here is one way to do that:
SELECT REPLACE(name, '-', '')
FROM subs
GROUP BY REPLACE(name, '-', '')
HAVING (COUNT(name) > 1);
But this approach runs the risk of removing dashes occurring inside a name. To avoid this, we need to do more work:
SELECT
CASE WHEN SUBSTR(name, 1, 1) = '-' AND SUBSTR(name, LENGTH(name), 1) = '-'
THEN SUBSTR(name, 2, LENGTH(name)-2)
ELSE name END AS name
FROM subs
GROUP BY
CASE WHEN SUBSTR(name, 1, 1) = '-' AND SUBSTR(name, LENGTH(name), 1) = '-'
THEN SUBSTR(name, 2, LENGTH(name)-2)
ELSE name END
HAVING COUNT(*) > 1;
In Oracle, how do i sort a value for eg: Name in SQL using its second word. Looks a bit tricky to me. For eg: If in a table i have the names as below:
I have a list of objects which i want to sort on basis of name. I have done coding where it does get sorted on basis of name but i have a slight different requirement.
The names are for Example:
Bull AMP
Cat DEF
Dog AMP
Frog STR
Zebra DEF
I want the list to be :
Bull AMP
Dog AMP
Cat DEF
Zebra DEF
Frog STR
To be sorted by second word.
I tried the below query but it didnt seem to work.
SELECT NAME,
SUBSTR(NAME, INSTR(' ',NAME),
LENGTH(NAME) - INSTR(' ',NAME) +2) AS word2
FROM animal_master
ORDER BY SUBSTR(NAME,INSTR(' ',NAME), LENGTH(NAME) - INSTR(' ',NAME) +2) asc;
Can anyone please guide whats wrong.
Your INSTR function had back to front arguments, so that is why you were gettting the incorrect results. I would recommend using the following:
SELECT NAME
FROM animal_master
ORDER BY SUBSTR(NAME,INSTR(NAME, ' ')) asc;
The SUBSTR(NAME,INSTR(NAME, ' ')) returns only the second word, and you order by this second word. If you also want to order by the first, then the second word you can do something like this:
ORDER BY SUBSTR(NAME,INSTR(NAME, ' ')), NAME
Clean up all that nested instr/substr stuff with regexp_replace:
order by regexp_replace(NAME, '.* (\w)', '\1'), NAME;
The regex matches and remembers the last "word" after the space, then orders by that first.
Try the following.
SELECT NAME
FROM animal_master
ORDER BY TRIM(SUBSTR(NAME, (INSTR(NAME, ' ', 1, 1) + 1), (INSTR(NAME, ' ', 1, 2) - INSTR(NAME, ' ', 1, 1) - 1))) asc;
Below query explains better..
SELECT NAME
FROM (
SELECT
NAME,
(INSTR(NAME, ' ', 1, 1) + 1) first_occurrence_of_space,
(INSTR(NAME, ' ', 1, 2) - INSTR(NAME, ' ', 1, 1) - 1) second_occurrence_of_space
FROM animal_master)
ORDER BY TRIM(SUBSTR(NAME, first_occurrence_of_space, second_occurrence_of_space)) asc;
select display_name from employee order by substr(upper(display_name),instr(upper(display_name),' ',1));
Below is my query. It is giving me correct output but I need to run it efficiently as it is used for 500k records.
DECLARE #DESC_MESSAGE VARCHAR(5000)
SET #DESC_MESSAGE = '12345 VENKAT was entered ODC ABCD-3'
SELECT REPLACE(#DESC_MESSAGE,SUBSTRING(#DESC_MESSAGE,1,CHARINDEX('was',#DESC_MESSAGE,3)-1),'')
I just want to retrieve text after 'was' which can change depending on condition.
for ex. text can be like
'112233 XYZ was entered ODC PQRS-3' or
'223344 HARRY was gone out of ODC AMD-3'
Please suggest efficient way to retrieve such text.
I would be inclined to use stuff():
select stuff(col, 1, chardindex('was ', col + 'was ') + 4, '')
The + 'was + in the charindex() function just guarantees no error if 'was ' is not in the text.
half milion rows is not so huge..
what i can see in your question is that there is an architecture issue,
why do you need to split a column to make a query?
why don't you keep the colums splitted in origin ?
eventually you could have another column that contains only the text after the "was"
this could be better even if the rows grow a lot.
select LTRIM(stuff(#DESC_MESSAGE, 1, CHARINDEX(' was', #DESC_MESSAGE + 'was') + 3, ''))
I am writing a small query to extract all last names from a bunch of Author name database. Names on file will contain first and middle name, or just first name.
Ex: John Smith
John T. Smith
So I cannot search purely by just after the first space... But I know for sure that the lastname should be from the END to the first space from the right side of the string. I don't really care about first name.
This is what I currently have...
select [name], LEFT([name], CHARINDEX(' ', [name] + ' ')-1) as firstName,
SUBSTRING([name], charindex(' ', [name]+' ') + 1, LEN([name])) as lastName
from Author
;
I am quite new to sql, any help is highly appreciated!
Thanks in advance!
EDIT: for those who ever come across this need for help, this line helps:
select substr(t.string,instr(t.string,' ',-1)+1) last_word
For Oracle DB try this :
WITH t AS
(SELECT name AS l FROM <your query>
)
SELECT SUBSTR(l,instr(l,' ',-1)+1,LENGTH(l)) FROM t;
SUBSTRING_INDEX() should work in this case.
select name, SUBSTRING_INDEX(name,' ',-1) as lastName from Author;