473,421 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

Append Fields in SQL

Hi,



Consider, that I have 5 fields, In which first four fields has same value and fifth one has different value. If I retrieve these records from table it should shows first 4 fields only once and fifth fields should be appended. How can I achieve.

Eg:

field #1 field #2 field #3 field #4 field #5

101 VV BLR HOME 12K

101 VV BLR HOME 15K

101 VV BLR HOME 18K



if I retrieve means, It should come like.



101 VV BLR HOME 12K,15k,18k instead repeating all fields.
Sep 4 '10 #1
1 1696
gpl
152 100+
With the help of a function, this is quite easy; there is no generic solution, so it will have to be rewritten for every different situation.

Expand|Select|Wrap|Line Numbers
  1. CREATE FUNCTION dbo.AllField5 (@Field1 Varchar(255), @Field2 Varchar(255), @Field3 Varchar(255), @Field4 Varchar(255))
  2. RETURNS Varchar(1024)
  3.  
  4. AS
  5. BEGIN
  6.  
  7. DECLARE @Fld5List varchar(1024)
  8.  
  9. SELECT @Fld5List = COALESCE(@Fld5List + ', ', '') + CAST(MyTable.Field5 AS varchar(255))
  10. FROM MyTable
  11. WHERE 
  12.       MyTable.Field1 = @Field1 AND
  13.       MyTable.Field2 = @Field2 AND
  14.       MyTable.Field3 = @Field3 AND
  15.       MyTable.Field4 = @Field4 
  16.  
  17.     RETURN @Fld5List
  18. END
  19.  
And to use it
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT
  2.       MyTable.Field1,
  3.       MyTable.Field2,
  4.       MyTable.Field3,
  5.       MyTable.Field4,
  6.       dbo.AllField5 (
  7.                       MyTable.Field1,
  8.                       MyTable.Field2,
  9.                       MyTable.Field3,
  10.                       MyTable.Field4
  11.       ) as All5
  12. FROM MyTable
  13.  
Sep 6 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: DC | last post by:
Im getting the error ADODB.Fields error '800a0bb9' Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another. In the following script to copy and...
1
by: MIchael McDowell | last post by:
Anyidea how this might be done using and XML Web Service and the XMLSerialzation class? A pointer to an online example would be deeply appreciated. Thankyou in advance, Michael McD
9
by: JMCN | last post by:
hi- i have inherited an access 97 database that keeps track of the loans. i have been running into referential intergrity problems when i try to append new loans to table. first of all is a...
2
by: Danny | last post by:
I want to extract a subset of fields from one table into another the master table has many fields the subset has about half, but still many. Is there a way I can just append the master into the...
4
by: Melissa | last post by:
I have a frontend file named CustomerApp and backend file named CustomerData. CustomerApp is at C:\Customer Database and CustomerData is at S:\Customer Database. Could someone help me with the code...
3
by: sparks | last post by:
I was copying fields from one table to another. IF the var name starts with milk I change it to egg and create it in the destination table. It works fine but I want to copy the description as...
3
by: JOEP | last post by:
What do I need to do to allow an append query to post null values to records in a field of the destination table? Basically I want to allow records with null values to post to the table. The append...
0
by: msnews.Microsoft.com | last post by:
Hi , Not able to figure out the way to format numeric fields. Code snippet Dim ColStyle3 As New DataGridTextBoxColumn With ColStyle3
1
by: sphinney | last post by:
All, I have a ADODB.Recordset in my Access 2002 project. I've been able to successfully add fields to the record set. According the the MS Access help files, I now must update the recordset to...
1
by: flormarinkovic | last post by:
Good morning all, I am new in Python and i need your help. I have already wrote a program that works for one of the shapes that i need to check, but now i need to do the same for the rest of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.