Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 18th, 2008, 11:27 PM
Registered User
 
Join Date: Feb 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to tam240579
Default I want to use both "IN", "LIKE" in "SELECT...WHER"

I have 2 table:
    Table 1: tbl_account. It's content:
       account_id (varchar(10))
         4211%%
         4231%%
         42320%
         421236
         421239

    Table 2: tbl_details. It's content:
         account_id (varchar(10)) money(int)
            42113100 100
            42113200 500
            42113200 400
            42313100 600
            42313A00 780
            42314A00 220
            42123637 86
            4212394B 140
            42153486 105
            42110123 120
             ......

I want get somethings from tbl_account and tbl_details.
I have SELECT command

SELECT A.*
FROM tbl_details
WHERE left(account_id,6) IN (SELECT RTRIM(account_id) FROM tbl_account)

But, i have results is not right. I think i have prolems about with LIKE, IN....
Please, everybody show me how to solve this problem...
Thanks you very much....




 
Old February 18th, 2008, 11:55 PM
Friend of Wrox
 
Join Date: Oct 2007
Posts: 130
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via AIM to urtrivedi
Default

Please tell us that what result you are expecting from the query.
Show sample result. Right now your query looks proper.

urt

Help yourself by helping someone.
 
Old February 21st, 2008, 04:39 AM
Friend of Wrox
 
Join Date: Dec 2004
Posts: 221
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi tam,

-- // SQL BATCH START
declare @tbl_account table (account_id varchar(10))
declare @tbl_details table(account_id varchar(10), [money] int)

insert into @tbl_account values('4211%%')
insert into @tbl_account values('4231%%')
insert into @tbl_account values('42320%')
insert into @tbl_account values('421236')
insert into @tbl_account values('421239')

insert into @tbl_details values ('42113100', 100)
insert into @tbl_details values ('42113200', 500)
insert into @tbl_details values ('42113200', 400)
insert into @tbl_details values ('42313100', 600)
insert into @tbl_details values ('42313A00', 780)
insert into @tbl_details values ('42314A00', 220)
insert into @tbl_details values ('42123637', 86)
insert into @tbl_details values ('4212394B', 140)
insert into @tbl_details values ('42153486', 105)
insert into @tbl_details values ('42110123', 120)


SELECT *
FROM @tbl_details
WHERE left(account_id,6) IN (SELECT RTRIM(account_id) FROM @tbl_account)

-- // SQL BATCH END

OUTPUT
========
account_id money
======================
42123637 86
4212394B 140

According to your SQL statement the 6 chars of left which are matched with details table are shown.

When you want to show the data from table A and table B then using nestes SQL statement is not a right choice. you have use JOINS on these two tables and fetch those columns as you need.

so..

sql:
select a.account_id, b.money from @tbl_account a
inner join @tbl_details b on left(a.account_id, 6) = left(b.account_id, 6)

output:
account_id money
==========================
421236 86
421239 140

if you want to use "like" and "in" then

sql:
select a.account_id, b.money from @tbl_account a
inner join @tbl_details b on left(a.account_id, 4) = left(b.account_id, 4)
where b.account_id like ('%4211%')

output:
account_id money
==========================
4211 120
4211 100
4211 500
4211 400

hope, this will give you an idea. I know I am not completely answering your question.


With Regards,
Raghavendra Mudugal





Similar Threads
Thread Thread Starter Forum Replies Last Post
Select row in GridView WITHOUT using Select button rao965 ASP.NET 2.0 Professional 1 February 15th, 2008 10:44 AM
Select from another select statement to a repeater simsen ASP.NET 2.0 Professional 0 May 2nd, 2007 04:34 PM
Fill select box and select recordset value markd Classic ASP Databases 1 February 20th, 2006 06:41 PM
select="node1", select="node2"... Baldo XSLT 7 March 12th, 2004 10:38 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.