Solved

Sql Query- Null values

Posted on 2014-01-08
9
517 Views
Last Modified: 2014-01-20
Hello I have the below query
SELECT 'VA',  CONVERT(char(10),Entered,101),COUNT(RXNO) FROM  HRXS
 WHERE 
-- AND  
 NewRx=1 
 AND PharmID ='VA' and TransType not in('Q','B') and  (CCType IS NULL OR CCType ='C') 
 and Entered is not null and  CONVERT(char(10),Entered,101) between CONVERT(char(10),GETDATE()-31,101) and CONVERT(char(10),GETDATE(),101)
 
GROUP BY CONVERT(char(10),Entered,101)

Open in new window

Somewhere in the data there is null value for the entered field and so its returning no data,but if I comment the where condition on the entered field its returning data.How to handle this?
0
Comment
Question by:Star79
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39766197
try the below code

SELECT 'VA',  CONVERT(char(10),Entered,101),COUNT(RXNO) FROM  HRXS
 WHERE 
-- AND  
 isnull(NewRx,'')=1 
 AND isnull(PharmID,'') ='VA' and isnull(TransType,'') not in('Q','B') and  (CCType IS NULL OR CCType ='C') 
 and Entered is not null and  CONVERT(char(10),Entered,101) between CONVERT(char(10),GETDATE()-31,101) and CONVERT(char(10),GETDATE(),101)
 
GROUP BY CONVERT(char(10),Entered,101)

Open in new window

0
 

Author Comment

by:Star79
ID: 39766200
Surendra,
Your code is what I have posted as question.Thanks..
0
 

Author Comment

by:Star79
ID: 39766205
When I comment the convert function on Entered it brings data..as entered somewhere has null data in it


SELECT 'VA',  CONVERT(char(10),Entered,101),COUNT(RXNO) FROM  HRXS
 WHERE
-- AND  
 NewRx=1
 AND PharmID ='VA' and TransType not in('Q','B') and  (CCType IS NULL OR CCType ='C')
 and Entered is not null --and  CONVERT(char(10),Entered,101) between CONVERT(char(10),GETDATE()-31,101) and CONVERT(char(10),GETDATE(),101)
 
GROUP BY CONVERT(char(10),Entered,101)
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39766259
then why don't you just do this

add the below code inplace of that between

CONVERT(CHAR(10),ISNULL(Entered,GETDATE()-60),101) between CONVERT(CHAR(10),GETDATE()-31,101) and CONVERT(CHAR(10),GETDATE(),101)

Open in new window

0
 
LVL 11

Expert Comment

by:John_Vidmar
ID: 39766285
Your where-clause is saying "don't show me nulls on the Entered field"... I suspect you want either a null or a range-test:
SELECT	'VA'
,	CONVERT(char(10),Entered,101)
,	COUNT(RXNO) 
FROM	HRXS
WHERE	NewRx=1 
AND	PharmID ='VA'
AND	TransType not in ('Q','B') 
AND 	(	CCType IS NULL
	OR	CCType ='C'
	) 
AND	(	Entered IS NULL
	OR	CONVERT(char(10),Entered,101)
			between	CONVERT(char(10),GETDATE()-31,101)
			and	CONVERT(char(10),GETDATE(),101)
	)
GROUP
BY	CONVERT(char(10),Entered,101) 

Open in new window

0
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 250 total points
ID: 39766315
But you have "Entered IS NOT NULL", so I think the issue is that is a non-null value which does not meet the criteria.  Are you wanting a row even if the count is zero?

In addition, use this to go between dates, noting direct comparisons exclude NULLs already.

Entered >= DATEADD(DD, -31, CONVERT(DATE, GETDATE()))
AND Entered <= CONVERT(DATE, GETDATE())

 
If you are not in SQL 2008, or higher, compatibility, you can accomplish the same thing CONVERT(DATE, ...) is doing with DATEDIFF:
e.g., DATEADD(DD, -31, DATEDIFF(DD, 0, GETDATE()))

If you want row even when there is nothing matching the date range, then use a conditional aggregate.

SELECT 'VA'
     , CONVERT(char(10),Entered,101)
     /* if you have Entered values greater than today, 
        then add the second part to date filter; otherwise, it is unnecessary. */
     , COUNT(CASE WHEN Entered >= DATEADD(DD, -31, CONVERT(DATE, GETDATE())) THEN RXNO END) 
FROM  HRXS
WHERE NewRx = 1 
  AND PharmID = 'VA' 
  AND TransType NOT IN ('Q','B') 
  AND (CCType IS NULL OR CCType = 'C') 
GROUP BY CONVERT(char(10),Entered,101)
;

Open in new window

0
 

Author Comment

by:Star79
ID: 39766326
Surendra you code did not help.
John,
I want data that has no null on the entered field..and its has to be in the date range.
0
 
LVL 11

Expert Comment

by:John_Vidmar
ID: 39766396
Perhaps your data is too old, and that is the reason why you are not getting the records you are expecting.  Your where-clause is saying "only show me records where the Entered date is within the last 31 days"
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 250 total points
ID: 39766716
SELECT 'VA', CONVERT(char(10),Entered,101), COUNT(RXNO)
FROM dbo.HRXS
WHERE
 NewRx=1
 AND PharmID ='VA'
 and TransType not in('Q','B')
 and (CCType IS NULL OR CCType ='C')
 and Entered >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) - 31, 0)
 and Entered < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) + 1, 0)
GROUP BY CONVERT(char(10),Entered,101)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question