How do I check if a SQL Server text column is empty?
Asked Answered
K

17

214

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?

Kei answered 28/8, 2008 at 21:9 Comment(1)
I would convert the data type if possible to varchar (max), text has been deprecated - best to start making the changes now if you are touching the table. Check with your dba of course. But the more things can get converted before they must be converted the better is my thought. It will depend on how much code you have using things like contains and write text which will be broken as to whether to do this now, but I bring it up, so you are aware that this will need to be changed eventually.Dziggetai
N
346
where datalength(mytextfield)=0
Naldo answered 28/8, 2008 at 21:14 Comment(3)
This was not the actual question, but just a remark for people that only read the title, don't forget to add OR mytextfield IS NULL when your column can be NULLKlipspringer
mytextfield IS NULL *OR* :-)Hoot
@Hoot SQL Server T-SQL doesn't honor short-circuit evaluation techniques, so order here doesn't effect the outcome.Daily
C
54
ISNULL(
case textcolum1
    WHEN '' THEN NULL
    ELSE textcolum1
END 
,textcolum2) textcolum1
Chemisorption answered 23/6, 2010 at 18:29 Comment(1)
The above query will actually handle nullness and emptiness nature of a text column and accordingly assign value based on condition. Upvote for the answer since this is what I was looking for. ThanksFrederiksen
C
36

Actually, you just have to use the LIKE operator.

SELECT * FROM mytable WHERE mytextfield LIKE ''
Cranach answered 29/8, 2008 at 6:6 Comment(1)
+1 I prefer this answer over the others here because it does not rely on the added overhead of calling SQL Functions like DataLength(), IsNull(), or Cast(). Maybe the generated query plan is the same (I didn't check); still I find this to be a far cleaner approach.Ossieossietzky
M
15

To get only empty values (and not null values):

SELECT * FROM myTable WHERE myColumn = ''

To get both null and empty values:

SELECT * FROM myTable WHERE myColumn IS NULL OR myColumn = ''

To get only null values:

SELECT * FROM myTable WHERE myColumn IS NULL

To get values other than null and empty:

SELECT * FROM myTable WHERE myColumn <> ''


And remember use LIKE phrases only when necessary because they will degrade performance compared to other types of searches.

Monoatomic answered 22/8, 2014 at 20:0 Comment(1)
Don't you mean myColumn IS NOT NULL AND my column = '';?Quarterhour
N
3
SELECT * FROM TABLE
WHERE ISNULL(FIELD, '')=''
Norine answered 10/2, 2014 at 19:21 Comment(2)
upvoted, but ... where ISNULL( TRIM( Field), '') = '' is even better ;-), if you feel that " " is an Empty string even with spaces insidePlywood
For MySQL the correct syntax is: SQL SELECT * FROM TABLE WHERE IFNULL(FIELD, '')='' Snowslide
T
2

Use the IS NULL operator:

Select * from tb_Employee where ename is null
Tineid answered 4/6, 2013 at 10:49 Comment(1)
atoumey states in the question that "the value of this column is not null, but it is empty" therefore ISNULL() wouldn't work :)Peripteral
C
1

I know this post is ancient but, I found it useful.

It didn't resolve my issue of returning the record with a non empty text field so I thought I would add my solution.

This is the where clause that worked for me.

WHERE xyz LIKE CAST('% %' as text)
Colonial answered 13/6, 2012 at 15:5 Comment(0)
S
1

Use DATALENGTH method, for example:

SELECT length = DATALENGTH(myField)
FROM myTABLE
Selfcommand answered 27/8, 2015 at 20:15 Comment(0)
N
1

Instead of using isnull use a case, because of performance it is better the case.

case when campo is null then '' else campo end

In your issue you need to do this:

case when campo is null then '' else
  case when len(campo) = 0 then '' else campo en
end

Code like this:

create table #tabla(
id int,
campo varchar(10)
)

insert into #tabla
values(1,null)

insert into #tabla
values(2,'')

insert into #tabla
values(3,null)

insert into #tabla
values(4,'dato4')

insert into #tabla
values(5,'dato5')

select id, case when campo is null then 'DATA NULL' else
  case when len(campo) = 0 then 'DATA EMPTY' else campo end
end
from #tabla

drop table #tabla
Norine answered 14/6, 2019 at 13:28 Comment(3)
you first check if is null, then you use len function... i hope it helpsNorine
What if the Column is not NULL and Empty, having no Text?Cordelia
spaces? or what do you mean?Norine
H
1
DECLARE @temp as nvarchar(20)

SET @temp = NULL
--SET @temp = ''
--SET @temp = 'Test'

SELECT IIF(ISNULL(@temp,'')='','[Empty]',@temp)
Habitforming answered 23/8, 2019 at 14:15 Comment(1)
You can also use IIF(DATALENGTH(@temp=0,'[Empty]',@temp)Tisdale
C
0

I would test against SUBSTRING(textColumn, 0, 1)

Cantharides answered 28/8, 2008 at 21:11 Comment(0)
A
0

Are null and an empty string equivalent? If they are, I would include logic in my application (or maybe a trigger if the app is "out-of-the-box"?) to force the field to be either null or '', but not the other. If you went with '', then you could set the column to NOT NULL as well. Just a data-cleanliness thing.

Angle answered 29/8, 2008 at 20:3 Comment(0)
I
0

I wanted to have a predefined text("No Labs Available") to be displayed if the value was null or empty and my friend helped me with this:

StrengthInfo = CASE WHEN ((SELECT COUNT(UnitsOrdered) FROM [Data_Sub_orders].[dbo].[Snappy_Orders_Sub] WHERE IdPatient = @PatientId and IdDrugService = 226)> 0)
                            THEN cast((S.UnitsOrdered) as varchar(50))
                    ELSE 'No Labs Available'
                    END
Introjection answered 7/3, 2012 at 19:50 Comment(1)
Wow, a subquery. That's not expensive at all.Quagmire
B
0

You have to do both:

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

Bamboozle answered 7/9, 2012 at 23:11 Comment(0)
D
0

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], ''))))
Dissect answered 7/8, 2015 at 10:9 Comment(0)
A
0

try this:

select * from mytable where convert(varchar, mycolumn) = ''

i hope help u!

Aquacade answered 10/10, 2018 at 12:47 Comment(0)
C
0

It will do two things:

  1. Null check and string null check
  2. Replace empty value to default value eg NA.
SELECT coalesce(NULLIF(column_name,''),'NA') as 'desired_name') from table;
Cuirass answered 29/7, 2021 at 9:26 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.