MsSql

关注公众号 jb51net

关闭
首页 > 数据库 > MsSql > sqlserver生成连续数值,字母,字符

sqlserver如何生成连续数值,字母,字符

作者:huryer

这篇文章主要介绍了sqlserver如何生成连续数值,字母,字符问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

sqlserver生成连续数值,字母,字符

-- 生成连续数值(0-9)
WITH x AS
    (SELECT 0 AS id UNION ALL
    SELECT id + 1 AS id
    FROM x
    WHERE id < 9)
SELECT *
FROM x;
-- 生成连续字母(a-z)
WITH x AS
    (SELECT 1 AS id UNION ALL
    SELECT id + 1 AS id
    FROM x
    WHERE id < 26)
SELECT CHAR(id+96) AS id
FROM x;
-- 生成连续字符(0-9,a-z)
WITH x AS
    (SELECT 0 AS id,CHAR(ascii('0')) AS cc UNION ALL
    SELECT id + 1 AS id,CASE WHEN id<9 THEN CHAR(ascii('1')+id) ELSE CHAR(ascii('a')+id-9) END AS cc
    FROM x
    WHERE id < 35)
SELECT id, cc
FROM x;

sqlserver 生成连续日期

在sqlserver里,可以利用系统表master..spt_values里面存储的连续数字0到2047,结合dateadd()函数生成连续的日期。

SELECT
    CONVERT (VARCHAR (10),dateadd(d, number, GETDATE()),23) AS rq
FROM
    master..spt_values 
WHERE
    type = 'p'
AND number between 1 and 365

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文