Solved

SQL Like Query

Posted on 2013-06-26
2
321 Views
Last Modified: 2013-06-26
Hi all I have the following query

SELECT     TOP (100) PERCENT dbo.cname.cn_ref, dbo.cname.cn_anal, dbo.cname.cn_catag, dbo.ctran.ct_date, dbo.ctran.ct_quan
FROM         dbo.ctran RIGHT OUTER JOIN
                      dbo.cname ON dbo.ctran.ct_ref = dbo.cname.cn_ref
WHERE     (dbo.ctran.ct_type = 'I') AND (dbo.ctran.ct_date >= '2013-05-01')

which returns me 401 Records if I then add a like statement

SELECT     TOP (100) PERCENT dbo.cname.cn_ref, dbo.cname.cn_anal, dbo.cname.cn_catag, dbo.ctran.ct_date, dbo.ctran.ct_quan
FROM         dbo.ctran RIGHT OUTER JOIN
                      dbo.cname ON dbo.ctran.ct_ref = dbo.cname.cn_ref
WHERE     (dbo.ctran.ct_type = 'I') AND (dbo.ctran.ct_date >= '2013-05-01') AND (dbo.cname.cn_catag LIKE '%STD%')

I then get 177 records I then want to add a further like to include LIKE '%HBNE%')

this however returns thousends of records yet I f I run the query with just the one like

SELECT     TOP (100) PERCENT dbo.cname.cn_ref, dbo.cname.cn_anal, dbo.cname.cn_catag, dbo.ctran.ct_date, dbo.ctran.ct_quan
FROM         dbo.ctran RIGHT OUTER JOIN
                      dbo.cname ON dbo.ctran.ct_ref = dbo.cname.cn_ref
WHERE     (dbo.ctran.ct_type = 'I') AND (dbo.ctran.ct_date >= '2013-05-01') AND (dbo.cname.cn_catag LIKE '%HBNE%')

I only get 12 records

so basically I would like the original statement to include both STD and HBNE

John
0
Comment
Question by:pepps11976
[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
2 Comments
 
LVL 16

Accepted Solution

by:
EvilPostIt earned 500 total points
ID: 39278348
SELECT     TOP (100) PERCENT dbo.cname.cn_ref, dbo.cname.cn_anal, dbo.cname.cn_catag, dbo.ctran.ct_date, dbo.ctran.ct_quan
FROM         dbo.ctran RIGHT OUTER JOIN
                      dbo.cname ON dbo.ctran.ct_ref = dbo.cname.cn_ref
WHERE     (dbo.ctran.ct_type = 'I') AND (dbo.ctran.ct_date >= '2013-05-01') AND (dbo.cname.cn_catag LIKE '%HBNE%' OR dbo.cname.cn_catag LIKE '%STD%')

Open in new window

0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39280187
regarding TOP 100 PERCENT..
the optimizer recognizes that TOP 100 PERCENT qualifies all rows and does not need to be computed at all.  It gets removed from the query plan, and there is no other reason to do an intermediate sorting operation.  As such, the output isn't returned in any particular order.
fro MSDN : TOP 100 Percent ORDER BY Considered Harmful.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

615 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