Solved

SQL Like Query

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
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…

910 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

22 Experts available now in Live!

Get 1:1 Help Now