2017-06-27 125 views
2

我不確定如何在腳本中使用CAST或CONVERT。
SQL說:轉換VARCHAR值時, '神奇' 數據 類型爲intSQL,CONVERT,CAST數據類型?

轉換失敗。

有人能告訴我該怎麼做嗎?

USE AdventureWorks2012 
GO 

DECLARE @Number01 AS INT 
DECLARE @Number02 AS INT 
DECLARE @Number03 AS INT 
DECLARE @Number04 AS INT 
DECLARE @Number05 AS INT 
DECLARE @Number06 AS INT 
DECLARE @Number07 AS INT 
DECLARE @Text01 AS VARCHAR (15) 


SET @Number01 = 150 
SET @Number02 = 200 
SET @Number03 = 350 
SET @Number04 = 450 
SET @Number05 = 550 
SET @Number06 = 650 
SET @Number07 = 800 
SET @Text01 = 'Amazing' 


SELECT A.ProductID ,A.Name ,A.ProductModelID ,A.ProductNumber ,A.MakeFlag ,A.Color ,A.SafetyStockLevel 
       ,A.StandardCost ,A.ListPrice,A.DaysToManufacture ,A.SellEndDate ,A.ModifiedDate ,A.ListPrice 
       ,B.Name ,B.ListPrice ,B.Adjusted_List_Price ,C.ProductDescriptionID ,C.Description 
       ,C.ModifiedDate ,D.ProductID ,D.StartDate ,D.EndDate ,D.ListPrice ,D.ModifiedDate, E.Name 
       ,E.CatalogDescription,E.ModifiedDate ,F.ReferenceOrderID ,F.TransactionDate 
       ,F.TransactionID ,F.Quantity 
       ,IIF(A.ListPrice >[email protected],CAST((@Number01 *@Number02 + @Number03) AS nvarchar(50)), 100 + @Number01) AS [Test90] 
       ,IIF(A.ListPrice >[email protected],CAST((@Number01 *@Number02 + @Number03) AS nvarchar(50)), 'GG') AS [Test91] 
       ,PATINDEX('%M94B%', A.ProductNumber) AS [Test92] 
       ,PATINDEX('%M63S%', A.ProductNumber) AS [Test94] 
       ,CASE 
       WHEN A.ProductNumber LIKE '%M94B%' THEN PATINDEX('%M94B%', A.ProductNumber) * 2 
       WHEN A.ProductNumber LIKE '%M63S%' THEN 5 * @Number01 
       WHEN A.ProductNumber LIKE '%T98U%' THEN @Text01 
      END AS [Test95] 

FROM [Production].[Product] AS A 
INNER JOIN [Production].[Product_2] AS B 
ON A.Name = B.Name 

INNER JOIN [Production].[ProductDescription] AS C 
ON A.ProductID = C.ProductDescriptionID 

INNER JOIN [Production].[ProductListPriceHistory] AS D 
ON A.ProductID = D.ProductID 

INNER JOIN [Production].[ProductModel] AS E 
ON A.ProductModelID = E.ProductModelID 

FULL JOIN [Production].[TransactionHistory] AS F 
ON A.ProductID = F.ProductID 

WHERE A.ProductModelID IS NOT NULL 
AND A.Color IS NOT NULL AND F.Quantity IS NOT NULL 

回答

1

在你的樣品中,CASE語句假定每個WHEN子句返回的數據類型是INT,因爲第一WHEN..THEN回報number

SQL Server認爲在第3個WHEN..THEN,結果將是INT像第一個和第二個。

您正在通過在第3個WHEN..THEN中返回varchar數據類型來破壞SQL Server的假設,這是不對的。 Amazing不可轉換爲INT

CASE語句中的所有路徑應返回相同的數據類型。

+0

能否請您給我的這個變化會做一個例子,腳本的方面嗎? – Shrikesh

+0

其他答案應該適合你。只需將其他2個路徑轉換爲返回varchar而不是數字 – FLICKER

2

試試這個

USE AdventureWorks2012 
GO 

DECLARE @Number01 AS INT 
DECLARE @Number02 AS INT 
DECLARE @Number03 AS INT 
DECLARE @Number04 AS INT 
DECLARE @Number05 AS INT 
DECLARE @Number06 AS INT 
DECLARE @Number07 AS INT 
DECLARE @Text01 AS VARCHAR (15) 


SET @Number01 = 150 
SET @Number02 = 200 
SET @Number03 = 350 
SET @Number04 = 450 
SET @Number05 = 550 
SET @Number06 = 650 
SET @Number07 = 800 
SET @Text01 = 'Amazing' 


SELECT A.ProductID ,A.Name ,A.ProductModelID ,A.ProductNumber ,A.MakeFlag ,A.Color ,A.SafetyStockLevel 
       ,A.StandardCost ,A.ListPrice,A.DaysToManufacture ,A.SellEndDate ,A.ModifiedDate ,A.ListPrice 
       ,B.Name ,B.ListPrice ,B.Adjusted_List_Price ,C.ProductDescriptionID ,C.Description 
       ,C.ModifiedDate ,D.ProductID ,D.StartDate ,D.EndDate ,D.ListPrice ,D.ModifiedDate, E.Name 
       ,E.CatalogDescription,E.ModifiedDate ,F.ReferenceOrderID ,F.TransactionDate 
       ,F.TransactionID ,F.Quantity 
       ,IIF(A.ListPrice >[email protected],CAST((@Number01 *@Number02 + @Number03) AS nvarchar(50)), 100 + @Number01) AS [Test90] 
       ,IIF(A.ListPrice >[email protected],CAST((@Number01 *@Number02 + @Number03) AS nvarchar(50)), 'GG') AS [Test91] 
       ,PATINDEX('%M94B%', A.ProductNumber) AS [Test92] 
       ,PATINDEX('%M63S%', A.ProductNumber) AS [Test94] 
       ,CASE 
       WHEN A.ProductNumber LIKE '%M94B%' THEN cast(PATINDEX('%M94B%', A.ProductNumber) * 2 as varchar(100)) 
       WHEN A.ProductNumber LIKE '%M63S%' THEN cast(5 * @Number01 as varchar(100)) 
       WHEN A.ProductNumber LIKE '%T98U%' THEN @Text01 
      END AS [Test95] 

FROM [Production].[Product] AS A 
INNER JOIN [Production].[Product_2] AS B 
ON A.Name = B.Name 

INNER JOIN [Production].[ProductDescription] AS C 
ON A.ProductID = C.ProductDescriptionID 

INNER JOIN [Production].[ProductListPriceHistory] AS D 
ON A.ProductID = D.ProductID 

INNER JOIN [Production].[ProductModel] AS E 
ON A.ProductModelID = E.ProductModelID 

FULL JOIN [Production].[TransactionHistory] AS F 
ON A.ProductID = F.ProductID 

WHERE A.ProductModelID IS NOT NULL 
AND A.Color IS NOT NULL AND F.Quantity IS NOT NULL