是否可以更改视图中列的数据类型?

时间:2022-02-15 17:14:16

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?

结果是表中的字段转换为nvarchar。但是为视图做同样的事情的语法是什么?或者甚至可能吗?

2 个解决方案

#1


23  

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

因此,请查看CAST以及msdn页面上的CONVERT以获取完整信息

#2


-1  

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).

eg.Convert(varchar(50),dbo.User_master.User_email)其中User_email的前一个类型为nvarchar(MAX)。

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

如果要将nvarchar数据转换为datetime,则转换函数需要附加参数

CONVERT(data_type(length),expression,style)

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

例如。转换(日期时间,dbo.User_master.User_DOB,103)

more info at SQL Server CONVERT() Function

SQL Server CONVERT()函数的更多信息

#1


23  

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

因此,请查看CAST以及msdn页面上的CONVERT以获取完整信息

#2


-1  

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).

eg.Convert(varchar(50),dbo.User_master.User_email)其中User_email的前一个类型为nvarchar(MAX)。

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

如果要将nvarchar数据转换为datetime,则转换函数需要附加参数

CONVERT(data_type(length),expression,style)

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

例如。转换(日期时间,dbo.User_master.User_DOB,103)

more info at SQL Server CONVERT() Function

SQL Server CONVERT()函数的更多信息