Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Substring How to combine first letter of every word in a column

Posted on 2006-06-08
1
Medium Priority
?
1,216 Views
Last Modified: 2008-01-09

 Hi,

    I wanted to know How can I combine the first letter of every word seperated by space in a column. I have got the query almost to work, For the second word it finds the result fine but for the third word it seems to go back to the first words first letter for ex:

SELECT     BANK_NAME, SUBSTRING(BANK_NAME, 1, 1) + SUBSTRING(BANK_NAME, CHARINDEX(' ', BANK_NAME) + 1, 1) + SUBSTRING(BANK_NAME,
                      CHARINDEX('  ', BANK_NAME) + 1, 1) AS Expr1
FROM         TBL_BANKS

Returns bob for

ABN Amro Bank         It should display AAB but it displays AAA
Alpha Credit Bank      It should display ACB but it displays ACA


I appreciate any one's help regarding the problem in advance.

0
Comment
Question by:m270350
1 Comment
 
LVL 32

Accepted Solution

by:
Brendt Hess earned 500 total points
ID: 16866664
The reason this fails is that the Charindex looking for the second space fails for two reasons.  

First, it is looking for two spaces.  When it fails to find this, it returns a zero, +1, and then takes the character there.
Second, if you use only one space, it finds the second word again.

To correctly find the third word, you must find the second word.  There are a couple of ways to do this:

1) Use REVERSE, as in this query:

DECLARE @BANK_NAME varchar(256)

SET @BANK_NAME = 'ALPHA BETA GAMMA'

SELECT     SUBSTRING(@BANK_NAME, 1, 1) +
    SUBSTRING(@BANK_NAME, CHARINDEX(' ', @BANK_NAME) + 1, 1) +
    Substring(Reverse(Substring(Reverse(@Bank_Name), 1, Charindex(' ', @Bank_Name)-1)), 1, 1)
    AS Expr1

This reverses the string, finds the first space, takes all of the letters up to (but not including) the space, uses REVERSE again, and takes the first character of that word.

2)  Chop off the first word, and use SUBSTRING again to get the third word:

DECLARE @BANK_NAME varchar(256)

SET @BANK_NAME = 'ALPHA BETA GAMMA'

SELECT     SUBSTRING(@BANK_NAME, 1, 1) +  -- first letter
     SUBSTRING(@BANK_NAME, CHARINDEX(' ', @BANK_NAME) + 1, 1) + -- second letter
          SUBSTRING(
               SUBSTRING(@BANK_NAME, CHARINDEX(' ', @BANK_NAME) + 1, Len(@Bank_Name)) -- get last two words of bank name
               , charindex(' ',
                    SUBSTRING(@BANK_NAME, CHARINDEX(' ', @BANK_NAME) + 1, Len(@Bank_Name)) ' get the position of the space
               ) + 1
          , 1
     )
AS Expr1

Clear now? ;-)
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question