自动修整数据库条目? [英] Auto trim Database entries?

查看:69
本文介绍了自动修整数据库条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以编辑表定义以在输入时自动修剪所有尾随空白的条目?



或者这会更高效/更轻松/通常在

解决方案

如果您在SQL Server中工作,则可以使其自动执行,但实际上如果您确定并进行适当考虑,请执行此操作。 ANSI_PADDING在SQL中决定是否修剪插入值的尾部空格。



该测试将在创建表时应用,并且在设置后不会更改,并且该设置适用于varchar,但不适用于nvarchar。



测试显示差异的脚本如下:

 
上设置ansi_padding创建表foo(myID int身份,myString varchar (50))
插入foo值('abcd')
从foo

下拉表中选择datalength(mystring)foo
设置ansi_padding off
创建表foo(myID int身份,myString varchar(50))
从foo值('abcd')中插入
从foo
中选择datalength(mystring)

在第一个表上,由于插入了空格,数据长度返回10,在第二个示例中,在插入时对其进行了修剪。



我个人更喜欢用代码控制需要完成的事情,而不是将其留给设置,但是我提供了一些示例来说明可以根据需要在数据库级别完成它。


Is it possible edit a table definition to automagically trim entries of all trailing whitespace when they are entered?

Or would this be more efficient/easier/normally done in the code befeore submitting the entries??

解决方案

If you are working within SQL Server you can make this automatic but you should only really do this if you are sure and with appropriate consideration. The ANSI_PADDING decides in SQL as to whether it trims the trailing spaces of a value being inserted. The setting is applied at the time that table is created, and is not altered after, and the setting will work for varchar, but does not work for nvarchar.

A test script to show the difference is as follows:

set ansi_padding on
create table foo (myID int identity, myString varchar(50))
insert into foo values ('abcd      ')
select datalength(mystring) from foo

drop table foo
set ansi_padding off
create table foo (myID int identity, myString varchar(50))
insert into foo values ('abcd      ')
select datalength(mystring) from foo

On the first table the data length returns as 10 since the spaces were inserted, on the second example they are trimmed on insert.

I would personally prefer the code controlling what was needed to be done instead of leaving it to the setting, but I included the examples to show it can be done at the DB level if required.

这篇关于自动修整数据库条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆