June 23, 2023 at 5:18 am
I want to find the space between First Name and Last Name in SQL Server or First Name and Middle Name. I am aware of space function, but not sure if we can use it to find the space between two words. My table has around one million rows in the table. One of the columns is customerName. First Name, Middle Name and Last Name are stored as a combined name in the same row so for e.g.
Steven Ramirez is stored in the CustomerName column. Below is my table
CREATE TABLE [dbo].[CustomerData](
[ID] [int] IDENTITY(1,1) NOT NULL,
[CustomerName] [varchar](500) NULL
) ON [PRIMARY]
GO
These are the names in the table:
INSERT INTO [dbo].[CustomerData]
([CustomerName])
VALUES
('Steven Ramirez')
GO
INSERT INTO [dbo].[CustomerData]
([CustomerName])
VALUES
('Steven Middle Ramirez')
go
INSERT INTO [dbo].[CustomerData]
([CustomerName])
VALUES
('Steven Ramirez')
I want to find the space between First Name and Last Name and also First Name and Middle Name in SQL Server. So for all the rows, I should get
4
4
3
because there are 4 spaces between steven and Ramirez and there are 4 spaces between Steven and Middle and 3 spaces between steven and Ramirez
Steven Ramirez --4
Steven Middle Ramirez--4
Steven Ramirez--3
so I just want to return
4
4
3
any help will be appreciated.
June 23, 2023 at 1:28 pm
I'd use CHARINDEX. You can search for an expression, as simple as looking for the space, within the string and return the value where that space is.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
June 23, 2023 at 4:33 pm
--======================================================================================================
-- Create and populate the test table. This is not a part of the solution.
--======================================================================================================
--===== Create the test table.
DROP TABLE IF EXISTS #CustomerData;
CREATE TABLE #CustomerData
(
ID int IDENTITY(1,1) NOT NULL
,CustomerName varchar(500) NULL
)
;
--===== Populate the test table with spaced names.
INSERT INTO #CustomerData
(CustomerName)
VALUES ('Steven Ramirez') --4 Spaces
,('Steven Middle Ramirez') --4 Spaces from first to middle
,('Steven Ramirez') --3 Spaces
,('Steven Ramirez') --1 Space
,('StevenRamirez') --No Spaces
;
--======================================================================================================
-- Demonstrate a solution to how many spaces are in the first space block.
--======================================================================================================
SELECT cd.ID
,SpaceCount = PATINDEX('%[ ][^ ]%',fs.FirstSpaceStr)
FROM #CustomerData cd
CROSS APPLY (VALUES(SUBSTRING(cd.CustomerName,CHARINDEX(' ',cd.CustomerName),500)))fs(FirstSpaceStr)
ORDER BY cd.ID
;
Results:
--Jeff Moden
Change is inevitable... Change for the better is not.
June 24, 2023 at 2:32 am
I only have select permission on this table. This is a third party vendor database and I cannot update/delete/insert or create procedure/functions in this database. I am getting the data using select query
June 24, 2023 at 3:38 am
I only have select permission on this table. This is a third party vendor database and I cannot update/delete/insert or create procedure/functions in this database. I am getting the data using select query
Read the comments. The create table and insert are just to build the test data. The final select is all you need. Just change the table name from a temp table to your actual table.
--Jeff Moden
Change is inevitable... Change for the better is not.
July 1, 2023 at 3:40 am
Just curious... did it work for you after my clarification above?
--Jeff Moden
Change is inevitable... Change for the better is not.
July 2, 2023 at 11:43 pm
@Anjali66 ,
Just curious... did it work for you after my clarification above?
No answer... Ok then... good luck! 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
July 5, 2023 at 12:58 pm
In Microsoft SQL, you can find the spaces between the first and last name using various string manipulation functions. One approach is to use the CHARINDEX function, which returns the starting position of a specified substring within a string. By searching for the space character (' ') in the column containing the full name, you can identify the position of the space. For example, the query "SELECT CHARINDEX(' ', FullName) AS SpacePosition FROM TableName" will retrieve the position of the first space in the FullName column. This allows you to extract the first name and last name by utilizing string functions such as LEFT and RIGHT. Remember to adjust the query according to your table and column names.
July 5, 2023 at 7:00 pm
In Microsoft SQL, you can find the spaces between the first and last name using various string manipulation functions. One approach is to use the CHARINDEX function, which returns the starting position of a specified substring within a string. By searching for the space character (' ') in the column containing the full name, you can identify the position of the space. For example, the query "SELECT CHARINDEX(' ', FullName) AS SpacePosition FROM TableName" will retrieve the position of the first space in the FullName column. This allows you to extract the first name and last name by utilizing string functions such as LEFT and RIGHT. Remember to adjust the query according to your table and column names.
The OP provided test data. Please code your example and provide it. No fair copying mine, either. 😀
--Jeff Moden
Change is inevitable... Change for the better is not.
July 5, 2023 at 11:15 pm
Here is another way.
DROP TABLE IF EXISTS #CustomerData;
CREATE TABLE #CustomerData(
[ID] [int] IDENTITY(1,1) NOT NULL,
[CustomerName] [varchar](500) NULL
)
GO
INSERT INTO #CustomerData ([CustomerName]) VALUES ('Steven Ramirez');
INSERT INTO #CustomerData ([CustomerName]) VALUES ('Steven Middle Ramirez');
INSERT INTO #CustomerData ([CustomerName]) VALUES ('Steven Ramirez');
WITH cte (CustName, CountSpaces)
AS
(SELECT
Customername
,PATINDEX('%[ ][a-zA-Z]%', c.CustomerName) - PATINDEX('%[a-zA-z][ ]%', c.CustomerName) CountSpaces
FROM #CustomerData c)
SELECT
CustName, CountSpaces
FROM cte t
=======================================================================
July 6, 2023 at 4:24 am
Nice.... you don't even need the CTE with that method.
SELECT
Customername
,PATINDEX('%[ ][a-zA-Z]%', c.CustomerName) - PATINDEX('%[a-zA-z][ ]%', c.CustomerName) CountSpaces
FROM #CustomerData c
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply