Solved

SQL Like Query

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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

728 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