Is it possible to change the datatype of a column

2019-02-08 05:09发布

问题:

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?

回答1:

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



回答2:

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