用于标准化/清理名字/中间名/姓氏字段的 SQL

发布于 2024-08-12 08:21:43 字数 215 浏览 2 评论 0原文

我正在用 SQL 编写一个函数,我们可以用它来验证给定表中的名字/中间名/姓氏。

目前,我正在处理字符的 ascii 代码列表,我将认为这些字符无效并从输入中删除。

我的计划是创建一个表,其中包含我认为无效的那些字符的字符代码,并编写一个游标来替换其正在处理的当前输入记录中的每个无效字符。

我应该直接浏览整个 ascii 表,还是有人见过类似的工作,我可以从中构建?

I'm writing a function in SQL that we can use to validate First/Middle/Last names we have in a given table.

At the moment I'm working on a list of ascii codes for characters that I'm going to consider invalid and strip out of the input.

My plan is to create a table which contains the character codes of those characters I consider to be invalid, and to write a cursor that will replace each invalid character from the current input record its working on.

Should I just work my way through the entire ascii table or has anyone ever seen a similar effort like this that I can look at to build from?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

jJeQQOZ5 2024-08-19 08:21:43

使用 SQL 中的代码扫描数据中的无效字符是一种非常慢的方法,而且不太可能让您满意。

我认为大多数人会在数据库之外进行验证。

如果必须在数据库内执行此操作,请编写一个使用数据库固有语言(Oracle PL/SQL、MSSQL tsql)的触发器来检查字符串,只需将有效字符列表编码到脚本中即可。

当名字带有口音或其他有趣字符的人出现时会发生什么?

Using code in SQL to scan data for invalid characters is a very slow approach and unlikely to make you happy.

Most people would, I think, do this validating outside the db.

If you must do it inside the db, look to write a trigger that uses the database's inherent language (Oracle PL/SQL, MSSQL tsql) to check the string, just coding the valid character list into the script.

And what happens when someone with a name with accents or other interesting characters shows up?

々眼睛长脚气 2024-08-19 08:21:43

这几乎就是我们所做的。

declare @currentCharacter char(1)
declare @alphanumericString VARCHAR(250)
declare @inputStringLength int 
declare @positionIndex int

    --init variables
    select @positionIndex = 1
    select @alphanumericString = ''

    --get the string length
    select @inputStringLength = LEN(@inputString)

    --loop through the set
    while @positionIndex <= @inputStringLength
    begin
        --get each character 
        select @currentCharacter = substring(@inputString,@positionIndex,1)

        --make sure its between 0-9, A-Z, or a-z
        if (ascii(@currentCharacter) > 31 and ascii(@currentCharacter) < 126)

            set @alphanumericString = @alphanumericString + @currentCharacter

        --increament counter
        set @positionIndex = @positionIndex + 1
    end

    return @alphanumericString
end

当然,您希望在数据输入上执行此操作,而不是在整个表上执行此操作,因为这将花费很长时间。

This is pretty much what we do

declare @currentCharacter char(1)
declare @alphanumericString VARCHAR(250)
declare @inputStringLength int 
declare @positionIndex int

    --init variables
    select @positionIndex = 1
    select @alphanumericString = ''

    --get the string length
    select @inputStringLength = LEN(@inputString)

    --loop through the set
    while @positionIndex <= @inputStringLength
    begin
        --get each character 
        select @currentCharacter = substring(@inputString,@positionIndex,1)

        --make sure its between 0-9, A-Z, or a-z
        if (ascii(@currentCharacter) > 31 and ascii(@currentCharacter) < 126)

            set @alphanumericString = @alphanumericString + @currentCharacter

        --increament counter
        set @positionIndex = @positionIndex + 1
    end

    return @alphanumericString
end

Of course you want to do this on data entry not to the whole table as that will take forever.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文