Solved

SQL Query - Select Statement Help

Posted on 2013-05-30
9
467 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sql query 7 35
help with issues with ReportViewer in VS2015 5 23
SQL JOIN 6 32
Loop through Multiple Processes Async 2 17
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

705 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

20 Experts available now in Live!

Get 1:1 Help Now