I have a view with a field for date as a varchar. I need to change it to date time using the following code

    CONVERT(DATETIME, MyDates)

This works fine when executing the view but I want to make the change permanent. I need some help with the syntax. So far I have

    ALTER VIEW tableName
    CONVERT(DATETIME, MyDates)

but it's obviously not working

有帮助吗?

解决方案 2

A view only fetches the data from the table as per the query.So you cannot change the datatype of the view. you have to change it in table.

其他提示

Since a view (unless it's a materialized/indexed view which has some extra peculiarities) is more or less just a stored select query, what you do is to just change the select query and alter the view using that.

For example, if you have the view;

CREATE VIEW testview AS 
  SELECT id, value FROM test;

...where value is a varchar and you want it to be reflected in the view as a datetime, you can just issue;

ALTER VIEW testview AS
  SELECT id, CAST(value AS DATETIME) value FROM test; 

...to make it appear as a datetime in the view.

An SQLfiddle with a simple demo.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top