Solved

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

Posted on 2006-06-08
1
1,161 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:
bhess1 earned 125 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Read about achieving the basic levels of HRIS security in the workplace.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now