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

前端 未结 16 1112
粉色の甜心
粉色の甜心 2020-12-07 11:07

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 comp

相关标签:
16条回答
  • 2020-12-07 11:22

    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.

    0 讨论(0)
  • 2020-12-07 11:24

    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
    
    0 讨论(0)
  • 2020-12-07 11:28
    ISNULL(
    case textcolum1
        WHEN '' THEN NULL
        ELSE textcolum1
    END 
    ,textcolum2) textcolum1
    
    0 讨论(0)
  • 2020-12-07 11:28

    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], ''))))
    
    0 讨论(0)
  • 2020-12-07 11:30

    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)
    
    0 讨论(0)
  • 2020-12-07 11:31

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

    0 讨论(0)
提交回复
热议问题