[sql] 숫자와 문자열을 연결하여 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가 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 페이지에서 Weight 또는 DIMS 만 선택할 수 있으며 사용자가 DIMS를 선택한 경우 길이, 너비 및 높이를 제공해야합니다. 그렇지 않으면 ASP.net 페이지에서 오류가 발생합니다. SQL 측에서 걱정해야합니까?



답변

몇 가지 간단한 참고 사항 :

  • “길이”가 아니라 “길이”입니다.
  • 쿼리의 테이블 별칭은 아마도 훨씬 더 읽기 쉽게 만들 것입니다.

이제 문제에 대해 …

매개 변수를 연결하기 전에 VARCHAR로 명시 적으로 변환해야합니다. SQL Server에 @my_int + ‘X’가 표시되면 @my_int에 숫자 “X”를 추가하려고하는데 그렇게 할 수 없다고 생각합니다. 대신 다음을 시도하십시오.

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


답변

당신이 사용하는 경우 SQL Server를 2012+ 당신이 사용할 수있는 CONCAT의 우리가 어떤 명시 적 변환을 할 필요가 없습니다있는 기능을

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


답변

이것을 변경하십시오 :

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에 대해 모두 알아보십시오 !


답변

select 'abcd' + ltrim(str(1)) + ltrim(str(2))


답변

정수를 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);


답변

먼저 정수를 varchar로 캐스팅하십시오!


답변

문자열 연결을 수행하기 전에 숫자 데이터를 문자열로 변환해야합니다. 예를 들어 , & c CAST(@Actual_Dims_Lenght AS VARCHAR)대신 사용하십시오 @Actual_Dims_Lenght.