How to add a title and substring - sql

I need to get a list of names as per the following format
"Mr."+first name initial+last name+"."
There is only one table for this
salesperson (f_name, l_name)
What i have been trying is;
SELECT 'Mr.' ||' ' || SUBSTRING(f_name,1,1) || ' ' || l_name ||’.’||
FROM salesperson;
It works without the substring or left, but not if I include them.

Use concat instead of || operator to concatenate strings in MySQL. As you have it, it would be interpreted as logical OR condition, hence you get the error.
SELECT CONCAT('Mr.',' ',SUBSTRING(f_name,1,1),' ',l_name,'.')
FROM salesperson;
Oracle solution
SELECT 'Mr.'||' '||SUBSTR(f_name,1,1)||' '||l_name||'.'
FROM salesperson;

It is better practice anyways to grab the name data in full and then format it in the view part of your application with languages that are more suited to string manipulation. This also makes your code more reusable.
That being said use this
SELECT CONCAT("Mr. ",SUBSTRING( f_name, 1, 1 ) ," ",l_name,".") FROM salesperson

Related

SQL SELECT WHERE without underscore and more

I want to select where 2 strings but without taking
underscore
apostrophe
dash..
Hello !
I want to select an option in my SQL database who look like this :
Chef d'équipe aménagement-finitions
With an original tag who look like this
chef-déquipe-aménagement-finitions
Some results in database had a - too
SELECT *
FROM table
WHERE REPLACE(name, '-', ' ') = REPLACE('chef-déquipe-aménagement-finitions', '-', ' ')
didnt work because of missing '
And a double replace didn't work too.
I want the string be able to compare without taking
underscore
apostrophe
dash
and all things like that
is this possible ?
Thanks for your help
Have good day !
Depends on your rdbms, but here's how I would perform in MySQL 8. If using a different version or rdbms, then first determine how to escape the single quote and modify as needed.
with my_data as (
select 'Chef d''équipe aménagement-finitions' as name
)
select name,
lower(replace(replace(name, '\'', ''), ' ', '-')) as name2
from my_data;
name
name2
Chef d'équipe aménagement-finitions
chef-déquipe-aménagement-finitions
Sql-server and Postgres version:
lower(replace(replace(name, '''', ''), ' ', '-')) as name
After posting, this, I re-read and noticed you are also looking to replace other characters. You could either keep layering the replace function, or, look into other functions.

SQL: count occurrences for each row

I have an SQL table called Codes with a primary column code of type String.
I also have another table called Items with a column codestring also of type String. This entry always contains a string with some of the codes of the above table separated by spaces.
Now I want to get all codes and their number of Items containing the respective code. Can I do that?
Codes:
code|...
----|---
"A0A"|
"A0B"|
...|
Items:
...|codestring
----|---------
|"A0A C2B F1K"
|"A0C D2S H3K"
|...
Output:
Codes:
code|...|noOfItems
----|---|---------
"A0A"|...|5
"A0B"|...|10
...|...|...
Assuming all the codes are distinct (or at least no code is a substring of another code), you can use the LIKE operator:
(untested)
SELECT codes.code, count(*)
FROM codes LEFT JOIN items ON items.codestring LIKE '%' + codes.code + '%'
GROUP BY codes.code;
You have a horrible data format and should fix it. The mapping to codes should have a separate row for each code.
If the codes are all three characters, then L Scott Johnson's answer is close enough. Otherwise, you should take the delimiter into consideration:
SELECT c.code, count(i.codestring)
FROM codes c LEFT JOIN
items i
ON ' ' || i.codestring || ' ' LIKE '% ' || c.code || ' %'
GROUP BY c.code;
Note other fixes to the code:
The concatenation operator is the standard ||.
The count() will return 0 if there are no matches.

How to put together data from different tables without duplicating

Im feeling a bit stupid now but I cant seem to make it happen. I have som tables with data and the problem I have with one SELECT is that the data is sometimes duplicated. (Sorry, English is not my first language, ask if unclear.
SELECT (IM_FAKTUROR.FAKT_NUMMER || ' ' || IM_FAKTURA_GRUPPER.FAKT_TYP) AS 'ProjektNrNamn',
But sometimes those two tables/columns have exactly the same data and in those cases I only want the data from one of them, not both. How to?
If there is different data in the two I want all info.
Use a case expression. If the two columns have the same value, just return one of them. Else return both of them:
SELECT case when IM_FAKTUROR.FAKT_NUMMER = IM_FAKTURA_GRUPPER.FAKT_TYP
then IM_FAKTUROR.FAKT_NUMMER
else (IM_FAKTUROR.FAKT_NUMMER || ' ' || IM_FAKTURA_GRUPPER.FAKT_TYP)
end AS 'ProjektNrNamn',
Try this:
SELECT DISTINCT ProjektNrNamn FROM
(
SELECT (IM_FAKTUROR.FAKT_NUMMER || ' ' || IM_FAKTURA_GRUPPER.FAKT_TYP) AS 'ProjektNrNamn'
) as t
Try SELECT DISTINCT:
SELECT DISTINCT
IM_FAKTUROR.FAKT_NUMMER || ' ' || IM_FAKTURA_GRUPPER.FAKT_TYP AS 'ProjektNrNamn'
FROM ...
But this answer assumes that the project name is the only thing in your select list. If you have other columns, it gets more complicated.

Changing LastName,FirstName to LastName,FirstInitial

I'm sure this is super easy, but how would I go about converting LastName,FirstName to LastName,FirstInitial?
For example changing Smith,John to Smith,J or Johnson,John to Johnson,J etc.
Thank You!
In case of LastName and FirstName columns:
select LastName,substr(FirstName,1,1)
from mytable
;
In case of a fullname saved in a single column:
select substr(fullname,1,instr(fullname || ',',',')-1) || substr(fullname,instr(fullname || ',',','),2)
from mytable
;
or
select regexp_replace (fullname,'([^,]*,?)(.).*','\1\2')
from mytable
;
Here is one way, using just "standard" instr and substr. Assuming your input is a single string in the format 'Smith,John':
select substr(fullname, 1, instr(fullname, ',')+1) from yourtable;
yourtable is the name of the table, and fullname is the name of the column.
instr(fullname, ',') finds the position of the comma within the input string (it would be 6 in 'Smith,John'); thensubstrtakes the substring that begins at the first position (the1in the function call) and ends at the position calculated byinstr`, PLUS 1 (to get the first initial as well).

SQL limiting Count function based on IN clause using comma delimited list

I have a table of Matricies (JobMatricies) ID, Desc, DeptIDs
1 Admin (PM) 6,7,138,131,11,9,10,134,135,14,105,129
5 Sales Processing (PM) 92,16,153,17,91,32,26,93,99,18,89,90,155,19
6 Construction Processing (PM) 100,36,20,136,22,88,23,25,34,106,38,39,132,41,42,43,154,152,84
DeptIDs are a Comma Delimited list of departments that I want to use to count how many records are represented by the Matrix.
Normally I would do something like....
select Matrix_ID,
Matrix_Desc,
JobCount = (select count(sched_ID) from JobSchedule where dept_ID in (**92,16,153,17,91,32,26,93,99,18,89,90,155,19**))
from jobMatrices
How do I replace the hard coded delimited string with the ID's stored with each matrix, so that I can produce a list of matricies with their own unique count based on the comma delimited string that is stored with each matrix.
Thanks
I just answered a similar question where the poster wanted to sum the delimited list of numbers in a similar table layout. My solution used CTE's in Oracle to turn that list into a CTE table which would allow you to join against it. I believe that technique would be of use here if your RDBMS supports that. Please have a look: https://stackoverflow.com/a/38231838/2543416
select s.ID
, m."Desc"
from JobSchedule s
, jobMatrices m
where position(',' || s.ID || ',' in ',' || m.DeptIDs || ',')>0;
select m."Desc"
, count(s.ID) JobCount
from JobSchedule s
, jobMatrices m
where position(',' || s.ID || ',' in ',' || m.DeptIDs || ',')>0
group by m."Desc";
In MySQL, you can treat a comma-separated string as a SET, and there's a builtin function FIND_IN_SET() that helps:
select m.Matrix_ID,
m.Matrix_Desc,
(select count(sched_ID) from JobSchedule
where FIND_IN_SET(dept_ID, m.DeptIds)) AS JobCount
from jobMatrices AS m;
This will have terrible performance, however.
If you use some RDBMS other than MySQL, they may have a different solution that works similarly.
You should be specific in your question and tag your question appropriately. You only tagged your question sql, but this is a language used by many RDBMS vendors.