Solved

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

Posted on 2006-06-08
1
1,190 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl Versus AWK? 7 67
Hive vs Impla in Hadoop 1 73
Populate a MS Access field based on contents of 2 other fields. 5 24
Database-Scoped Permissions 2 12
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
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…

740 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