Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-06-08
1
Medium Priority
?
1,207 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:
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

Technology Partners: 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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

704 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