“not convert in Ltrim and Rtrim” 翻译为中文是 “不在 Ltrim 和 Rtrim 中转换”。

huangapple go评论58阅读模式
英文:

not convert in Ltrim and Rtrim

问题

Customer_Fname varchar(20) 非空
检查 (len(Rtrim(Ltrim(Customer_Fname))) > 1)

我尝试使用此语法来检查数据长度,但未转换 Ltrim 和 Rtrim。

示例:

插入到 dbo.person(Customer_Fname) 的值为 ' Mukund'

答案与我插入的值相同

英文:
Customer_Fname varchar(20) not null 
    check (len(Rtrim(Ltrim(Customer_Fname))) > 1)

I try this syntax to check the data length, but not convert Ltrim and Rtrim.

Example:

insert into dbo.person(Customer_Fname) 
values ('  Mukund')

Answer is same as I insert value

答案1

得分: 1

The current trigger only checks the length after trimming. Here's an example of a trigger set to fire on INSERT and UPDATE events that strips leading or trailing spaces and checks you don't store an empty string:

当前触发器仅在修剪后检查长度。以下是一个触发器示例,设置为在插入和更新事件上触发,会去除前导或尾随空格并检查您不存储空字符串:

CREATE TRIGGER trg_RemoveSpaces_Customer_Fname
ON YourTable
AFTER INSERT, UPDATE
AS
BEGIN
UPDATE YourTable
SET Customer_Fname = CASE
WHEN LTRIM(RTRIM(Customer_Fname)) = '' THEN NULL
ELSE LTRIM(RTRIM(Customer_Fname))
END
FROM YourTable
INNER JOIN inserted i ON YourTable.ID = i.ID
END

nb: you could use TRIM() if using SQL Server 2017 or later

nb: 如果使用SQL Server 2017或更高版本,可以使用TRIM()

英文:

The current trigger only checks the length after trimming. Here's an example of a trigger set to fire on INSERT and UPDATE events that strips leading or trailing spaces and checks you don't store an empty string:

CREATE TRIGGER trg_RemoveSpaces_Customer_Fname 
ON YourTable
AFTER INSERT, UPDATE
AS
BEGIN
    UPDATE YourTable
    SET Customer_Fname = CASE
        WHEN LTRIM(RTRIM(Customer_Fname)) = '' THEN NULL
        ELSE LTRIM(RTRIM(Customer_Fname))
    END
    FROM YourTable
    INNER JOIN inserted i ON YourTable.ID = i.ID
END

nb: you could use TRIM() if using SQL Server 2017 or later

huangapple
  • 本文由 发表于 2023年5月15日 11:16:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定