• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 508
  • Last Modified:

SQL Query - Select Statement Help

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
Jimbo99999
Asked:
Jimbo99999
  • 3
  • 3
  • 2
  • +1
3 Solutions
 
ralmadaCommented:
maybe some trailing spaces?

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

or using like

[Audit Reason 3] not like 'Import History%'
0
 
Ross TurnerCommented:
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
 
Philip PinnellCommented:
Try

SELECT [Audit Reason 3],*
FROM [Processing Table]
WHERE [Process Week] > '20130105'
AND isnull([Audit Reason 3],'') not like '%Import History%'
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
Jimbo99999Author Commented:
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
 
Jimbo99999Author Commented:
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
 
ralmadaCommented:
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
 
ralmadaCommented:
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
 
Jimbo99999Author Commented:
Thank you all for your repsonses. I have archived all for future reference.

Have a good day,
jimbo99999
0
 
Philip PinnellCommented:
Thanks
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now