问题描述
我的表中有一列是varchar。我用它以
格式存储日期,例如01/12/2013我想要做的是将列转换为DataType日期时间或日期。我是SQL新手,所以很难用它。存储的所有日期都不同。
Hi,
I have a column in my table that is varchar. I use it to store dates in the format
e.g. 01/12/2013 What I would like to do is convert the column to DataType datetime or date. I'm new to SQL so having hard time with it. All the dates stored are different.
推荐答案
select convert(datetime,columnName) as [DateTime]
select convert(date,columnName) as [Date]
--or
select cast(columnName as datetime) as [DateTime]
select cast(columnName as date) as [Date]
UPDATE tablename SET newColumn = CONVERT(date, oldColumn,103)
(您可能希望之后删除旧列)。您可以使用ALTER TABLE或通过Management Studio执行此操作
3.您可以根据旧表的内容创建一个新表格,例如
(You might want to delete the old column afterwards). You can do this with ALTER TABLE or via Management Studio
3. You could create a new table from the contents of the old one e.g.
SELECT column1, column2, column3, CONVERT(date, oldColumn,103), .. etc
INTO NewTable FROM OldTable
(NB你必须列出其他列)然后删除旧表
如果是我,我可能会选择1,除非你真的有很多数据你不能轻易复制
(NB you must list the other columns) and then drop the old table
If it was me I would probably go with option 1 unless you really have lots of data you can't easily reproduce
这篇关于将varchar转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!