Connecting Tech Pros Worldwide Help | Site Map

Query for displaying a particular set of data

Member
 
Join Date: Jul 2008
Posts: 42
#1: Aug 12 '08
Hi,

I have a table with fields number,category,date and if i want to choose numbers of a particular category and date.
then the query will b
select number from <table name> where category=" " and date =" "
and it will display a list of numbers.

And wat if i want those numbers horizontally.Suppose i hav three numbers
123
234
678
and i want it to be like this
123 234 678
but it should be done automatically. is there any query for that.
rsrinivasan's Avatar
Familiar Sight
 
Join Date: Mar 2007
Location: India
Posts: 221
#2: Aug 12 '08

re: Query for displaying a particular set of data


Try this, and reply me

Expand|Select|Wrap|Line Numbers
  1. DECLARE @numberList varchar(255)
  2. Select @numberList = IsNull(number + ',' + @numberList, NULLIF( number, @numberList)) from <tablename>   where <condition>
  3. Select @numberList as numberList
ck9663's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,925
#3: Aug 12 '08

re: Query for displaying a particular set of data


If this is a big data and you're using sql2005, use UN/PIVOT...

-- CK
Member
 
Join Date: Jul 2008
Posts: 42
#4: Aug 12 '08

re: Query for displaying a particular set of data


Thanks a lot.It works.

And wat if I want to select number and category where date is given like

number category date
123 1 8/12/2008
234 1 8/12/2008
123 2 8/12/2008
789 2 8/12/2008
5678 1 8/12/2008

then i should get:

numberlist category
123,234,5678 1
123,789 2

can u plz help me in this.
Member
 
Join Date: Jul 2008
Posts: 42
#5: Aug 14 '08

re: Query for displaying a particular set of data


Quote:

Originally Posted by rsrinivasan

Try this, and reply me

Expand|Select|Wrap|Line Numbers
  1. DECLARE @numberList varchar(255)
  2. Select @numberList = IsNull(number + ',' + @numberList, NULLIF( number, @numberList)) from <tablename>   where <condition>
  3. Select @numberList as numberList

Thanks a lot.Ur query helps me a lot

i want to store the data from variable to a new table.can it b possible.plz reply
Member
 
Join Date: Jul 2008
Posts: 42
#6: Aug 16 '08

re: Query for displaying a particular set of data


Quote:

Originally Posted by rsrinivasan

Try this, and reply me

Expand|Select|Wrap|Line Numbers
  1. DECLARE @numberList varchar(255)
  2. Select @numberList = IsNull(number + ',' + @numberList, NULLIF( number, @numberList)) from <tablename>   where <condition>
  3. Select @numberList as numberList


Hi,

I want the variable data in a new table.cud u plz help me.
ck9663's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,925
#7: Aug 18 '08

re: Query for displaying a particular set of data


You can parse the variable.

But I'd still say you use PIVOT/UNIPVOT, if you're running sql 2005.

-- CK
Member
 
Join Date: Jul 2008
Posts: 42
#8: Aug 18 '08

re: Query for displaying a particular set of data


Quote:

Originally Posted by ck9663

You can parse the variable.

But I'd still say you use PIVOT/UNIPVOT, if you're running sql 2005.

-- CK

would u plz help me in that?
amitpatel66's Avatar
Moderator
 
Join Date: Mar 2007
Location: Hyderabad, India
Posts: 2,192
#9: Aug 18 '08

re: Query for displaying a particular set of data


PIVOT for MSSQL 2005
Member
 
Join Date: Jul 2008
Posts: 42
#10: Aug 18 '08

re: Query for displaying a particular set of data


Quote:

Originally Posted by amitpatel66

PIVOT for MSSQL 2005


i dont know how to use PIVOT. I have only one table.i have to concatenate the multiple records into single record.
Can u send me a query related to my prob.
Reply


Similar Microsoft SQL Server bytes