| | |
SQL Question
Please support our MS SQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
Good Morning All
I have a table named “Final” with Values like this
As you can see in the Description Field “Earl G” appears 3 times and it can be more than that. Now I want to append Cycles [cycle] strings to the corresponding description, I want to run a query so that this table may look like this
How can I achieve this in SQL
Thank you
I have a table named “Final” with Values like this
MS SQL Syntax (Toggle Plain Text)
ID || DESCR || CYCLE ====================================== 1 || Earl G || 20 2 || Earl G || 21 3 || Earl G || 22 4 || Davidson I Dr || 20 5 || Davidson I Dr || 21 6 || Davidson I Dr || 22 7 || Easton C || 20 8 || Easton C || 21 9 || Easton C || 22 10 || Edwards J Ms || 20 11 || Edwards J Ms || 21 12 || Edwards J Ms || 22
As you can see in the Description Field “Earl G” appears 3 times and it can be more than that. Now I want to append Cycles [cycle] strings to the corresponding description, I want to run a query so that this table may look like this
MS SQL Syntax (Toggle Plain Text)
ID || DESCR || CYCLE ============================================== 1 ||Earl G || 20 , 21, 22 4 ||Davidson I Dr || 20 , 21, 22 7 ||Easton C || 20 , 21, 22 10 ||Edwards J Ms || 20 , 21, 22
Thank you
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
try this
I did not test this.
Regards.
sql Syntax (Toggle Plain Text)
SELECT ( SELECT f.cycle as col1 FROM final as f for xml raw, elements, type).query('for $col1 in (row/col1) return concat($col1, " ")')
I did not test this.
Regards.
Last edited by peter_budo; May 7th, 2009 at 3:29 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
i have Resolved the Problem. Thank you. i did the Following
i created a UDF like this
and Called the Function like this
Thank you
i created a UDF like this
MS SQL Syntax (Toggle Plain Text)
CREATE FUNCTION GetCycle (@Descr Varchar(50)) RETURNS Varchar(500) AS BEGIN Declare @RetStr AS varchar(500),@Cycle Int --<-- Assuming Cycle field is of Type Integer Declare TmpCur Cursor FOR SELECT CyCle FROM YourTblName WHERE Descr=@Descr Open TmpCur SET @RetStr='' Fetch Next FROM TmpCur INTO @Cycle While @@Fetch_status=0 Begin SET @RetStr = @RetStr + Case when @RetStr='' then '' else ' ' End + Cast(@Cycle AS varchar) Fetch Next FROM TmpCur INTO @Cycle End Close TmpCur Deallocate TmpCur RETURN (@RetStr) END
and Called the Function like this
MS SQL Syntax (Toggle Plain Text)
SELECT Descr,.dbo.GetCycle(Descr) AS Cycle FROM YourTableName WHERE Descr IN ( SELECT DISTINCT Descr FROM YourTableName)
Thank you
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
![]() |
Similar Threads
- an sql question/challenge from a job application exam that i failed to answer (MS SQL)
- sql question (IT Professionals' Lounge)
- Newbie SQL question (VB.NET)
- Basic SQL question! (MS SQL)
- sql*plus or pl/sql question (MS SQL)
Other Threads in the MS SQL Forum
- Previous Thread: trigger
- Next Thread: INSERT with subquery and scalar vars
Views: 666 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for MS SQL
"last age autogrowth business connectingtodatabaseinuse count cursor data database dateadd datediff datepart day" dbsize deadlock delete_trigger exploit getdate hack highperformancecomputing hpc hpcserver2008 ibm iis limit live loop maximum microsoft ms mssql multiple multithreading news number password permission position query reporting result security server services sets single source sql sql-injection sqlserver sqlserver2005 subtype supercomputing supertype tables uniqueid update view weekday





