sql server - Sql ForXml; concatenating fields returning either a comma seperated list or empty string -
i have looked through several questions on using xml have either missed, or not found specific query.
fortunately, or perhaps not, service need submit data wants fields table(s) submitted attributes rather elements means can use xml auto. require things i'm not sure how write correct sql achieve, , welcome advice.
some fields (let's have 3 named log1, log2 , log3) need returned 1 attribute called arguments sake logs. if 3 log fields null needs empty string, or if contain data need concatenated comma separator. suspect simple coalesce not answer here, i'm not entirely sure else do.
as said welcome suggestions. thanks.
edit
apologies, should have added sql assist.
select landingid, vesselid, ticketnumber, convert(varchar(10),landingdate1,112) landingdate1, log1, log2, log3, coalesce(vesselname,'') vesselname, vesselowner landings.landingheaders sale xml auto
log1, log2 , log3 should become logs , either empty string if null or comma separated list if contain data.
edit2 (current output vesselname , owner removed preserve confidentiality)
<sale landingid="3388" vesselid="1" ticketnumber="1 " landingdate1="20130705" />
ass log values null nothing being returned, not sure if can use form of coalesce either produce empty string or comma separated list. know need trim ticketnumber.
it's better use not for xml auto
, for xml path
since gives more flexibility.
your desired goal (if understood correctly), can achieved (i've skipped fields)
select vesselid '@vesselid', ticketnumber '@ticketnumber', isnull(log1 + ',', '') + isnull(log2 + ',', '') + isnull(log3 + ',', '') '@log123' landings.landingheaders xml path('sale')
update
frankly, in particular case can achive goal using for xml auto
too, key idea of concatenating fields
isnull(log1 + ',', '') + isnull(log2 + ',', '') + isnull(log3 + ',', '')
to empty string if 3 fields nulls , non-empty string data otherwise remains same.
Comments
Post a Comment