【问题标题】:sqlserver trims char data when using a subselect使用子选择时sql server修剪char数据
【发布时间】:2014-10-21 15:25:33
【问题描述】:

我在 SQLSERVER 2008 中的选择出现问题。

我有一个 char(32) 列作为 PK 的表。每当我从这个表中选择时,pk 结果总是有 32 个字符,即使数据少于 32 个字符。 IE:(用空格替换点)'id1.......')。

但是,如果我从另一个表中选择数据,并使用该 id 进行子选择,则数据会被修剪。

示例代码:

select id,
(select description from othertable where othertable.id = mytable.id) as description
from mytable

此查询将修剪 id 数据('id1' 而不是 'id1.......................')。

我没有找到任何有关导致此问题或如何避免此问题的信息。

这个问题似乎只发生在 SQLSERVER 的特定模式中,它不会发生在 ORACLE 或其他引擎中。

【问题讨论】:

  • othertabledescription的数据类型是什么?
  • 与其他 char(32) 相同

标签: sql sql-server sql-server-2008 trim subquery


【解决方案1】:

确保您的 ANSI_PADDING 设置为“真”

SET ANSI_PADDING (Transact-SQL)

如果这不起作用,您可以更改查询以添加空格

select LEFT(id + SPACE(32),32),
(select description from othertable where othertable.id = mytable.id) as description
from mytable

【讨论】:

  • 如果othertable.descriptionvarchar 而不是char 可能不相关,因此我向他/她提出问题。
  • 这就是我添加更改后的查询的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-26
  • 1970-01-01
  • 2021-11-28
  • 2019-01-19
相关资源
最近更新 更多