Sql 密码生成器 - 8 个字符,上下并包括一个数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15038311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:52:00  来源:igfitidea点击:

Sql Password Generator - 8 characters, upper and lower and include a number

sqlsql-servertsql

提问by DotNet

I need to create a new password in TSQL which needs to be 8 characters (a-z) which have to include upper and lower characters as well as at least one number.

我需要在 TSQL 中创建一个新密码,该密码需要 8 个字符 (az),其中必须包括大写和小写字符以及至少一个数字。

Can anyone help me with this?

谁能帮我这个?

回答by Grisha Weintraub

select cast((Abs(Checksum(NewId()))%10) as varchar(1)) + 
       char(ascii('a')+(Abs(Checksum(NewId()))%25)) +
       char(ascii('A')+(Abs(Checksum(NewId()))%25)) +
       left(newid(),5)
from yourTable

回答by Joseph Lee

Something to consider, a bit more complex:

需要考虑的东西,有点复杂:

SELECT      TOP 8
            SUBSTRING(tblSource.vsSource, tblValue.number + 1, 1) AS vsChar
FROM        (SELECT 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' AS vsSource) AS tblSource
            JOIN master..spt_values AS tblValue 
                ON tblValue.number < len(tblSource.vsSource)
WHERE       tblValue.type = 'P'
ORDER BY    NEWID()

but very random.

但很随意。

回答by chrisb

Here's one option that gives you very strongly random passwords. It uses a numbers tablebut other than that fairly straightforward.

这是一种可以为您提供非常随机的密码的选项。它使用数字表,但除此之外相当简单。

declare @chars char(62), @bytes binary(8)
set @chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
set @bytes = CRYPT_GEN_RANDOM(8)

select SUBSTRING(@chars, (SUBSTRING(@bytes, n.Number, 1) % LEN(@chars)) + 1, 1)
from dbo.Numbers n
where n.Number between 1 and LEN(@bytes)
FOR XML PATH ('')