To test the proposed solutions, the following table was created:
use tempDB;
CREATE TABLE DocumentNotes (Subject varchar(50), Notes ntext);
INSERT into DocumentNotes
values (1, space(1)), (2, N''), (3, N'teste 3'),
(4, N' teste 4'), (5, NULL), (6, space(10)),
(7, replicate(cast(N' ' as nvarchar(max)), 10000) + N'teste 7');
go
In case 4 starts with spaces but there is content in the column. Case 7 has 10 thousand spaces before text 'test 7'. The code will be correct if it returns only lines 1, 2, 5 and 6, because lines 3, 4 and 7 contain text.
When creating the code, it is necessary to be aware of the peculiarities of columns of type ntext, whose storage capacity is up to (2 30 - 1).
The Ltrim() function can be used to delete spaces on the left. If there are only spaces, the result is space(0) or ''. But the Ltrim() function does not accept ntext columns as parameter, so just convert previously to nvarchar(max).
The code suggestion is:
SELECT Subject, Notes
from DocumentNotes
where Ltrim(Cast(IsNull(Notes, space(0)) as nvarchar(max))) = space(0);
I would like to know why the -1, so that I can improve the question,
– Marco Souza