英文:
Change data type in a SQL Server view
问题
我有一个名为dbo.NpsResultsView
的视图。
这个视图的代码如下:
SELECT
dbo.CRMDeals.dealid,
dbo.CRMDeals.contact,
dbo.CRMContacts.title,
dbo.CRMContacts.firstname,
dbo.CRMContacts.lastname,
dbo.CRMContacts.dealCustomFieldId,
CAST(dbo.CRMCustomFieldsData.fieldValue AS INT) AS fieldValue,
dbo.CRMCustomFieldsData.updatedTimestamp,
dbo.ClientsMigrationDataset.Groups AS Expr1,
dbo.ClientsMigrationDataset.Client,
dbo.ClientsMigrationDataset.Email,
dbo.ClientsMigrationDataset.ClientManager
FROM
dbo.CRMDeals
INNER JOIN
dbo.CRMContacts ON dbo.CRMContacts.contactid = dbo.CRMDeals.contact
INNER JOIN
dbo.CRMDealCustomFieldsData ON dbo.CRMDealCustomFieldsData.dealId = dbo.CRMDeals.dealid
INNER JOIN
dbo.ClientsMigrationDataset ON dbo.ClientsMigrationDataset.Email = dbo.CRMContacts.email
WHERE
(dbo.CRMDealCustomFieldsData.dealCustomFieldId = '15')
在这个视图中,我有一个名为fieldValue
的列,它被返回为VarChar
,但它需要是int
,因为它是一个10分制的分数。
注意:使用SQL Server Management Studio。
英文:
I have a view called dbo.NpsResultsView
.
The code for this view is:
SELECT
dbo.CRMDeals.dealid,
dbo.CRMDeals.contact,
dbo.CRMContacts.title,
dbo.CRMContacts.firstname,
dbo.CRMContacts.lastname,
dbo.CRMContacts.dealCustomFieldId,
dbo.CRMCustomFieldsData.fieldValue,
dbo.CRMCustomFieldsData.updatedTimestamp,
dbo.ClientsMigrationDataset.Groups AS Expr1,
dbo.ClientsMigrationDataset.Client,
dbo.ClientsMigrationDataset.Email,
dbo.ClientsMigrationDataset.ClientManager
FROM
dbo.CRMDeals
INNER JOIN
dbo.CRMContacts ON dbo.CRMContacts.contactid = dbo.CRMDeals.contact
INNER JOIN
dbo.CRMDealCustomFieldsData ON dbo.CRMDealCustomFieldsData.dealId = dbo.CRMDeals.dealid
INNER JOIN
dbo.ClientsMigrationDataset ON dbo.ClientsMigrationDataset.Email = dbo.CRMContacts.email
WHERE
(dbo.CRMDealCustomFieldsData.dealCustomFieldId = '15')
In that view, I have a column called fieldValue
which is returned as VarChar
, and this needs to be int
as it's a score out of 10.
Note: using SQL Server Management Studio.
答案1
得分: 2
使用CAST(fieldValue AS INT)
在你的视图定义中 - 或者如果你希望在fieldValue
中的值不是真正的INT
时避免异常,也可以使用TRY_CAST(fieldValue AS INT)
。然后,任何“不合适”的值都将被替换为NULL
,而不是引发异常。
尝试这样做:
CREATE VIEW dbo.NpsResultsView
AS
SELECT
dbo.CRMDeals.dealid,
dbo.CRMDeals.contact,
dbo.CRMContacts.title,
dbo.CRMContacts.firstname,
dbo.CRMContacts.lastname,
dbo.CRMContacts.dealCustomFieldId,
TRY_CAST(dbo.CRMCustomFieldsData.fieldValue AS INT) AS 'fieldValueInt',
dbo.CRMCustomFieldsData.updatedTimestamp,
dbo.ClientsMigrationDataset.Groups AS Expr1,
dbo.ClientsMigrationDataset.Client,
dbo.ClientsMigrationDataset.Email,
dbo.ClientsMigrationDataset.ClientManager
FROM
dbo.CRMDeals
INNER JOIN
dbo.CRMContacts ON dbo.CRMContacts.contactid = dbo.CRMDeals.contact
INNER JOIN
dbo.CRMDealCustomFieldsData ON dbo.CRMDealCustomFieldsData.dealId = dbo.CRMDeals.dealid
INNER JOIN
dbo.ClientsMigrationDataset ON dbo.ClientsMigrationDataset.Email = dbo.CRMContacts.email
WHERE
(dbo.CRMDealCustomFieldsData.dealCustomFieldId = '15')
英文:
Use CAST(fieldValue AS INT)
in your view definition - or if you want to avoid an exception when a value in fieldValue
isn't really an INT
, maybe use TRY_CAST(fieldValue AS INT)
. Then any "improper" value will be replaced by NULL
instead of throwing an exception.
Try this:
CREATE VIEW dbo.NpsResultsView
AS
SELECT
dbo.CRMDeals.dealid,
dbo.CRMDeals.contact,
dbo.CRMContacts.title,
dbo.CRMContacts.firstname,
dbo.CRMContacts.lastname,
dbo.CRMContacts.dealCustomFieldId,
TRY_CAST(dbo.CRMCustomFieldsData.fieldValue AS INT) AS 'fieldValueInt',
dbo.CRMCustomFieldsData.updatedTimestamp,
dbo.ClientsMigrationDataset.Groups AS Expr1,
dbo.ClientsMigrationDataset.Client,
dbo.ClientsMigrationDataset.Email,
dbo.ClientsMigrationDataset.ClientManager
FROM
dbo.CRMDeals
INNER JOIN
dbo.CRMContacts ON dbo.CRMContacts.contactid = dbo.CRMDeals.contact
INNER JOIN
dbo.CRMDealCustomFieldsData ON dbo.CRMDealCustomFieldsData.dealId = dbo.CRMDeals.dealid
INNER JOIN
dbo.ClientsMigrationDataset ON dbo.ClientsMigrationDataset.Email = dbo.CRMContacts.email
WHERE
(dbo.CRMDealCustomFieldsData.dealCustomFieldId = '15')
答案2
得分: 0
请注意,您不能使用此命令更改视图的定义。要更改视图定义,您必须删除视图,然后重新创建它。
CREATE VIEW AView
AS
SELECT CAST(title AS char(50))
FROM titles
您可以使用 CAST 和 CONVERT 函数来更改数据类型。
英文:
Note that you cannot use this command to change the definition for a view. To change the view definition, you must drop the view and then recreate it.
CREATE VIEW AView
AS
SELECT CAST(title AS char(50))
FROM titles
You can use cast and covert function to change a datatype
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论