MS SQL中的空间是什么 [英] what is space in MS SQL

查看:46
本文介绍了MS SQL中的空间是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(General_Brand +SPACE(1)+ General_Model LIKE @BrandModel)


我在to字段之间需要一个空格,但是它不起作用.

例如:当作者键入"Nokia"时,可以找到"Nokia 701";当他键入"701"时,可以找到"Nokia 701",但是当他键入"Nokia 70"时,它找不到任何提示,我该怎么办?
tnx寻求帮助.


i need one space between the to field but it does not work .

for example : when the writer type "Nokia" it can find "Nokia 701" and also when he type "701" it can find "Nokia 701" but when he type "Nokia 70" it does not find any think what can i do ?
tnx for helps.

推荐答案

如果需要添加单个字符,则可以使用CHAR 函数和ASCII值32.例如:
If you need to add a single char you can use CHAR function and the ASCII value 32. For example:
select '>' + char(32) + '<'


但是,在数据库中串联字段通常被认为是不好的做法.考虑将品牌和模型划分到两个不同的字段(或适当的话甚至划分到两个不同的表)


However, having concatenated fields in the database is often considered as a bad practice. Consider separating the brand and the model to two different fields (or even to two different tables if appropriate)


除了Mika提出的建议之外,我建议使用参数化查询代替用字符串组成查询(这也是不安全的).换句话说,诸如诺基亚70"或诺基亚701"之类的具体参数值永远不会出现在查询中.代替这些字符串,应该出现一个形式参数.

当执行参数替换时,字符串参数的值可以包含任何字符,因为替换机制将传递对象,而不是字符串成为查询的一部分.这些机制取决于特定的数据库提供程序,它们可以使用命名或位置参数对应关系.对于SQL Server,请参见System.Data.SqlClient.SqlCommandSystem.Data.SqlClient.SqlCommand.Parameters:
http://msdn.microsoft.com/en-us/library/system. data.common.dbcommand.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. data.sqlclient.sqlcommand.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx [ ^ ].

有关一般方法的说明,请参见 http://msdn.microsoft.com/en-us/library/yy6y35y8 .aspx [ ^ ] .

—SA
In alternative or addition to what Mika suggested, I would suggest to use parametrized queries instead of composing queries out of string (which also can be unsafe). In other words, the concrete parameter values like "Nokia 70" or "Nokia 701" should never appear in the query. In place of these strings, a formal parameter should appear.

When you perform substitution of a parameter, the value of a string parameter can contain any characters, because substitution mechanism passes object, not strings being a part of query. The mechanisms depend on particular database provider, they can use named or positional parameter correspondence. For SQL Server, see System.Data.SqlClient.SqlCommand, System.Data.SqlClient.SqlCommand.Parameters:
http://msdn.microsoft.com/en-us/library/system.data.common.dbcommand.aspx[^],
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx[^],
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx[^].

For explanation of general approach, see http://msdn.microsoft.com/en-us/library/yy6y35y8.aspx[^].

—SA


尝试一下:

Try this one :

(General_Brand +' '+ General_Model LIKE '%' + @BrandModel + '%')



它会找到与@BrandModel
类似的任何General_Brand +'' ''+ General_Model
希望对您有所帮助.



It finds any General_Brand +'' ''+ General_Model that is like @BrandModel

Hope it helps.


这篇关于MS SQL中的空间是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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