| Author |
Topic |
|
umertahir
Posting Yak Master
154 Posts |
Posted - 2009-03-09 : 11:30:59
|
Hi all,I need a little help to put comma at the right place, depending if there are more than one rows returned from the query, on my piece of string from the following code:DECLARE @currentColumnName VARCHAR(50)SET @currentColumnName = ''SELECT @currentColumnName = @currentColumnName + ', ' + tB.chrColumnNameFROM dbo.tblLandcatchEntityEventType AS tA INNER JOIN dbo.tblLandcatchEntityEventTypeColumnNames AS tB ON tA.intEventTypeID = tB.intEventTypeID LEFT OUTER JOIN #tblTempTableWithColumnOrderAndName AS tSheet ON tB.intColumnOrderNumber = tSheet.intColumnId AND tB.chrColumnName = tSheet.chrColumnNameWHERE tA.chrEventName = @tableName AND tSheet.intColumnId IS NULLORDER BY tB.intColumnOrderNumber Result:, Site, YearClassI want the result to be like:Expected Result:Site, YearClassand incase if there is only one row thenExpected Result for one row rproduced:SiteThanksUmer |
|
|
umertahir
Posting Yak Master
154 Posts |
Posted - 2009-03-09 : 11:48:05
|
I have done it the following way which is working for me:SELECT @currentColumnName = CASE WHEN @currentColumnName = '' THEN tB.chrColumnName WHEN @currentColumnName <> '' THEN @currentColumnName + ', ' + tB.chrColumnName END |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-03-10 : 03:29:41
|
quote: Originally posted by umertahir Hi all,I need a little help to put comma at the right place, depending if there are more than one rows returned from the query, on my piece of string from the following code:DECLARE @currentColumnName VARCHAR(50)SET @currentColumnName = ''SELECT @currentColumnName = COALESECE(@currentColumnName + ', ','') + tB.chrColumnNameFROM dbo.tblLandcatchEntityEventType AS tA INNER JOIN dbo.tblLandcatchEntityEventTypeColumnNames AS tB ON tA.intEventTypeID = tB.intEventTypeID LEFT OUTER JOIN #tblTempTableWithColumnOrderAndName AS tSheet ON tB.intColumnOrderNumber = tSheet.intColumnId AND tB.chrColumnName = tSheet.chrColumnNameWHERE tA.chrEventName = @tableName AND tSheet.intColumnId IS NULLORDER BY tB.intColumnOrderNumber Result:, Site, YearClassI want the result to be like:Expected Result:Site, YearClassand incase if there is only one row thenExpected Result for one row rproduced:SiteThanksUmer
do like above |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-03-10 : 04:35:17
|
| if ur using 2005 & above try this tooSELECT stuff(@currentColumnName,1,1,'') |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-03-10 : 04:45:04
|
quote: Originally posted by bklr if ur using 2005 & above try this tooSELECT stuff(@currentColumnName,1,1,'')
stuff works even in sql 2000 |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-03-10 : 04:58:12
|
quote: Originally posted by visakh16
quote: Originally posted by bklr if ur using 2005 & above try this tooSELECT stuff(@currentColumnName,1,1,'')
stuff works even in sql 2000
i haven't tried in sql 2000 so that i mentioned 2005 onwards |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-03-10 : 05:03:44
|
quote: Originally posted by bklr
quote: Originally posted by visakh16
quote: Originally posted by bklr if ur using 2005 & above try this tooSELECT stuff(@currentColumnName,1,1,'')
stuff works even in sql 2000
i haven't tried in sql 2000 so that i mentioned 2005 onwards
you need to make sure you check something before suggesting in future |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-03-10 : 05:07:18
|
quote: Originally posted by visakh16
quote: Originally posted by bklr
quote: Originally posted by visakh16
quote: Originally posted by bklr if ur using 2005 & above try this tooSELECT stuff(@currentColumnName,1,1,'')
stuff works even in sql 2000
i haven't tried in sql 2000 so that i mentioned 2005 onwards
you need to make sure you check something before suggesting in future
so that only i have mentioned 2005 and above it will works |
 |
|
|
umertahir
Posting Yak Master
154 Posts |
Posted - 2009-03-10 : 05:25:40
|
Msg 195, Level 15, State 10, Line 93'COALESECE' is not a recognized built-in function name.quote: Originally posted by visakh16
quote: Originally posted by umertahir Hi all,I need a little help to put comma at the right place, depending if there are more than one rows returned from the query, on my piece of string from the following code:DECLARE @currentColumnName VARCHAR(50)SET @currentColumnName = ''SELECT @currentColumnName = COALESECE(@currentColumnName + ', ','') + tB.chrColumnNameFROM dbo.tblLandcatchEntityEventType AS tA INNER JOIN dbo.tblLandcatchEntityEventTypeColumnNames AS tB ON tA.intEventTypeID = tB.intEventTypeID LEFT OUTER JOIN #tblTempTableWithColumnOrderAndName AS tSheet ON tB.intColumnOrderNumber = tSheet.intColumnId AND tB.chrColumnName = tSheet.chrColumnNameWHERE tA.chrEventName = @tableName AND tSheet.intColumnId IS NULLORDER BY tB.intColumnOrderNumber Result:, Site, YearClassI want the result to be like:Expected Result:Site, YearClassand incase if there is only one row thenExpected Result for one row rproduced:SiteThanksUmer
do like above
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-03-10 : 05:26:59
|
quote: Originally posted by umertahir Msg 195, Level 15, State 10, Line 93'COALESECE' is not a recognized built-in function name.quote: Originally posted by visakh16
quote: Originally posted by umertahir Hi all,I need a little help to put comma at the right place, depending if there are more than one rows returned from the query, on my piece of string from the following code:DECLARE @currentColumnName VARCHAR(50)SET @currentColumnName = ''SELECT @currentColumnName = COALESCE(@currentColumnName + ', ','') + tB.chrColumnNameFROM dbo.tblLandcatchEntityEventType AS tA INNER JOIN dbo.tblLandcatchEntityEventTypeColumnNames AS tB ON tA.intEventTypeID = tB.intEventTypeID LEFT OUTER JOIN #tblTempTableWithColumnOrderAndName AS tSheet ON tB.intColumnOrderNumber = tSheet.intColumnId AND tB.chrColumnName = tSheet.chrColumnNameWHERE tA.chrEventName = @tableName AND tSheet.intColumnId IS NULLORDER BY tB.intColumnOrderNumber Result:, Site, YearClassI want the result to be like:Expected Result:Site, YearClassand incase if there is only one row thenExpected Result for one row rproduced:SiteThanksUmer
do like above
thats a typo it should be COALESCE |
 |
|
|
umertahir
Posting Yak Master
154 Posts |
Posted - 2009-03-10 : 06:14:19
|
Result: , Site, YearClassquote: Originally posted by visakh16
quote: Originally posted by umertahir Msg 195, Level 15, State 10, Line 93'COALESECE' is not a recognized built-in function name.quote: Originally posted by visakh16
quote: Originally posted by umertahir Hi all,I need a little help to put comma at the right place, depending if there are more than one rows returned from the query, on my piece of string from the following code:DECLARE @currentColumnName VARCHAR(50)SET @currentColumnName = ''SELECT @currentColumnName = COALESCE(@currentColumnName + ', ','') + tB.chrColumnNameFROM dbo.tblLandcatchEntityEventType AS tA INNER JOIN dbo.tblLandcatchEntityEventTypeColumnNames AS tB ON tA.intEventTypeID = tB.intEventTypeID LEFT OUTER JOIN #tblTempTableWithColumnOrderAndName AS tSheet ON tB.intColumnOrderNumber = tSheet.intColumnId AND tB.chrColumnName = tSheet.chrColumnNameWHERE tA.chrEventName = @tableName AND tSheet.intColumnId IS NULLORDER BY tB.intColumnOrderNumber Result:, Site, YearClassI want the result to be like:Expected Result:Site, YearClassand incase if there is only one row thenExpected Result for one row rproduced:SiteThanksUmer
do like above
thats a typo it should be COALESCE
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-03-10 : 06:21:40
|
| show your code. i dont think posted code will give you leading , |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-03-10 : 06:22:20
|
| did u tried by using stuff |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-03-10 : 06:30:53
|
also you should have concat null yields null property set to true for above to work. what does this return?EXEC sp_dboption 'your db name here','concat null yields null' |
 |
|
|
santosh2307
Starting Member
7 Posts |
Posted - 2009-03-10 : 08:54:02
|
Check patindexdeclare @var varchar(40)set @var=',Site, YearClass'select case when patindex(',',@var)=0 then substring(@var,(patindex(',',@var))+ 2,len(@var)) else @var end Hope this helps u.quote: Originally posted by umertahir Hi all,I need a little help to put comma at the right place, depending if there are more than one rows returned from the query, on my piece of string from the following code:DECLARE @currentColumnName VARCHAR(50)SET @currentColumnName = ''SELECT @currentColumnName = @currentColumnName + ', ' + tB.chrColumnNameFROM dbo.tblLandcatchEntityEventType AS tA INNER JOIN dbo.tblLandcatchEntityEventTypeColumnNames AS tB ON tA.intEventTypeID = tB.intEventTypeID LEFT OUTER JOIN #tblTempTableWithColumnOrderAndName AS tSheet ON tB.intColumnOrderNumber = tSheet.intColumnId AND tB.chrColumnName = tSheet.chrColumnNameWHERE tA.chrEventName = @tableName AND tSheet.intColumnId IS NULLORDER BY tB.intColumnOrderNumber Result:, Site, YearClassI want the result to be like:Expected Result:Site, YearClassand incase if there is only one row thenExpected Result for one row rproduced:SiteThanksUmer
|
 |
|
|
umertahir
Posting Yak Master
154 Posts |
Posted - 2009-03-10 : 09:18:58
|
Guys my CASE statement works fine, please suggest if there is something wrong or some scenario which I have not covered in it.Rest of the functions suggested by all you gurus sounds good but kind of complicated to me. |
 |
|
|
|