August 29, 2012 at 7:24 am
I have letter "a", "b", "c". I would like my results to be "b", "c", "d" in TSQL respectively. Would what I use to achieve this?
August 29, 2012 at 7:39 am
Something like this?
select char(ASCII('a') +1)
August 29, 2012 at 7:43 am
What do you want when the letter is z?
select NextLetter = char(ascii('z')+1)
Results:
NextLetter
----------
{
(1 row(s) affected)
August 29, 2012 at 7:58 am
You could return a 'Z' or a NULL
SELECT NextLetter = CASE WHEN ascii(@Letter) IN (122,90)
THEN NULL --What would you return here?
ELSE char(ascii(@Letter)+1) END
August 29, 2012 at 8:28 am
Or you could start over at 'a'.
declare @MyChar char(1) = 'z'
select NextLetter = case @MyChar when 'z' then 'a' else char(ascii(@MyChar) + 1) end
Or you could add collation to make it case sensitive and start into the UPPER CASE letters.
Maybe the OP will post back with some requirements and we can answer the riddle for them. 😉
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply