通过SQL创建一个字母表列表

时间:2014-01-30 12:55:59

标签: sql sql-server

我想通过SQL生成字母表的结果吗?

这样的事情:

A
B
C
D
E
F

我试过了:

SELECT 
'A','B','C'

但这只是在列中生成字母。

9 个答案:

答案 0 :(得分:6)

使用表spt_values并将值转换为字符

SELECT Char(number+65) 
  FROM master.dbo.spt_values
 WHERE name IS NULL AND 
       number < 26

编辑:此表未记录。但是,它被许多系统存储过程使用,并且该表极不可能消失,因为所有这些过程都应该被重写。这就像戳一只沉睡的狮子。

答案 1 :(得分:3)

--
-- tally = 9466 rows in my db, select upper & lower alphas
-- 
;
with 
cte_tally as
(
select row_number() over (order by (select 1)) as n 
from sys.all_columns
)
select 
  char(n) as alpha
from 
  cte_tally
where
  (n > 64 and n < 91) or
  (n > 96 and n < 123);
go

sys.all_columns是一个记录表。它会存在一段时间。

http://technet.microsoft.com/en-us/library/ms177522.aspx

似乎很清楚,表sp_values没有文档,将来可以在没有Microsoft发表任何评论的情况下删除。

答案 2 :(得分:2)

尝试:

select 'A' union
select 'B' union
select 'C'

答案 3 :(得分:1)

如果要从A打印到Z,则:

DECLARE @i int=65
WHILE @i < 91
BEGIN
PRINT CHAR(@i);
SET @i=@i+1;
END

答案 4 :(得分:1)

您可以使用U-SQL

2019-01-21T09:04:58.949152Z info    Version root@6f6ea1061f2b-docker.io/istio-1.0.5-c1707e45e71c75d74bf3a5dec8c7086f32f32fad-Clean
2019-01-21T09:04:58.949283Z info    Proxy role: model.Proxy{ClusterID:"", Type:"sidecar", IPAddress:"10.20.228.89", ID:"productpage-v1-54b8b9f55-jpz8g.default", Domain:"default.svc.cluster.local", Metadata:map[string]string(nil)}
2019-01-21T09:04:58.949971Z info    Effective config: binaryPath: /usr/local/bin/envoy
configPath: /etc/istio/proxy
connectTimeout: 10s
discoveryAddress: istio-pilot.istio-system:15007
discoveryRefreshDelay: 1s
drainDuration: 45s
parentShutdownDuration: 60s
proxyAdminPort: 15000
serviceCluster: productpage
zipkinAddress: zipkin.istio-system:9411

答案 5 :(得分:0)

使用递归CTE(公用表表达式)输出字母A-Z,每个字母/字符一行:

;WITH cteAZ AS
(
SELECT ASCII('A') [AlphaCode],CAST('A' AS CHAR(1)) [Alpha]
UNION ALL
SELECT a.AlphaCode + 1 [AlphaCode],CAST(CHAR(a.AlphaCode + 1) AS CHAR(1)) [Alpha]
FROM cteAZ a WHERE a.AlphaCode < ASCII('Z')
)
SELECT 
az.AlphaCode,az.Alpha
FROM 
cteAZ az

答案 6 :(得分:0)

with AlphabetList as
(
select  char(65) letter
union all
select  char(ascii(letter) + 1)
from    AlphabetList
where   letter <> 'Z'
)
select  *
from    AlphabetList

答案 7 :(得分:0)

试试这个

;WITH CHARA2Z
AS (
    SELECT 
        [ASCII] = ASCII('A'), 
        [LETTER] = CHAR(ASCII('A'))
    
    UNION ALL

    SELECT 
        [ASCII] + 1, 
        [LETTER] = CHAR([ASCII]+1)
    FROM 
        CHARA2Z
    WHERE 
        [ASCII] < ASCII('Z')
)

SELECT * FROM CHARA2Z
<块引用>

用“a”和“z”替换小写字母的“A”和“Z”。

答案 8 :(得分:0)

源于@MarkoJuvančič 的回答,但适用于每个 SQL DBMS 的解决方案:

CREATE TEMPORARY TABLE alphabet (ltr CHAR(1));

SET @row_number = 0;

INSERT INTO alphabet
SELECT
        CHAR((@row_number:=@row_number + 1) +64) -- 'A' is the 65th character on the ASCII table
    FROM customer -- any table with 26 or more rows could suffice for this job
    WHERE @row_number < 26;