简体   繁体   中英

Column_name > “ ”?

I came across a query today where inside an IIF statement there was a comparison where column > " " . I have never seen this before and I am not even sure what it is doing. I am familiar with comparing strings with < and >. IE "B" < "W" returns True.

However what exactly does comparing a column to a string of spaces do?

The line in the select statement:

,IIf([CD] > "     " AND [DT]=#12/31/9999#,[MT],[O]) AS [Final]

Can someone explain exactly what this comparison is doing and why would you want this?

Your logic is checking that CD is larger than a field whose name consists only of spaces. That seems highly unlikely.

My guess is that you are either using MS Access because the date constant is not compatible with SQL Server. In SQL Server this would be:

IIf([CD] > '     ' AND [DT] = #12/31/9999#, [MT], [O]) AS [Final]

This is validating that the string does not start with a bunch of spaces. It is actually a rather pathetic way of doing this; more typically LTRIM() would be involved.

I might further speculate that CD is exactly five characters long. In this case, the logic is checking that it is not all spaces. This would be particularly applicable if the type were CHAR(5) NOT NULL .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM