473,388 Members | 1,230 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Finding a space in T-SQL


Hello,

If I have a string "Mr. Bill Gates" stored as an nvarchar variable how would I split it into two strings "Mr. Bill" and "Gates" using T-SQL? I assume I could use Substring but to use that I would have to know the index of the last whitespace and don't know how to get that index. Any suggestions would be appreciated. Thanks!
Nov 18 '05 #1
3 3889
Hi
Here is a quick fix. There could be a better way.

declare @v varchar(50)
select @v='Mr. Bill Gates'

select ltrim(reverse(substring(reverse(@v),1,charindex(' ',reverse(@v)))))
--
Ibrahim

"Solel Software" wrote:

Hello,

If I have a string "Mr. Bill Gates" stored as an nvarchar variable how would I split it into two strings "Mr. Bill" and "Gates" using T-SQL? I assume I could use Substring but to use that I would have to know the index of the last whitespace and don't know how to get that index. Any suggestions would be appreciated. Thanks!

Nov 18 '05 #2
"Solel Software" <So***********@newsgroup.nospam> wrote in message
news:55**********************************@microsof t.com...

Hello,

If I have a string "Mr. Bill Gates" stored as an nvarchar variable how

would I split it into two strings "Mr. Bill" and "Gates" using T-SQL? I
assume I could use Substring but to use that I would have to know the index
of the last whitespace and don't know how to get that index. Any
suggestions would be appreciated. Thanks!

DECLARE @strInput varchar(50)
SELECT @strInput = 'Mr. Bill Gates'

SELECT LEFT(@strInput, CHARINDEX(' ', @strInput) - 1) AS LeftSide
SELECT SUBSTRING(@strInput, CHARINDEX(' ', @strInput) + 1,
DATALENGTH(@strInput) - CHARINDEX(' ', @strInput)) AS RightSide
Nov 18 '05 #3

Thank you so much Ibrahim! This works great.
"Ibrahim Shameeque" wrote:
Hi
Here is a quick fix. There could be a better way.

declare @v varchar(50)
select @v='Mr. Bill Gates'

select ltrim(reverse(substring(reverse(@v),1,charindex(' ',reverse(@v)))))
--
Ibrahim

"Solel Software" wrote:

Hello,

If I have a string "Mr. Bill Gates" stored as an nvarchar variable how would I split it into two strings "Mr. Bill" and "Gates" using T-SQL? I assume I could use Substring but to use that I would have to know the index of the last whitespace and don't know how to get that index. Any suggestions would be appreciated. Thanks!

Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Noam Dekers | last post by:
Hi all, I would like to find a word stored in a text file. Structure: I have one file named keyWords.txt that stores some key words I'm interested in finding. In addition I also have a file...
4
by: lecichy | last post by:
Hello Heres the situation: I got a file with lines like: name:second_name:somenumber:otherinfo etc with different values between colons ( just like passwd file) What I want is to extract...
1
by: lawrence | last post by:
Is this the correct way to find a string of characters that goes 40 spaces without any white space? {40} Would this find a stretch with no white space, newlines, or tabs? {40}
5
by: lawrence | last post by:
"Garp" <garp7@no7.blueyonder.co.uk> wrote in message news:<_vpuc.1424$j_3.13346038@news-text.cableinet.net>... > "lawrence" <lkrubner@geocities.com> wrote in message >...
5
by: sdowney717 | last post by:
Is there a way to construct a query to select for whole words only? select id from bookdata where titles like '%Test%' gets everything with test somewhere in the field. So you could get records...
1
by: Luc Le Blanc | last post by:
Under UDB 7.2 for OS/2, how can I determine the % of used space in a DMS container (preferably via the CLP interface, so I can script the command with REXX)? At what level should I go about adding...
10
by: Dixie | last post by:
I need to delete some relationships in code. How do I know what the names of those relationships are?
1
by: Peter Rilling | last post by:
I am trying to write a regular expression that locates href attributes in some html content. I used the example in the .NET documentation as a starting point...
0
by: eric.goforth | last post by:
Hello, I'm trying to find the available disk space on a folder using VB.NET. I tried using: Dim Info As New System.IO.DriveInfo(sAttachmentDir) Console.WriteLine("Total disk space " &...
4
by: mattG | last post by:
I have a scenario where I have these things that take up (x) number of space. I know the max number of space I will ever have is 512. I am trying to figure out a way to write a formula or...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.