views:

118

answers:

1

Hi.

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 ahead and add this computed column to an important table until I know why it has a problem with the formula.

+3  A: 

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

marc_s
+1 Right on, as always :)
Andrew Hare
@Andrew: I'm trying my best, thanks! :-)
marc_s
I don't need it to be persisted (in fact I don't want it to be because I don't want to grow the database). I was trying to add it as a non persistent column (the 'persist' check box unchecked)It's just for convenience really - fed up of pasting data into excel and having it think the date is the 9th of november 2027.
MrVimes
and... won't that alter table query just add a new standard column with the computed value actually stored there? It needs to be computed on the fly. (because the data essentially exists elsewhere in the table so I don't want to duplicate data unnecesarily
MrVimes
As long as you don't have the PERSISTED on it, it's just computed on the fly as needed - it's **NOT** stored anywhere (just the formula is - no values are stored)
marc_s
Ok I went ahead and did it. Seems to have worked fine. Thanks Marc!
MrVimes