我已将日期作为字符串格式存储在我的数据库中(EX:01-July-2017) 有没有办法在sql server中将其更改为正确的日期格式,因为我必须最终使用Range
Create table xus(Date varchar(40) )
#my input is 01-July-2017
我需要使用Range运算符,因此我需要将其转换为正确的日期格式而不删除数据
答案 0 :(得分:0)
-- Add new date column
ALTER TABLE xus
ADD NewDate DATETIME
-- Migrate data
UPDATE xus
SET NewDate = CONVERT( DATETIME, OldDate )
-- If any errors at this stage, you may need to fix them individually for each problematic record.
-- ISDATE function may help with this
-- Drop current date column (which is VARCHAR)
ALTER TABLE xus
DROP COLUMN OldDate
-- Rename New column to be old column enter code here
EXEC sp_RENAME 'xus.NewDate' , 'OldDate', 'COLUMN'