我有字母“a”,“b”,“c”。我希望我的结果分别是TSQL中的“b”,“c”,“d”。我会用什么来实现这个目标?
答案 0 :(得分:12)
使用ASCII
获取角色的值,添加一个,然后使用CHAR
将值转换回角色。
SELECT CHAR(ASCII('a') + 1)
答案 1 :(得分:11)
这就是你如何用一个字母
DECLARE @myletter char(1) = 'a';
SELECT CHAR(ASCII(@myletter)+1);
答案 2 :(得分:1)
以下是Jonathan Wood实施的CTE
;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
你可以像这样使用它,或者将结果插入表变量或临时表中并在其上使用它。
我还要提出的另一个提示是,在数据库中有一个包含此cte数据的表,然后将来更容易加入并以任何目的或原因使用它。
答案 3 :(得分:1)
3个字母alfabetic计数器,带有两个SQL程序
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
alter procedure letraMas
@letraEntra as char( 1),
@letraSale as char( 1) OUTPUT,
@seLleva as bit OUTPUT
as
--set @letraEntra = 'w'
--set @letraSale = 'm'
set @seLleva = 0
select
@letraSale = CASE
WHEN @letraEntra = '' or (@letraEntra is null) or @letraEntra = 'z'
THEN 'a'
WHEN @letraEntra < 'z'
THEN CHAR (ASCII( @letraEntra) + 1)
end
if @letraEntra = 'z' set @seLleva = 1
return
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/* USO:
declare
@tareaEntra as char(3),
@tareaSale as char(3)
set @tareaEntra = 'xzz'
EXEC tareaMas @tareaEntra, @tareaSale OUTPUT
*/
alter procedure tareaMas
@tareaEntra as char( 3),
@tareaSale as char( 3) OUTPUT
as
declare @charU as char(1 ) -- char de U_nidades albabéticas
declare @charD as char(1 ) -- char de D_ecenas albabéticas
declare @charC as char(1 ) -- char de C_entenas albabéticas
declare @letraSale as char(1 ) -- char de C_entenas albabéticas
declare @seLleva as bit
set @tareaEntra = right(' ' + rtrim (@tareaEntra), 3)
set @charU = substring(@tareaEntra , 3, 1)
set @charD = substring(@tareaEntra , 2, 1)
set @charC = substring(@tareaEntra , 1, 1)
EXEC letraMas @charU, @letraSale OUTPUT, @seLleva OUTPUT
set @charU = @letraSale
if @seLleva = 1
BEGIN
EXEC letraMas @charD, @letraSale OUTPUT, @seLleva OUTPUT
set @charD = @letraSale
if @seLleva = 1
BEGIN
EXEC letraMas @charC, @letraSale OUTPUT, @seLleva OUTPUT
set @charC = @letraSale
END
END
set @tareaSale = ltrim(@charC + @charD + @charU)
return
答案 4 :(得分:0)
DECLARE @Letters AS TABLE
(
Letter CHAR(1)
)
INSERT INTO @Letters
( Letter )
VALUES ( 'A' )
INSERT INTO @Letters
( Letter )
VALUES ( 'B' )
INSERT INTO @Letters
( Letter )
VALUES ( 'C' )
SELECT CHAR(ASCII(Letter) + 1) FROM @Letters
在SQL中使用表变量的示例,但上述任何一个都将为您完成。取决于你用什么方法来整理字母列表。