开发者

Trying to convert "yy/mm/dd hh:mm:ss" (stored as char) into datetime using computed column

This seems like it should be simple but it's driving me up the wall.

I have two columns - 'tx_date' and 'time' stored each as char(10). (bad database design I know, but wasn't my design)

From a query I can convert them into a datetime just fine -

"...convert(datetime,tx_date,time,11)..."

(so tx_date "09/11/27" and time "07:12:18" will produce "2009-11-27 07:12:18.000")

But if I then copy and paste the convert into the 'formula' field in SQL Server Management Studio (the same place I tested it works in a query) it tells me "Error validating the formula for column.."

If I force it to use that formula anyway it works, but I don't want to go ah开发者_如何学Goead and add this computed column to an important table until I know why it has a problem with the formula.


You can add the computed field to the table in SQL Server Management Studio using a query no problem:

ALTER TABLE dbo.YourTable 
  ADD NewDateTimeField AS CONVERT(DATETIME, tx_date + ' ' + time, 11) 

but unfortunately, you cannot make it "PERSISTED" since the CONVERT function in non-deterministic. This means it'll be examined each time it's accessed, and you cannot put an index on it :-(

Marc

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜