3

我怎样才能做oracle相当于:

to_char(date_field, 'YYYY-MM-DD:HH')  # e.g. 2012-05-25:19

在 SQL Server 中?

我想要订购栏,这就是我想要的原因year-month-day-hour

4

2 回答 2

5
select convert(varchar(10),date_field,120) + ':'+
convert(varchar(2), datepart(hour,date_field))
于 2012-06-27T12:56:07.910 回答
3

Unfortunately mssql isn't great at custom date formats. You're stuck with string parsing:

e.g.

select replace(CONVERT(varchar(13),date_field,121),' ',':')

The full details of the formats that are available are here: http://msdn.microsoft.com/en-us/library/ms187928.aspx

Note: If you're lucky enough to be in SQL 2012 you get a new FORMAT function, which is basically a wrapper of the .Net equivalent: See here: http://msdn.microsoft.com/en-us/library/hh213505.aspx

于 2012-06-27T12:59:16.737 回答