如何在T-SQL中连接数字和字符串以格式化数字?


105

我有以下功能

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 
 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不为null,则为“ Actual Weight / DIMS”返回ActualWeight,否则使用Actual_Dims_Lenght,Width和Height。

如果是DIMS,那么我想将输出格式设置为LenghtxWidhtxHeight(15x10x4)。ActualWeight,Adcutal_Dims_Lenght,Width和Height均为int(整数)值,但“ Actual Weight / DIMS”的输出应为varchar(50)。

我在哪里弄错了?

谢谢

编辑:用户只能在ASP.net页上选择“重量”或“ DIMS”,如果用户选择了“ DIMS”,则他们必须提供“长度”,“宽度”和“高度”。否则,它将在ASP.net页上引发错误。我应该在sql方面担心吗?

Answers:


211

快速注意事项:

  • 它是“长度”而不是“长度”
  • 查询中的表别名可能会使它更具可读性

现在问题了...

您需要先将参数显式转换为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的长度sqlblog.com/blogs/aaron_bertrand/archive/2009/10/09/...
尤金Ryabtsev

谢谢。有点晚了,但是我添加了length参数。
汤姆H

53

如果您使用的是SQL Server 2012+,则可以使用CONCAT函数,在该函数中我们无需进行任何显式转换

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

我碰到过一些声称CONCAT()性能比更快的说法CAST()。性能研究的可靠来源将是有用的。
带锤子的代码

如果它可以在基准测试中测量,我不会感到惊讶,但是如果它对正常查询产生影响,我会感到非常惊讶。您必须进行大量的强制转换才能淹没正常的操作时间。
SilverbackNet

8

更改此:

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的所有信息,因为数据类型很重要!



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

2
默认情况下,STR函数将数字填充为10个字符,因此会增加空格。例如STR(1)给我9个空格后跟1CAST实际效果更好。
塔希尔·哈桑


2

在进行字符串连接之前,您需要将数字数据转换为字符串,例如,使用CAST(@Actual_Dims_Lenght AS VARCHAR)而不是@Actual_Dims_Lenght,&c。


2

我在下面的查询中尝试过,它完全适合我

 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

1

尝试将int强制转换为varchar,然后再将它们添加到字符串中:

SET @ActualWeightDIMS = cast(@Actual_Dims_Lenght as varchar(8)) + 
   'x' + cast(@Actual_Dims_Width as varchar(8)) + 
   'x' + cast(@Actual_Dims_Height as varhcar(8))

1

当您将Integer转换为Str时,有可能以科学编号结尾。更安全的方法是

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

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.