SqlQuestion如何转换dd / mm / yyyy hh:mm:ss tt为dd-MM-yyyy hh:mm:ss tt

时间:2013-08-06 06:23:49

标签: sql-server-2008

我将列名abcd设为varchar,因为我将日期时间插入为dd/mm/yyyy hh:mm:ss tt

因此,当我选择提交日期时间时,我希望将日期时间显示为dd-MM-yyyy hh:mm:ss tt 只是

3 个答案:

答案 0 :(得分:0)

如果该列是varchar,您可以使用replace函数将'/'替换为' - '。

select replace(abcd, '/', '-') newdates from table

答案 1 :(得分:0)

您必须使用转换功能。有关详细信息,请参阅此链接http://www.w3schools.com/sql/func_convert.asp

http://technet.microsoft.com/en-us/library/ms187928.aspx

答案 2 :(得分:0)

如果要将varchar转换为正确的日期,则必须先在此处更改DATEFORMAT,否则会失败。

SET DATEFORMAT dmy;
-- then you can convert the string to a datetime
SELECT CONVERT(DATETIME, abcd); 

然后您可以根据需要以所有支持的SQL Server格式输出 - 请参阅此链接上的“日期和时间样式”:http://technet.microsoft.com/en-us/library/ms187928(v=sql.105).aspx