转换分隔符
我正在使用 SQL Server Express 2005,我想转换列和小数分隔符:
Use StockDataFromSella;
DECLARE @D1 DateTime
DECLARE @D2 DateTime
DECLARE @Interval FLOAT
SET @D1 = '2009-09-21T09:00:00.000'
SET @D2 = '2010-10-20T17:30:00.000'
SET @Interval = 15
;WITH
L0 AS (SELECT 1 AS c UNION ALL SELECT 1),
L1 AS (SELECT 1 AS c FROM L0 A CROSS JOIN L0 B),
L2 AS (SELECT 1 AS c FROM L1 A CROSS JOIN L1 B),
L3 AS (SELECT 1 AS c FROM L2 A CROSS JOIN L2 B),
L4 AS (SELECT 1 AS c FROM L3 A CROSS JOIN L3 B),
Nums AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS i FROM L4),
Ranges AS(
SELECT
DATEADD(MINUTE,@Interval*(i-1),@D1) AS StartRange,
DATEADD(MINUTE,@Interval*i,@D1) AS NextRange
FROM Nums where i <= 1+CEILING(DATEDIFF(MINUTE,@D1,@D2)/@Interval))
,cte AS (
SELECT
*
,ROW_NUMBER() OVER (PARTITION BY Simbolo,r.StartRange ORDER BY [DataOra]) AS RN_ASC
,ROW_NUMBER() OVER (PARTITION BY Simbolo,r.StartRange ORDER BY [DataOra] DESC) AS RN_DESC
FROM Ranges r
JOIN dbo.tbl1MinENI p ON p.[DataOra] >= r.StartRange and p.[DataOra] < r.NextRange )
SELECT
CONVERT(VARCHAR, MAX(CASE WHEN RN_ASC=1 THEN [DataOra] END), 103) AS DataOraDate,
CONVERT(CHAR(5), MAX(CASE WHEN RN_ASC=1 THEN [DataOra] END), 108) AS DataOraTime,
MAX(CASE WHEN RN_ASC=1 THEN [DataOra] END) AS DataOra,
MAX(CASE WHEN RN_ASC=1 THEN [Apertura] END) AS [Apertura],
MAX(Massimo) Massimo,
MIN(Minimo) Minimo,
MAX(CASE WHEN RN_DESC=1 THEN [Chiusura] END) AS [Chiusura],
SUM(Volume) Volume
/*MAX(CASE WHEN RN_DESC=1 THEN [DataOra] END) AS ChiusuraDataOra*/
FROM cte
GROUP BY Simbolo,StartRange
ORDER BY DataOra
执行查询时,复制并粘贴到 txt 文件中将给出“;”作为列分隔符,而我需要 ',',并且我在需要 '.' 的地方得到 ',' 作为小数。
还有一种方法可以删除 DataOraTime 列中的“:”吗?
谢谢!阿尔贝托
I'm using SQL Server Express 2005 and I would like to convert columns and decimal delimiters:
Use StockDataFromSella;
DECLARE @D1 DateTime
DECLARE @D2 DateTime
DECLARE @Interval FLOAT
SET @D1 = '2009-09-21T09:00:00.000'
SET @D2 = '2010-10-20T17:30:00.000'
SET @Interval = 15
;WITH
L0 AS (SELECT 1 AS c UNION ALL SELECT 1),
L1 AS (SELECT 1 AS c FROM L0 A CROSS JOIN L0 B),
L2 AS (SELECT 1 AS c FROM L1 A CROSS JOIN L1 B),
L3 AS (SELECT 1 AS c FROM L2 A CROSS JOIN L2 B),
L4 AS (SELECT 1 AS c FROM L3 A CROSS JOIN L3 B),
Nums AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS i FROM L4),
Ranges AS(
SELECT
DATEADD(MINUTE,@Interval*(i-1),@D1) AS StartRange,
DATEADD(MINUTE,@Interval*i,@D1) AS NextRange
FROM Nums where i <= 1+CEILING(DATEDIFF(MINUTE,@D1,@D2)/@Interval))
,cte AS (
SELECT
*
,ROW_NUMBER() OVER (PARTITION BY Simbolo,r.StartRange ORDER BY [DataOra]) AS RN_ASC
,ROW_NUMBER() OVER (PARTITION BY Simbolo,r.StartRange ORDER BY [DataOra] DESC) AS RN_DESC
FROM Ranges r
JOIN dbo.tbl1MinENI p ON p.[DataOra] >= r.StartRange and p.[DataOra] < r.NextRange )
SELECT
CONVERT(VARCHAR, MAX(CASE WHEN RN_ASC=1 THEN [DataOra] END), 103) AS DataOraDate,
CONVERT(CHAR(5), MAX(CASE WHEN RN_ASC=1 THEN [DataOra] END), 108) AS DataOraTime,
MAX(CASE WHEN RN_ASC=1 THEN [DataOra] END) AS DataOra,
MAX(CASE WHEN RN_ASC=1 THEN [Apertura] END) AS [Apertura],
MAX(Massimo) Massimo,
MIN(Minimo) Minimo,
MAX(CASE WHEN RN_DESC=1 THEN [Chiusura] END) AS [Chiusura],
SUM(Volume) Volume
/*MAX(CASE WHEN RN_DESC=1 THEN [DataOra] END) AS ChiusuraDataOra*/
FROM cte
GROUP BY Simbolo,StartRange
ORDER BY DataOra
When execute the query, copy and paste in a txt file will give ';' as column separator while I need ',' and I get ',' for decimals where I need '.'
Is there also a way to remove ':' in DataOraTime column?
Thanks! Alberto
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
结果的复制和粘贴与查询无关,而是与执行查询的程序有关,因为该程序负责将结果格式化为可以复制到剪贴板的格式。
要更改日期和时间格式,您很可能会在执行语句之前更改一些会话变量。
Copy and paste of the result has nothing to do with the query but with the program you execute the query in, because that program is responsible for formatting the result into a format that can be copied to the clipboard.
To change the date and time format you will most likely change some session variables before executing the statement.