标准化SQL表中的列

时间:2014-03-31 14:00:03

标签: sql sql-server datetime

我的SQL表中有一个名为'Dates'的列,其中日期以不同的格式存储,例如: 25/5/2000, 2013年6月1日, 1996年5月1日, 1990年1月23日等。

我想将所有这些日期转换为单一格式,例如DD / MM / YYYY或MM / DD / YYYY。我怎么能这样做?

3 个答案:

答案 0 :(得分:0)

使用DATE数据类型作为存储类型,并将格式保留在前端。

答案 1 :(得分:0)

试试这个!

create table Table(DateColumn varchar(20))

insert into Table values ('25/5/2000'), ('25052000'), ('5/1/1996'),('jan-23-1990'),(05012000)

select
  case len(DateColumn)
    when 10 then convert(date, DateColumn, 103)
    when 8 then convert(date, stuff(stuff(DateColumn, 5, 0, '/'), 3, 0, '/'), 103)
    when 6 then convert(date, stuff(stuff(DateColumn, 5, 0, '/'), 3, 0, '/'), 3)
    when 11 then convert(date,Datecolumn,103)
-- depending on string length you may query this   
end
from table

答案 2 :(得分:0)

如果您尝试在列中转换日期,请使用此示例网站了解您要完成的任务,希望这有助于感谢。 http://www.codeproject.com/Articles/576178/cast-convert-format-try-parse-date-and-time-sql