Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Concatenate Function VBA to SQL Function

Author  Topic 

micnie_2020
Posting Yak Master

232 Posts

Posted - 2009-12-23 : 23:47:57
This is the function i used for Ms Access VBA. Anyone here can hep me to convert it to be MS SQL Function??

Thank you very much.


Function Concatenate(pstrSQL As String, _
Optional pstrDelim As String = ", ") _
As String
'example
'tblFamily with FamID as numeric primary key
'tblFamMem with FamID, FirstName, DOB,...
'return a comma separated list of FirstNames
'for a FamID
' John, Mary, Susan
'in a Query
'SELECT FamID,
'Concatenate("SELECT FirstName FROM tblFamMem
' WHERE FamID =" & [FamID]) as FirstNames
'FROM tblFamily
'

'======For DAO uncomment next 4 lines=======
'====== comment out ADO below =======
'Dim db As DAO.Database
'Dim rs As DAO.Recordset
'Set db = CurrentDb
'Set rs = db.OpenRecordset(pstrSQL)

'======For ADO uncomment next two lines=====
'====== comment out DAO above ======
Dim rs As New ADODB.Recordset
rs.Open pstrSQL, CurrentProject.Connection, _
adOpenKeyset, adLockOptimistic
Dim strConcat As String 'build return string
With rs
If Not .EOF Then
.MoveFirst
Do While Not .EOF
strConcat = strConcat & _
.Fields(0) & pstrDelim
.MoveNext
Loop
End If
.Close
End With
Set rs = Nothing
'====== uncomment next line for DAO ========
'Set db = Nothing
If Len(strConcat) > 0 Then
strConcat = Left(strConcat, _
Len(strConcat) - Len(pstrDelim))
End If
Concatenate = strConcat
End Function



Regards,
Micheale

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-12-24 : 01:37:52
Post some sample data with expected result

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

micnie_2020
Posting Yak Master

232 Posts

Posted - 2009-12-24 : 01:57:36
Dear Mr Madhivanan,

I just looking for solution how to convert this function into ms sql??? Just wondering MS Access VBA Function izzit applicable in ms sql.

This is to handle rows to column by concatenate using comma(,). I knew there is many solution provided through sqlteam.com as well.

Just wanted to add-ons/expand my knowledge.


Regards,
Micheale
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-12-24 : 02:00:31
Refer this
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81254

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -