2009-06-04 91 views
74

我有以下功能如何連接數字和字符串以格式化T-SQL中的數字?

ALTER FUNCTION [dbo].[ActualWeightDIMS] 
(
    -- Add the parameters for the function here 
    @ActualWeight int, 
    @Actual_Dims_Lenght int, 
    @Actual_Dims_Width int, 
    @Actual_Dims_Height int 
) 
RETURNS varchar(50) 
AS 
BEGIN 

DECLARE @ActualWeightDIMS varchar(50); 
--Actual Weight 
    IF (@ActualWeight is not null) 
      SET @ActualWeightDIMS = @ActualWeight; 
--Actual DIMS 
    IF (@Actual_Dims_Lenght is not null) AND 
      (@Actual_Dims_Width is not null) AND (@Actual_Dims_Height is not null) 
      SET @ActualWeightDIMS= @Actual_Dims_Lenght + 'x' + @Actual_Dims_Width + 'x' + @Actual_Dims_Height; 


    RETURN(@ActualWeightDIMS); 

END 

但是當我試圖使用它,我得到了下面的錯誤「轉換爲varchar值‘X’到的數據類型爲int時轉換失敗。」當我使用下面的select語句

select 
BA_Adjustment_Detail.ID_Number [ID_Number], 
BA_Adjustment_Detail.Submit_Date [Submit_Date], 
BA_Category.Category [category], 
BA_Type_Of_Request.Request [Type_Of_Request], 
dbo.ActualWeightDIMS(BA_Adjustment_Detail.ActualWeight,BA_Adjustment_Detail.Actual_Dims_Lenght,BA_Adjustment_Detail.Actual_Dims_Width,BA_Adjustment_Detail.Actual_Dims_Height) [Actual Weight/DIMS], 
BA_Adjustment_Detail.Notes [Notes], 
BA_Adjustment_Detail.UPSCustomerNo [UPSNo], 
BA_Adjustment_Detail.TrackingNo [AirbillNo], 
BA_Adjustment_Detail.StoreNo [StoreNo], 
BA_Adjustment_Detail.Download_Date [Download_Date], 
BA_Adjustment_Detail.Shipment_Date[ShipmentDate], 
BA_Adjustment_Detail.FranchiseNo [FranchiseNo], 
BA_Adjustment_Detail.CustomerNo [CustomerNo], 
BA_Adjustment_Detail.BillTo [BillTo], 
BA_Adjustment_Detail.Adjustment_Amount_Requested [Adjustment_Amount_Requested] 
from BA_Adjustment_Detail 
inner join BA_Category 
on BA_Category.ID = BA_Adjustment_Detail.CategoryID 
inner join BA_Type_Of_Request 
on BA_Type_Of_Request.ID = BA_Adjustment_Detail.TypeOfRequestID 

我想要做的是,如果ActualWeight不爲空則返回ActualWeight爲「實際重量/ DIMS」或者使用的Actual_Dims_Lenght,寬度和高度。

如果是DIMS,那麼我想格式化輸出爲LenghtxWidhtxHeight(15x10x4)。 ActualWeight,Adcutal_Dims_Lenght,Width和Height都是int(整數)值,但「Actual Weight/DIMS」的輸出應該是varchar(50)。

我在哪裏弄錯了?

感謝

編輯:用戶只能挑重量或DIMS ASP.net頁面上,如果用戶選擇DIMS那麼他們必須提供長度,寬度和高度。否則它會在ASP.net頁面上顯示錯誤。我應該擔心在SQL方面呢?

回答

166

一對夫婦的快速說明:

  • 它在您的查詢「長度」不「lenght」
  • 表的別名可能會使其有更多可讀現在

到問題...

在嘗試連接它們之前,您需要顯式地將參數轉換爲VARCHAR。當SQL Server看到@my_int +'X'時,它認爲你試圖將數字「X」添加到@my_int,並且它不能這樣做。相反,嘗試:

SET @ActualWeightDIMS = 
    CAST(@Actual_Dims_Lenght AS VARCHAR(16)) + 'x' + 
    CAST(@Actual_Dims_Width AS VARCHAR(16)) + 'x' + 
    CAST(@Actual_Dims_Height AS VARCHAR(16)) 
+6

您應該始終指定varchar的長度:http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/09/bad-habits-to-kick-declaring-varchar-without-length.aspx – 2012-09-24 04:25:02

2

先將整數轉換爲varchar!

+0

沒錯。字符串'x'完全影響了無意義的int decls; UDF中沒有算術。 – bvj 2014-07-18 06:25:50

5

更改此:

SET @ActualWeightDIMS= @Actual_Dims_Lenght + 'x' + 
    @Actual_Dims_Width + 'x' + @Actual_Dims_Height; 

要這樣:

SET @ActualWeightDIMS= CAST(@Actual_Dims_Lenght as varchar(3)) + 'x' + 
    CAST(@Actual_Dims_Width as varchar(3)) + 'x' + 
    CAST(@Actual_Dims_Height as varchar(3)); 

更改此:

SET @ActualWeightDIMS = @ActualWeight; 

要這樣:

SET @ActualWeightDIMS = CAST(@ActualWeight as varchar(50)); 

您需要使用CAST。全面瞭解CAST和CONVERT here,因爲數據類型很重要!

4

當試圖將它們連接成一個varchar時,你必須將你的整數作爲字符串進行轉換。

SELECT @ActualWeightDIMS = CAST(@Actual_Dims_Lenght AS varchar(10)) 
           + 'x' + 
          CAST(@Actual_Dims_Width as varchar(10)) 
          + 'x' + CAST(@Actual_Dims_Height as varchar(10)); 

在SQL Server 2008中,您可以使用STR功能:

SELECT @ActualWeightDIMS = STR(@Actual_Dims_Lenght) 
           + 'x' + STR(@Actual_Dims_Width) 
           + 'x' + STR(@Actual_Dims_Height); 
+1

默認情況下,STR功能會將數字填充爲10個字符,因此會增加空間負荷。例如`STR(1)`給了我9個空格,然後是`1`。 CAST實際上效果更好。 – 2012-11-05 11:05:45

2

你需要轉換你的數值數據爲字符串你做字符串連接之前,因此,例如使用而不僅僅是@Actual_Dims_Lenght,& c。

0

嘗試鑄造整數爲varchar,將其添加到字符串之前:

​​
1

我試過下面的查詢它的作品對我來說正好

with cte as(

    select ROW_NUMBER() over (order by repairid) as'RN', [RepairProductId] from [Ws_RepairList] 
) 
    update CTE set [RepairProductId]= ISNULL([RepairProductId]+convert(nvarchar(10),RN),0) from cte 
29

如果你正在使用SQL Server 2012+你可以使用CONCAT函數,我們不必做任何明確的轉換

SET @ActualWeightDIMS = Concat(@Actual_Dims_Lenght, 'x', @Actual_Dims_Width, 'x' 
         , @Actual_Dims_Height) 
1

還有,你可能會用科學的號碼,當您轉換整型STR最後的機會......更安全的方法是

SET @ActualWeightDIMS = STR(@Actual_Dims_Width); OR Select STR(@Actual_Dims_Width) + str(@Actual_Dims_Width)

6
select 'abcd' + ltrim(str(1)) + ltrim(str(2)) 
相關問題