20

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?

Zolt
  • 2,761
  • 8
  • 43
  • 60
  • You can't dictate the data type for the output column of a view. The best you can do is as @buckley suggested - use CAST or CONVERT to force an explicit data type inline in the view definition. – Aaron Bertrand May 22 '12 at 23:44

3 Answers3

52

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

buckley
  • 13,690
  • 3
  • 53
  • 61
3

If you have updated the type in the table and the view still reflects the old type, simply running an alter view without changing anything will update the type

jimmy
  • 31
  • 1
0

Yes..You can try Convert function to do this.

Convert (Desired datatype,column name)

eg.Convert(varchar(50),dbo.User_master.User_email) where User_email has previous type as nvarchar(MAX).

If you want to convert nvarchar data to datetime then additional parameter is needed to Convert function like

CONVERT(data_type(length),expression,style)

eg. Convert(Datetime,dbo.User_master.User_DOB,103)

more info at SQL Server CONVERT() Function

SK16
  • 623
  • 8
  • 17