views:

76

answers:

1

I am trying to figure out the equivalent C# for this SQL:

@var1 = "1a1"
@var2 = "1F"
CONVERT(varbinary, @var1) > CONVERT(varbinary, @var2)

Is it the same as this?

if (var1.CompareTo(var2) > 0)
{

}

If not, then how do I simulate it?

A: 

Conversion to varbinary is presumably to force a case-sensitive comparison. If that is the only concern, then yes, the two statements are equivalent.

The default string comparison will be case sensitive. However, the default string comparison will use current culture information and may treat some strings differently depending on culture. If this is a concern for your application, then you can use ordinal comparison instead which would produce exactly the same results a the varbinary cast.

if (String.Compare(var1, var2, StringComparison.Ordinal) > 0)
{

}
Sam
You mean case sensitive, not insensitive? Isn't varbinary a blob-type so that it can't assume anything about the contents? Isn't it basically comparing bytes?
Lasse V. Karlsen
@Lasse V. karlsen, you're right, I had a few inconsistencies in my answer. Corrected now.
Sam