How do I check if a SQL Server text column is empt

2019-01-16 05:37发布

I am using SQL Server 2005. I have a table with a text column and I have many rows in the table where the value of this column is not null, but it is empty. Trying to compare against '' yields this response:

The data types text and varchar are incompatible in the not equal to operator.

Is there a special function to determine whether the value of a text column is not null but empty?

15条回答
姐就是有狂的资本
2楼-- · 2019-01-16 06:23

You have to do both:

SELECT * FROM Table WHERE Text IS NULL or Text LIKE ''

查看更多
The star\"
3楼-- · 2019-01-16 06:27

I know there are plenty answers with alternatives to this problem, but I just would like to put together what I found as the best solution by @Eric Z Beard & @Tim Cooper with @Enrique Garcia & @Uli Köhler.

If needed to deal with the fact that space-only could be the same as empty in your use-case scenario, because the query below will return 1, not 0.

SELECT datalength(' ')

Therefore, I would go for something like:

SELECT datalength(RTRIM(LTRIM(ISNULL([TextColumn], ''))))
查看更多
Root(大扎)
4楼-- · 2019-01-16 06:28
where datalength(mytextfield)=0
查看更多
登录 后发表回答