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

Sql ServerSql Server-2005SqldatatypesNotnullEmptydatatext

Sql Server Problem Overview


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?

Sql Server Solutions


Solution 1 - Sql Server

where datalength(mytextfield)=0

Solution 2 - Sql Server

ISNULL(
case textcolum1
	WHEN '' THEN NULL
	ELSE textcolum1
END 
,textcolum2) textcolum1

Solution 3 - Sql Server

Actually, you just have to use the LIKE operator.

SELECT * FROM mytable WHERE mytextfield LIKE ''

Solution 4 - Sql Server

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.

Solution 5 - Sql Server

SELECT * FROM TABLE
WHERE ISNULL(FIELD, '')=''

Solution 6 - Sql Server

Use the IS NULL operator:

Select * from tb_Employee where ename is null

Solution 7 - Sql Server

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)

Solution 8 - Sql Server

Use DATALENGTH method, for example:

SELECT length = DATALENGTH(myField)
FROM myTABLE

Solution 9 - Sql Server

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

Solution 10 - Sql Server

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

Solution 11 - Sql Server

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.

Solution 12 - Sql Server

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

Solution 13 - Sql Server

You have to do both:

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

Solution 14 - Sql Server

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], ''))))

Solution 15 - Sql Server

try this:

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

i hope help u!

Solution 16 - Sql Server

DECLARE @temp as nvarchar(20)

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

SELECT IIF(ISNULL(@temp,'')='','[Empty]',@temp)

Solution 17 - Sql Server

> 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;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionatoumeyView Question on Stackoverflow
Solution 1 - Sql ServerEric Z BeardView Answer on Stackoverflow
Solution 2 - Sql ServerEricView Answer on Stackoverflow
Solution 3 - Sql ServerDaneView Answer on Stackoverflow
Solution 4 - Sql ServerNimaView Answer on Stackoverflow
Solution 5 - Sql ServerEnrique GarciaView Answer on Stackoverflow
Solution 6 - Sql ServerPearlView Answer on Stackoverflow
Solution 7 - Sql ServerMike RobertsView Answer on Stackoverflow
Solution 8 - Sql ServerJorgesysView Answer on Stackoverflow
Solution 9 - Sql ServerEnrique GarciaView Answer on Stackoverflow
Solution 10 - Sql ServerbdukesView Answer on Stackoverflow
Solution 11 - Sql ServerTyler GoochView Answer on Stackoverflow
Solution 12 - Sql ServerYoosaf AbdullaView Answer on Stackoverflow
Solution 13 - Sql ServermarklarkView Answer on Stackoverflow
Solution 14 - Sql ServerLeoView Answer on Stackoverflow
Solution 15 - Sql ServerLuiz Fernando Corrêa LeiteView Answer on Stackoverflow
Solution 16 - Sql Serveruser11968038View Answer on Stackoverflow
Solution 17 - Sql ServerPallav ChananaView Answer on Stackoverflow