Using 'like' in ssrs expressions
Asked Answered
S

5

24

I'm trying to highlight a field when the value has the word 'deadline' in it. I'm trying to use the expression:

=IIf(Fields!Notes.Value like "%deadline%","Yellow","Transparent")

in the BackgroundColor property.

It's not highlighting the field (not changing the background color). The 'Notes' field is a text datatype and I'm using Report Builder 3.0 if that makes a difference. What am I doing wrong?

Serra answered 25/1, 2012 at 20:32 Comment(0)
D
17

SSRS does NOT use SQL syntax, but instead uses Visual Basic.

Use something like this:

=IIf(Fields!Notes.Value.IndexOf("deadline") >= 0,"Yellow","Transparent")

Or .Contains instead of .IndexOf

=IIf(Fields!Notes.Value.ToLowerInvariant().Contains("deadline"),"Yellow","Transparent")
Dubitable answered 25/1, 2012 at 20:34 Comment(2)
I tried using Contains() before but I was missing the tolower() or tolowerinvariant() method to convert it from text to string. Thanks :)Serra
What do you mean by converting text to string? Aren't they the same thing?Seasickness
I
47

It is like in access: not '%' but '*':

=Fields!Notes.Value Like "*deadline*"
Infrequency answered 25/1, 2012 at 20:38 Comment(2)
It is case-sensitive. FYI. Use with lowercase - =IIf(LCase(Fields!Notes.Value) Like "*deadline*","Yellow","Transparent")Harder
Can the expression be used to look for phrases in the string like "text with spaces" or does it only work with single words like "textwithoutspaces"?Seasickness
D
17

SSRS does NOT use SQL syntax, but instead uses Visual Basic.

Use something like this:

=IIf(Fields!Notes.Value.IndexOf("deadline") >= 0,"Yellow","Transparent")

Or .Contains instead of .IndexOf

=IIf(Fields!Notes.Value.ToLowerInvariant().Contains("deadline"),"Yellow","Transparent")
Dubitable answered 25/1, 2012 at 20:34 Comment(2)
I tried using Contains() before but I was missing the tolower() or tolowerinvariant() method to convert it from text to string. Thanks :)Serra
What do you mean by converting text to string? Aren't they the same thing?Seasickness
L
11

"InStr" works for me:

=IIF(InStr(Fields!Notes.Value,"deadline")>0, "Yellow", "Transparent") 

Remember that the compare value is case-sentive, so maybe use UCASE around:

=IIF(InStr(UCASE(Fields!Notes.Value),"DEADLINE"))>0, "Yellow", "Transparent") 
Larina answered 16/8, 2013 at 10:6 Comment(0)
L
2

Why not use something like:

Fields!Notes.Value.Contains("deadline") 
Logography answered 25/1, 2012 at 20:35 Comment(0)
C
1

It is case-sensitive. FYI. Use with lowercase - =IIf(LCase(Fields!Notes.Value) Like "*deadline*","Yellow","Transparent")

Coheman answered 18/11, 2020 at 7:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.