Solved

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

Posted on 2006-06-08
1
1,185 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Database connection opened on a machine 8 57
Steps to produce a data strategy 5 58
Exchange 2010 - Best practice MDB Data size 8 91
Database needed for HR files 4 56
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
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…

839 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