Sql Server upper lower and index logic - sql

I have FirstName and LastName columns in sql..
I want to make First Name's first letter big and the rests low
same LastName's first letter big and rests low
For example : we have FisrtName and LastName is john sinatra name
mine must be John Sinatra.
how T-SQL code should be ?

Try this:
SELECT
UPPER(LEFT(FirstName, 1)) + LOWER(RIGHT(FirstName, LEN(FirstName) - 1)) AS FirstName,
UPPER(LEFT(LastName, 1)) + LOWER(RIGHT(LastName, LEN(LastName) - 1)) AS LastName
FROM MyTable

Related

Extract first and last name from email address. Microsoft SQL Server

I am really new to the SQL environment.
I am now facing a problem that I can't get solved.
I have a field email in which are the email addresses with the following format firstname.lastname#company.com
I now need a select that splits the email into first name, last name so like this.
marc.mueller#blablabla.com
to
Firstname Lastname
marc mueller
I was able to filter out the first name but the last name does not work.
select email,
LEFT(email,CHARINDEX('.', email)-1) AS [New Firstname]
from data
Can someone pls point me in the right direction?
many greetings
Maybe you can try substring function according to the index of "." and "#"
SELECT
SUBSTRING(email,0,charindex('.',email)) as firstname,
SUBSTRING(email,charindex('.',email)+1,charindex('#',email)-charindex('.',email)-1) as lastname FROM data
Firstly, we split the email address according to the "." character and we got the firstname.
Secondly, we split the email address starting from the first "." character to the "#" character.
Remember substring syntax;
SUBSTRING(string, start, length)
So to find the length of the lastname sub string, we should subtract index of "#" from "."
Assuming we always have a single . separating first and last name, and will not appear anywhere else in the value, and also will only have a single # separating name and domain:
select left(left(email, CHARINDEX('#', email) - 1), CHARINDEX('.', email) - 1) first_name
, right(left(email, CHARINDEX('#', email) - 1), len(left(email, CHARINDEX('#', email) - 1)) - CHARINDEX('.', email)) last_name
from tbl
Edit:
Here is a case statement to handle a missing . in the name:
select left(left(email, CHARINDEX('#', email) - 1), CHARINDEX('.', email) - 1) first_name
, case when email like '%.%#%' then right(left(email, CHARINDEX('#', email) - 1), len(left(email, CHARINDEX('#', email) - 1)) - CHARINDEX('.', email)) else left(left(email, CHARINDEX('#', email) - 1), CHARINDEX('.', email) - 1) end last_name
from tbl
However, we have no way of knowing if it is the first or last name, so this will return the name in both columns.
And another alternative using parsename. CTEs are used to help "see" the logic flow but it can all be converted into a more compact form.
declare #x table (email varchar(200));
insert #x (email) values ('marc.mueller#blablabla.com'), ('lastname#company.com')
;
with cte_replace as (select replace(email, '#', '.') as p1
from #x
),
cte_split as (select parsename(p1, 4) as p2, parsename(p1, 3) as p3
from cte_replace
)
select * from cte_split;
Notice how the logic handles a name without a period to separate first/last parts. An alternative to the REPLACE usage is to simple truncate from the # sign.

How to parse a row in SQL into columns (separator-spaces)

I need to parse row (with spaces) into two different columns in SQL. The rows look like that:
FullName
John Mayer
Rajon Jennings
Donna Fraye
Michael Moore
So the expected result is :
Name
John
Rajon
Donna
Michael
Surname
Mayer
Jennings
Fraye
Moore
How can i do that in SQL?
If you have a requirement like First Name should be string before first space and rest everything what follows should go as Last name , you can update two columns as:
Update T
Set T.FirstName = Left(ltrim(rtrim(FullName)), CHARINDEX(' ',ltrim(rtrim(FullName))))
,T.LastName = Right(ltrim(rtrim(FullName)),len(FullName)- CHARINDEX(' ',ltrim(rtrim(FullName))))
From Test T
Check Demo here..
You can use a split function.This function must be created.
Here are some examples :
http://ole.michelsen.dk/blog/split-string-to-table-using-transact-sql/
Split function by comma in SQL Server 2008
If there is only ever 1 space / 2 name and you don't have to cater for names like Vincent van Gough, then a combination of substring and charindex should do.
select substring(fullname, 1, charindex(' ', fullname)) as name,
substring(fullname, charindex(' ', fullname)+1, len(fullname)-charindex(' ', fullname)) as surname
from tbl
This does not cover people with the same first and last name (ie. john john) but will work for rest. Not recommended but is a different solution.
select left(fullname, charindex(' ',fullname)) Name,
replace(fullname,left(fullname, charindex(' ',fullname)),'') Surname
from names
Fiddle

how to split a column into multiple value using sql

If I have thousands of rows with data and I would like to find out if the column called "Last
Name " contains both First Name and Last Name (could be middle initial too). What SQL command
do I use ? (SQL Server 2008). Once I find it, how do I split the Last Name field and place
first name or middle initial in their own columns ?
For example:
First Name MI Last Name
John B. Smith
Karen A. Jones
Jane Lawrence
Joan K. Bates
Could it be done in Excel as well ?
You could look for spaces in the last name:
select *
from t
where lastname like '% %';
You could look for rows where the first name is missing:
select *
from t
where firstname is null or firstname = '';
EDIT:
If we assume that the names are "simple" as given in the sample data, you can do:
update t
set firstname = left(lastname, charindex(' ', lastname) - 1),
lastname = right(lastname, charindex(' ', reverse(lastname))),
mi = (case when lastname like '% % %'
then rtrim(ltrim(substring(lastname, charindex(' ', lastname + 1), 2)))
end)
where lastname like '% %' and firstname is null;
This makes lots of assumptions about the formats . . . that there are no complete middle names, that there are no honorics, that there are no suffixes, that there are no multiple spaces together. But if your data is simple, it should work.
Here is an example of the logic in SQL Fiddle.
I would do it in two passes:
-- Set first name
UPDATE names
SET FirstName = SUBSTR(LastName,1,CHARINDEX(' ',LastName) - 1),
LastName = SUBSTR(LastName,CHARINDEX(' ',LastName) + 1, LEN(LastName))
WHERE FirstNAme IS NULL
AND CHARINDEX(' ',LastName) > 0
-- Set middle name
UPDATE names
SET MI = SUBSTR(LastName,1,CHARINDEX(' ',LastName) - 1),
LastName = SUBSTR(LastName,CHARINDEX(' ',LastName) + 1, LEN(LastName))
WHERE MI IS NULL
AND CHARINDEX(' ',LastName) > 0
You may have a few false positives but that should get the vast majority of cases.
I would STRONGLY recommend creating a transaction, doing the updates, doing a SELECT to see the results and rolling back the transaction since the update is not reversible.

How do I trim out middle initial from a person's full name in SQL?

I'm look for the simplest way to split up first and last name wile trimming out the middle initial. The current layout of the field is [Last Name], [First Name] [MI]. Also, middle initial is not always there. My current code is below, I'm just not sure how to trim out the middle initial from first name without writing a case statement.
SELECT SUBSTRING(h.Name, CHARINDEX(',', h.Name, 0) + 2, LEN(h.Name) - CHARINDEX(',', h.Name, 0)), 0 as FirstName
,SUBSTRING(h.Name, 0, CHARINDEX(',', h.Name, 0)) as LastName
FROM Members
I have made some assumptions below:
1 - First names are always longer than one character.
2 - Middle inital will always be preceded by a space.
3 - The data is trimmed.
This code will return NULL if any of the above are not true. If your data is not trimmed, you can use RTRIM on all instances of #n below to mitigate.
declare #n as varchar(50)
set #n = 'Smith, John A'
select #n,
case
when SUBSTRING(#n, LEN(#n) - 1, 1) = ' '
then SUBSTRING(#n, LEN(#n), 1)
end
What are the business rules of this system? Will it always be:
last name , first name space a possible middle initial
What other permutations can exist?
Will it always be space letter . ? Because then you could always take the right three characters, look for a space and a period, then remove the set of three.
select REPLACE(firstName+ISNULL(middleName+' ','')+ISNULL( lastName +' ',''),' ',' ') as 'name' from Contacts

SQL Like query last match

I've a database that has a name field. (i.e Firstname M. Lastname or just Firstname Lastname).
Trying to filter by lastname.
How can I do a query to find the last space?
Something like
select * from person where name like "% a%" (but the space is the last space)
Thanks,
Tee
If using some version of Microsoft SQL Server, you could reverse() the string, and then use charindex() to find the first space.
If this is mySQL, you can consider using the SUBSTRING_INDEX() function (with count = -1).
SELECT CASE WHEN [Name] LIKE '% [^ ]%'
THEN SUBSTRING([Name],
LEN([Name]) - CHARINDEX(' ', REVERSE([Name])) + 1,
8000)
ELSE [Name]
END AS [LastName]
FROM [Customers]