开发者

SQL query to make a column of numbers a string

开发者 https://www.devze.com 2023-03-13 19:09 出处:网络
Is it possible to convert a column of doubles in a big string: Something like: att1 ---------- 123.2 3.6 6.77

Is it possible to convert a column of doubles in a big string:

Something like:

att1
----------
123.2
3.6
6.77
23.43
4.78
7.6
123.2
9.6
1.77
3.43
24.78
76.6
411.5
346.5
975.75
162.788
4.5
16.2
324.5
746.5
975.75
12.788
24.5
6.2


to

string = "
123.2,
3.6,
6.77,
23.43,
4.78,
7.6,
123.2,
9.6,
1.77,
3.43,
24.78,
76.6,
411.5,
346.5,
975.75,
162.788,
4.5,
16.2,
324.5,
746.5,
开发者_开发百科975.75,
12.788,
24.5,
6.2
";


declare @List varchar(max)

select @List = isnull(@List + ',', '') + cast(ColumnName as varchar)
from MyTable

print @List

For example:

declare @List varchar(max)

select @List = isnull(@List + ',', '') + cast(object_id as varchar)
from sys.objects

print @List

or

select stuff(
    (select ',' + cast(object_id as varchar)
    from sys.objects
    for xml path('')),
    1, 1, '')


I don't think this is the best solution, but I've encountered a similar problem before. Using SQL Server, I just parsed the data into an XML string and then replaced out the actual XML. This turned it into a comma-separated list of values.

REPLACE(
    REPLACE(
        REPLACE(
            (SELECT
                some_field AS [data()]
            FROM
                some_table
            FOR
                XML PATH ('label'))
        ,'</label><label>',',')
    ,'</label>','')
,'<label>','')
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号