?
Solved

SQL Query - Select Statement Help

Posted on 2013-05-30
9
Medium Priority
?
492 Views
Last Modified: 2013-05-30
Good Day Experts!

I am having what seems to be a very straightforward issue but I cannot figure it out!

I am getting results as expected from this Select statement...about 35000 records with [Audit Reason 3] values being NULL:

Select
     [Audit Reason 3],*
from
     [Processing Table]
Where
    [Process Week] > '5/1/2013'

I have added to this to only include records where [Audit Reason 3] <> 'Import History'. So I have this Select statement:

Select
     [Audit Reason 3],*
from
     [Processing Table]
Where
    [Process Week] > '5/1/2013' and [Audit Reason 3] <> 'Import History'

NOW, I get no records!  I can't figure out why [Audit Reason 3] <> 'Import History' is now causing no records to show.

Can you help?

Thanks,
jimbo99999
0
Comment
Question by:Jimbo99999
[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
  • 3
  • 2
  • +1
9 Comments
 
LVL 41

Expert Comment

by:ralmada
ID: 39207951
maybe some trailing spaces?

rtrim([Audit Reason 3]) <> 'Import History'

or using like

[Audit Reason 3] not like 'Import History%'
0
 
LVL 7

Assisted Solution

by:Ross Turner
Ross Turner earned 200 total points
ID: 39207953
Hi Jimbo

[Process Week]  is this a datetime datatype ?

anyway try this

SELECT [Audit Reason 3],*
FROM [Processing Table]
WHERE [Process Week] > '20130105'
AND [Audit Reason 3] not like 'Import History'

Open in new window

0
 
LVL 13

Assisted Solution

by:Philip Pinnell
Philip Pinnell earned 200 total points
ID: 39207957
Try

SELECT [Audit Reason 3],*
FROM [Processing Table]
WHERE [Process Week] > '20130105'
AND isnull([Audit Reason 3],'') not like '%Import History%'
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:Jimbo99999
ID: 39207973
I just checked and the DataType is varchar.  I will try the trailing spaces idea...thanks, I did not even think down that road.
0
 

Author Comment

by:Jimbo99999
ID: 39208129
I tried the following and received no records returned:

rtrim([Audit Reason 3]) <> 'Import History'
[Audit Reason 3] not like 'Import History%'
[Audit Reason 3] not like 'Import History'

I tried the following and it worked:

isnull([Audit Reason 3],'') not like '%Import History%'

Ok, problem is I do not understand why it worked and the others didn't!  I mean NULL is <> 'Import History' so I guess that is where I am confused.

jimbo99999
0
 
LVL 41

Accepted Solution

by:
ralmada earned 1600 total points
ID: 39208147
Ok, In that case I would use the following, as this will improve performance:

.... AND ([Audit Reason 3] is null or [Audit Reason 3] not like 'Import History%')

This will help you understand why it didn't work:

http://weblogs.sqlteam.com/markc/archive/2009/06/08/60929.aspx
0
 
LVL 41

Expert Comment

by:ralmada
ID: 39208177
And now that I'm thinking, I guess you don't even have a trailing spaces problem. So I would just do

Select
     [Audit Reason 3],*
from
     [Processing Table]
Where
    [Process Week] > '5/1/2013' and ([Audit Reason 3] <> 'Import History' or [Audit Reason 3] is null)
0
 

Author Closing Comment

by:Jimbo99999
ID: 39208437
Thank you all for your repsonses. I have archived all for future reference.

Have a good day,
jimbo99999
0
 
LVL 13

Expert Comment

by:Philip Pinnell
ID: 39210014
Thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses

764 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