문제

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