Solved

SQL Query - Select Statement Help

Posted on 2013-05-30
9
470 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
  • 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 50 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 50 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
 

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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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 400 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exit the loop 4 37
Run a batch file when a Crystal Report is opened 5 15
SQL VIEW 7 23
C# guarantee sql connection close 6 32
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now