Given Letter, Get Next Letter in Alphabet

后端 未结 5 540
天涯浪人
天涯浪人 2021-01-17 19:18

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?

5条回答
  •  萌比男神i
    2021-01-17 19:59

    Here is a CTE with Jonathan Wood's implementation

    ;WITH cte AS
       (SELECT  CHAR(ASCII('a')) [char], 1 [count]
        UNION ALL
        SELECT  CHAR(ASCII('a') + cte.count) [char], cte.count + 1 [count]
        FROM    cte)
    SELECT  TOP(26) cte.count[pos], cte.char
    FROM    cte
    

    you can either use it just like that or insert the results into a table variable or temporary table and use it on that.

    Another tip I would also give is, have a table in your database with this cte's data, then it is easier in the future to join to it and use it like that for whatever purpose or reason.

提交回复
热议问题