使用sql以24小时格式选择表的DateTime列

时间:2012-10-05 11:57:49

标签: c# sql sql-server sql-server-2008

我的表格中包含ReceivedOn类型的列DateTime。我正在使用select语句执行存储过程,如下所示:

Select 
    CONVERT(DateTime,ReceivedOn)[Date/Time], MessageText[Event] 
From 
    RawFeed 
Where 
    ATM = (Select Code From ATM Where ATM = @ATMID) 
    AND ReceivedOn Between @From And @To 
Order By 
    ReceivedOn Desc 

我将存储过程结果存储在DataTable对象中。并在我的.aspx页面上显示此对象

dtReturn = sspObj.ExecuteDataTable();
return dtReturn ;

DataTable对象使用函数将表结果存储在表格对象中并将其添加到页面

tOutput = Generix.convertDataTable2HTMLTable(dtOutput, true, true, false);
Page.Controls.Add(tOutput);

但不是将值显示为

2012-10-05 16:40:35.234

我得到这样的输出:

2012-10-05 04:40:35 PM

3 个答案:

答案 0 :(得分:2)

请勿将其转换,保持原样。

Select ReceivedOn, ...

答案 1 :(得分:2)

这不是SQL问题; SQL正在返回一个日期时间,它将被C#格式化为您正在看到的输出。如果希望SQL Server返回表示格式中日期的varchar(字符串),请尝试:

SELECT CONVERT(varchar(24), ReceivedOn, 121)

有关详细信息,请参阅Cast and Convert

答案 2 :(得分:0)

select 
CONVERT(varchar,CONVERT(Date,ReceivedOn,101))  + ' ' + 
convert(varchar,CONVERT(TIME,ReceivedOn)) AS ReceivedDate,
CONVERT(DateTime,ReceivedOn)[Date/Time], MessageText[Event] 
From 
RawFeed 
Where 
ATM = (Select Code From ATM Where ATM = @ATMID) 
AND ReceivedOn Between @From And @To 
Order By 
ReceivedOn Desc 

试试这可能会对你有帮助....