0

I try to execute next code to replace all empty strings to NULL, where column type is TEXT. But this code seems to do nothing.

UPDATE mytable SET textField = NULL WHERE DATALENGTH(textField)=0

What is right way here to null this column?

UPD. OK, this is really work. It was just weird data showing of Embarcadero DBArtasian

7
  • 1
    That should work. Presumably you don't have any truly empty strings. Commented Mar 16, 2012 at 14:05
  • Is your table name table? You should put square brackets around it like so: [table] Commented Mar 16, 2012 at 14:06
  • @Jeff it just for example, not real name of table Commented Mar 16, 2012 at 14:07
  • @Martin Smith - I checked with SELECT * FROM mytable WHERE DATALENGTH(textField)=0 and there are a lot of empty text fields Commented Mar 16, 2012 at 14:08
  • Well there's no reason why those wouldn't qualify for the UPDATE too then. Any triggers on the table? Commented Mar 16, 2012 at 14:10

4 Answers 4

2

Your way is correct. Play a little with CAST and RTRIM, maybe you have some spaces in the field.

Sign up to request clarification or add additional context in comments.

Comments

2

Please convert the Text field to varchar, since LEN cannot be used on Text fields. This worked for me

UPDATE mytable SET textField = NULL WHERE LEN(convert(varchar(1000),textField )) = 0

Comments

0

probabily the where condition does not return anything

try do do the below to see how many record will be affected

SELECT * from Table WHERE DATALENGTH(textField)=0

Comments

0
UPDATE mytable 
SET textField = NULL 
WHERE LEN(LTRIM(RTRIM(textField))) = 0

1 Comment

You can't use any of those functions on a text column. And wouldn't need to trim anyway as LEN ignores trailing space.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.