Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Exclude rows with blanks in a particular field


Exclude rows with blanks in a particular field

Author
Message
Beatrix Kiddo
Beatrix Kiddo
SSC Eights!
SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)

Group: General Forum Members
Points: 963 Visits: 3571
I know this is a popular one and I've Googled it but I haven't found anything that has worked yet, so apologies if this is incredibly obvious.

I'm trying to amend a view to exclude records that are either null or blank in a field called CURRENT_POINT

...
and CURRENT_POINT is not null
and DATALENGTH(CURRENT_POINT)> 0



The nulls are fine, but I'm still getting the blanks. I hoped DATALENGTH would resolve that but it hasn't.

As I said, I'm sorry if it's obvious. Can anybody advise me please?
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9027 Visits: 19036
Beatrix Kiddo (5/1/2013)
I know this is a popular one and I've Googled it but I haven't found anything that has worked yet, so apologies if this is incredibly obvious.

I'm trying to amend a view to exclude records that are either null or blank in a field called CURRENT_POINT

...
and CURRENT_POINT is not null
and DATALENGTH(CURRENT_POINT)> 0



The nulls are fine, but I'm still getting the blanks. I hoped DATALENGTH would resolve that but it hasn't.

As I said, I'm sorry if it's obvious. Can anybody advise me please?


Here's a little test harness to see what DATALENGTH returns with different values:
SELECT 
CURRENT_POINT,
[LEN] = LEN(CURRENT_POINT),
[DATALENGTH] = DATALENGTH(CURRENT_POINT)
FROM (
SELECT rn = 1, CURRENT_POINT = CAST(' ' AS VARCHAR(15)) UNION ALL
SELECT 2, SPACE(3) UNION ALL
SELECT 3, NULL UNION ALL
SELECT 4, '' UNION ALL
SELECT 5, 'Something here'
) d
WHERE 1 = 1



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Steve Jones
Steve Jones
SSC-Dedicated
SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)

Group: Administrators
Points: 36344 Visits: 18752
Is not > '' working?

CREATE TABLE MyTable
( current_point VARCHAR(20)
)
;
INSERT MyTable SELECT 'Test'
INSERT MyTable SELECT ''
INSERT mytable SELECT ' '
INSERT mytable SELECT NULL
GO
SELECT * FROM mytable
-- 4 rows

SELECT * FROM mytable
WHERE Current_Point IS NOT NULL
-- 3 rows

SELECT * FROM mytable
WHERE Current_Point IS NOT NULL
AND current_point > ' '
-- 1 rows

SELECT * FROM mytable
WHERE Current_Point IS NOT NULL
AND current_point > ''
-- 1 row



Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Beatrix Kiddo
Beatrix Kiddo
SSC Eights!
SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)

Group: General Forum Members
Points: 963 Visits: 3571
Thanks for the replies so far.

Steve Jones - SSC Editor (5/1/2013)
Is not > '' working?


Using that doesn't exclude the rows with blanks in, if that's what you mean?

I'm sorry, I'm not quite sure what the previous post is asking.

I don't even know why some of these fields have blanks instead of nulls. It seems to be a data entry issue.
Beatrix Kiddo
Beatrix Kiddo
SSC Eights!
SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)

Group: General Forum Members
Points: 963 Visits: 3571
Ah, ChrisM@Work, I think I get what you were saying now.

and LEN(LTRIM(RTRIM(CURRENT_POINT))) > 0


Seems to have fixed it.


Thanks all!
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9027 Visits: 19036
Beatrix Kiddo (5/1/2013)
Ah, ChrisM@Work, I think I get what you were saying now.

and LEN(LTRIM(RTRIM(CURRENT_POINT))) > 0


Seems to have fixed it.


Thanks all!


LEN ignores leading spaces, you don't need to trim. However, Steve's suggestion is likely to perform faster because CURRENT_POINT isn't wrapped in a function, which in most cases would prevent an index on CURRENT_POINT from being used for the filter.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Beatrix Kiddo
Beatrix Kiddo
SSC Eights!
SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)

Group: General Forum Members
Points: 963 Visits: 3571
Cool, thanks. I've amended it to.

and LEN(CURRENT_POINT) > 0



:-)
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9027 Visits: 19036
Beatrix Kiddo (5/1/2013)
Cool, thanks. I've amended it to.

and LEN(CURRENT_POINT) > 0



:-)


Is there any reason not to use this?
CURRENT_POINT > ' '



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search