0

我在 SSRS 中编写的查询的一部分不能像我需要的那样工作。情况是我正在尝试使用列中的值列表创建一个下拉参数 - 我们将其称为 Column5。问题是该列中的某些行是空白的;它们不是空的,它们只是完全由空格组成。因此,它们不会出现在参数的值列表中,这意味着这些行将被完全忽略。

我想做的是这样的:
SELECT [...stuff...] CASE WHEN (RTRIM(LTRIM(Column5)) = '') THEN 'None' ELSE Column5 END AS ColumnAlias

我已经尝试了上述方法和一些变体,但似乎没有任何效果。

注意:我将上面的查询粘贴到 SQL Server 中,它工作得很好。似乎 SQL Server 和 SSRS 处理空白的方式不同。

编辑:显然问题的一部分是 SQL 不会根据重命名的列进行过滤。我将查询粘贴到 SQL Server 中,并包含WHERE Column5 = 'None'. 它没有返回任何行,即使 Column5 中有几千行明确表示“无”。看来我可能不得不重新考虑我的整个方法。

4

2 回答 2

0

I know this is an older post, but just in case you're still having trouble...

I've seen that empty strings or strings of spaces seem to handled and identified differently in different systems (Access vs SSRS vs T-SQL run in SSMS). I find that using the len() function works pretty well everywhere. I usually do something like the following:

Case 
    When ISNULL(LEN(LTRIM(RTRIM([Column5]))),0) = 0 
        Then 'None' 
        Else [Column5] 
    End

This way it's actively looking for and counting characters after you've removed all spacers.

Once you've identified these fields, you can put your whole query (assuming it's not overly complex) into a CTE and work with the end product. This can sometimes be easier than trying to work with the data as it is being derived.

With Selected_Records as (
Select ...
...
)
Select Distinct Column5

From Selected_Records

Then you can add any other conditions, ordering, or aggregates on top of the derived data without hindering its derivation. This works pretty well until the query in the CTE gets very complicated or utilizes many parameters (these are anecdotal observations).

Hope this helps someone.

于 2013-11-07T21:39:44.180 回答
0

尝试这个:

SELECT [...stuff...] CASE WHEN LEFT(Column5, 1) = ' ' or RIGHT(Column5, 1) = ' ' THEN 'None' ELSE Column5 END AS ColumnAlias

这样做的原因是,当您指定条件时,将忽略尾随空格。换句话说,“HI”、“hi”和“Hi”(后面有一个空格)都被认为是相等的。

于 2013-08-08T15:38:24.470 回答