英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论