dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online Membersprasad
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Forums » .NET » SQL Server »

Multiple Like Output in SQL


Posted Date: 09 Jul 2012      Posted By:: jeyjey     Member Level: Silver    Member Rank: 2238     Points: 4   Responses: 4



Hi,

I have a doubt in SQL

I have ten records in a table and i want to display that records based on
my criteria.

Select * from Tablename
where (Column like 'D%' or Column like 'B%' or Column like 'A%' or Column like 'C%' or Column like 'E%' )


myoutput

AA
BB
CC
DD
EE

Expected Output
DD
BB
AA
CC
EE

Can you please help on to get my expected output.

Thanks,
JJ





Responses

#679539    Author: Mohan M Devarinti      Member Level: Gold      Member Rank: 488     Date: 09/Jul/2012   Rating: 2 out of 52 out of 5     Points: 3

Hi,

actually, the result depends on the which order the data stored in table. try like bellow:

select * from dbo.table3
where (dr_t like 'D%' )
union all
select * from dbo.table3
where dr_t like 'B%'
union all
select * from dbo.table3
where dr_t like 'A%'
union all
select * from dbo.table3
where dr_t like 'C%'
union all
select * from dbo.table3
where dr_t like 'E%'



 
#679581    Author: Nathaniel Nellas Sumaya      Member Level: Silver      Member Rank: 1262     Date: 09/Jul/2012   Rating: 2 out of 52 out of 5     Points: 1

will you please insert the code of your table structure here?


 
#679624    Author: Ramiya      Member Level: Bronze      Member Rank: 3521     Date: 10/Jul/2012   Rating: 2 out of 52 out of 5     Points: 4

Just try it out it will work :
SELECT * FROM TABLENAME WHERE COLUMNNAME LIKE 'P%' UNION ALL
SELECT * FROM TABLENAME WHERE COLUMNNAME LIKE 'B%' UNION ALL
SELECT * FROM TABLENAME WHERE COLUMNNAME LIKE 'T%' UNION ALL
SELECT * FROM TABLENAME WHERE COLUMNNAME LIKE 'M%'

If the above doesn't work try the Following it will surely help you :
SELECT * FROM (SELECT *,1 AS 'ID' FROM TABLENAME WHERE COLUMNNAME LIKE 'P%' UNION ALL
SELECT *,2 AS 'ID' FROM TABLENAME WHERE COLUMNNAME LIKE 'B%' UNION ALL
SELECT *,3 AS 'ID' FROM TABLENAME WHERE COLUMNNAME LIKE 'T%' UNION ALL
SELECT *,4 AS 'ID' FROM TABLENAME WHERE COLUMNNAME LIKE 'M%')A ORDER BY ID






 
#679634    Author: Ravindran        Member Level: Diamond      Member Rank: 3     Date: 10/Jul/2012   Rating: 2 out of 52 out of 5     Points: 4

try like below query


--create table like below
create table Tablename(Column1 nvarchar(50))

--Insert data like below
insert into Tablename values('AA')
insert into Tablename values('BB')
insert into Tablename values('CC')
insert into Tablename values('DD')
insert into Tablename values('EE')
insert into Tablename values('FF')
insert into Tablename values('GG')
insert into Tablename values('HH')

--Try like below query to get expected output as per your requirement

Select Column1 from Tablename where (Column1 like 'D%') UNION ALL
Select Column1 from Tablename where (Column1 like 'B%') UNION ALL
Select Column1 from Tablename where (Column1 like 'A%') UNION ALL
Select Column1 from Tablename where (Column1 like 'C%') UNION ALL
Select Column1 from Tablename where (Column1 like 'E%')


Regards
N.Ravindran
Your Hard work never fails



 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Next : How to assign values in while loop from view
Previous : How to connect SQL server remotely inside the LAN?
Return to Discussion Forum
Post New Message
Category:

Related Messages



Follow us on Twitter: https://twitter.com/dotnetspider

Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.