Is it possible to change the datatype of a column in a view?

Zolt picture Zolt · May 23, 2012 · Viewed 67.9k times · Source

Usually I run a script like this:

ALTER TABLE [TABLE]
ALTER COLUMN [Column] NVARCHAR(40);

The result is that the field in the table gets converted to nvarchar. But what is the syntax for doing the same thing for a view? Or is that even possible?

Answer

buckley picture buckley · May 23, 2012

Sure

CREATE VIEW AView
AS
SELECT CAST(title AS char(50))
FROM titles

So check out CAST and also CONVERT on the msdn pages for full info