def text_concat(format_str, source_str, concat_with, order_by=None):
    """
    Generates the SQL for text concatenation.

    Parameters
    ----------
    format_str : str
        Format of the concatenated output.
    source_str : str
        fields and FROM part of statement to provide values, 
        along with WHERE to join to aggregated. 
    concat_with : str
        String to concatenate with
    order_by : str
        Column to order by
    """
    if order_by is not None:
        order_by = f" ORDER BY x.{order_by} ASC"
    else:
        order_by = ""
    sql = f"""
    STUFF((SELECT '{concat_with}'+CAST({format_str} AS VARCHAR(MAX))
	FROM (SELECT DISTINCT {source_str}) x {order_by}
	FOR XML PATH(''), TYPE).value('.','VARCHAR(MAX)'), 1, {len(concat_with)}, '') """
    return sql