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
Solved

Add comma after every letter in a string

Posted on 2011-09-29
5
196 Views
Last Modified: 2012-05-12
Hello All,

Using SQL2008, I have a parameter passed to a stored procedure.  The parameter passed over is 'Austin, Dallas, Chicago, New York' (just an example, and in our data no need to worry that the last letter would repeat)

I need to use the last character in each word in the where clause of a select statement within the stored procedure.

I've got a way to get the last letter (below, but could probably be simplified)

DECLARE @STRING VARCHAR(8000);
SET @STRING = 'Austin, Dallas, Chicago, New York' ;

declare @string2 varchar (200)
SET @string2 = (SELECT (SELECT right(Items,1)
          FROM crp_ReportsReferenceTables.dbo.split2(@String, ',')
           FOR XML PATH(''),TYPE).value('.','varchar(max)'))
This returns: 'NSOK'

BUT...How do I get my @string2 to be:  'N', 'S', 'O', 'K' so that I can put that in my where clause?
ie select * from table where [lastcharcity] in (@string2)

Thank you in advance...
0
Comment
Question by:angelnjj
  • 3
5 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36817921
Hi. Are you actually wanting this for use in an IN statement or something? If so, then drop the FOR XML portion as you are already using a split() function to get each word on a row. RIGHT(items, 1) will then work. If you do need this together, but comma-delimited then amend the FOR XML to this:

STUFF((SELECT ', ' + RIGHT(Items, 1) AS "text()"
FROM crp_ReportsReferenceTables.dbo.split2(@String, ',')
FOR XML PATH('')), 1, 2, '')

;)
0
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
ID: 36817924
try:

right(Items,1)
-->
'''' + right(Items,1) + ''', '
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 36817933
it should give you

 'N', 'S', 'O', 'K',
0
 
LVL 51

Assisted Solution

by:HainKurt
HainKurt earned 500 total points
ID: 36817955
then use

set @string2 = left(@string2, len(@string2)-1)

example:

declare @string2 varchar (200) = '''N'', ''S'', ''O'', ''K'','
select @string2
set @string2 = left(@string2, len(@string2)-1)
select @string2
0
 
LVL 1

Author Closing Comment

by:angelnjj
ID: 36818015
The other solution, stripped off the last letter if another city was added (just in case)...the one chosen didn't care how many cities were passed.

Thank you! Thank you!
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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