开发者

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.

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开发者_JS百科.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?


The TSQL equivalent of:

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.

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...

Reference:

  • CAST/CONVERT


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:

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?

datepart(minute, rowdate)

This returns just one number: the minute part of the datetime field. Very easy to group by.

Otherwise use CONVERT() or CAST() to convert between character types. 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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜