简体   繁体   English

什么是等效于tchar / sql server 2008的to-char

[英]what is the to-char equivalent tsql/sql server 2008

I am used to oracle and now been thrown T-SQL, I am doing a course shortly but can you help out until then. 我曾经使用过oracle,现在又被抛弃了T-SQL,我很快就会在做一门课程,但是到那时你能帮忙吗。

I have a list that I need to group in minutes. 我有一个列表,需要在几分钟内进行分组。

Sample of rowdate data 行日期数据样本

SELECT ROWDATE,count(rowdate)
FROM [mydb].[dbo].[mytable]
GROUP BY ROWDATE
order by 1

2010-08-16 15:01:18.110 1
2010-08-16 15:01:18.203 1
2010-08-16 15:01:18.377 1
2010-08-16 15:01:18.453 1
2010-08-16 15:01:18.530 1
2010-08-16 15:01:18.610 1
2010-08-16 15:01:18.703 1
2010-08-16 15:01:18.813 1
2010-08-16 15:01:18.953 1
2010-08-16 15:01:19.173 1
2010-08-16 15:01:19.360 1
2010-08-16 15:01:19.483 1
2010-08-16 15:01:19.593 1
2010-08-16 15:01:19.673 1
2010-08-16 15:01:19.733 1
2010-08-16 15:01:19.813 1
2010-08-16 15:01:19.890 1
2010-08-16 15:01:19.970 1
2010-08-16 15:01:20.047 1

I just want to group by mins. 我只想按分钟分组。

SELECT to_char(rowdate,'dd/MM/yyyy HH24:MI'),count(rowdate)
FROM mytable
GROUP BY to_char(rowdate,'dd/MM/yyyy HH24:MI')
order by 1

On sql server(T-SQL) what would be the equivalent script? 在sql server(T-SQL)上,等效脚本是什么?

The TSQL equivalent of: TSQL等效于:

TO_CHAR(rowdate,'dd/MM/yyyy HH24:MI')

...is: ...是:

CONVERT(VARCHAR(16), rowdate, 20)

The expression "20" returns: yyyy-mm-dd hh:mi:ss(24h) - which is VARCHAR(19), so cutting that down to VARCHAR(16) omits the seconds. 表达式“ 20”返回: yyyy-mm-dd hh:mi:ss(24h) -这是VARCHAR(19),因此将其减少到VARCHAR(16)会忽略秒数。

DATETIME formatting is a real pain in TSQL - with SQL Server 2005, the only real way to get customizable formatting is to use a CLR function so in C# you could use the DateTime.ToString method... DATETIME格式在TSQL中是一个真正的难题-使用SQL Server 2005,获得可自定义格式的唯一真正方法是使用CLR函数,因此在C#中可以使用DateTime.ToString方法...

Reference: 参考:

That'd be 那会是

convert(varchar, rowdate, 100)

The format is not exactly the same, but the important thing, it's up to a minute, too. 格式不完全相同,但重要的是,它也要长达一分钟。


EDIT: 编辑:

Alternatively, you can avoid conversion to varchar at all: 另外,您完全可以避免转换为varchar:

group by dateadd(ms, -datepart(ms, rowdate) , dateadd(s, -datepart(s, rowdate), rowdate))

to floor the datetime down to the minute use (which is better than using string manipulations): 将日期时间降低到分钟的使用时间(比使用字符串操作更好):

DATEADD(minute,DATEDIFF(minute,0,datetime),0)

so, to group by minutes, it would be: 因此,按分钟分组,将是:

SELECT 
    DATEADD(minute,DATEDIFF(minute,0,ROWDATE),0)
    ,count(rowdate)
FROM [mydb].[dbo].[mytable]
GROUP BY DATEADD(minute,DATEDIFF(minute,0,ROWDATE),0)
order by 1

Is there a specific reason you want a conversion to (var)char? 您是否有特定原因想要转换为(var)char?

datepart(minute, rowdate)

This returns just one number: the minute part of the datetime field. 它仅返回一个数字:datetime字段的分钟部分。 Very easy to group by. 分组非常容易。

Otherwise use CONVERT() or CAST() to convert between character types. 否则,请使用CONVERT()或CAST()在字符类型之间进行转换。 You'll also want to make sure you use the right format. 您还需要确保使用正确的格式。 There's a little bit more about that here: http://www.mssqltips.com/tip.asp?tip=1145 这里还有更多相关信息: http : //www.mssqltips.com/tip.asp?tip=1145

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM