Convert Generic List of strings / Arrays to CSV string


string.Join(",", MyList.ToArray());

or


string.Join(",", MyList);

Advertisements

T-SQL Determine the next monthly occurance from a starting date

A sample script to determine the next monthly occurence from a start date


select
DateCreated,
dateadd(d,
 -- If the day of the end of the month > anniversary day then add the end of the month instead of anniversary
 case when datepart(d,datecreated)> datepart(d,dateadd(d,-1,dateadd(m,datediff(m,0,getdate())+1,0)))
 then datepart(d,dateadd(d,-1,dateadd(m,datediff(m,0,getdate())+1,0)))
 else datepart(d,datecreated)
 end
 -1,
 -- This month or next month if we are already past today
 dateadd(m,
 case when datepart(d,datecreated)
 then 1
 else 0
 end
 + datediff(m,0,getdate()),0)) CreatedAnniversary
from MyDB..MyTable

T-SQL Order records based upon a group

Quite often you want to find the latest record order of records based upon a group of data.  Using group you would have to then join back onto the table to filter the data further. Using partition and ronumber you can do this without having to group.


select

FieldA,

FieldB,

FieldC,

row_number() over( partition by FieldA,Field B order by FieldC ) As RowNumber

From Table