Solved

SQL Like Query

Posted on 2013-06-26
2
304 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
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 48

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

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…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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…

789 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