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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10711473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 08:33:26  来源:igfitidea点击:

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

databasesql-server-2008tsqlviewsqldatatypes

提问by Zolt

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。但是对视图执行相同操作的语法是什么?或者这甚至可能吗?

回答by buckley

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

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

回答by SK16

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

例如。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 数据转换为日期时间,则需要其他参数来转换函数,例如

CONVERT(data_type(length),expression,style)

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

例如。 Convert(Datetime,dbo.User_master.User_DOB,103)

more info at SQL Server CONVERT() Function

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