2

私はSQLが初めてです。私はこの機能する SQL 関数を持っていますが、結果が空の場合に不要なコンマを残さないように、結果をコンマで結合したいと考えています。COALESCE でプレイしましたが、動作しませんでした。手がかりや助けは素晴らしいでしょう。

Python では、次のようにします。

'abcdef'.join(', ')

SQL関数は次のとおりです。

USE [Broadcast]
GO
/****** Object:  UserDefinedFunction [dbo].[MapConsumerAdvice]    Script Date: 04/24/2013 14:49:10 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date, ,>
-- Description: <Description, ,>
-- =============================================
ALTER FUNCTION [dbo].[MapConsumerAdvice] 
(
    @A as Int,
    @B as Int,
    @C as Int,
    @D as Int,
    @E as Int,
    @F as Int,
    @G as Int,
    @H as Int
)
RETURNS VarChar(20)
AS
BEGIN

    DECLARE @Result as VarChar(999)

    SET @Result = ''

    IF (@A > 0) SET @Result = @Result + ',' + 'a'
    IF (@B > 0) SET @Result = @Result + ',' + 'd'
    IF (@C > 0) SET @Result = @Result + ',' + 'h'
    IF (@D > 0) SET @Result = @Result + ',' + 'l'
    IF (@E > 0) SET @Result = @Result + ',' + 'm'
    IF (@F > 0) SET @Result = @Result + ',' + 'n'
    IF (@G > 0) SET @Result = @Result + ',' + 's'
    IF (@H > 0) SET @Result = @Result + ',' + 'v'

    Return @Result

END
4

3 に答える 3

1

試す

RETURN SUBSTRING(@Result, 2, LEN(@Result))

また

RETURN RIGHT(@Result, LEN(@Result)-1)

それ以外のReturn @Result

于 2013-04-24T06:33:51.070 に答える
0

何かが割り当てられているかどうかを確認する必要があります。

USE [Broadcast]
GO
/****** Object:  UserDefinedFunction [dbo].[MapConsumerAdvice]    Script Date: 04/24/2013 14:49:10 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date, ,>
-- Description: <Description, ,>
-- =============================================
ALTER FUNCTION [dbo].[MapConsumerAdvice] 
(
    @A as Int,
    @B as Int,
    @C as Int,
    @D as Int,
    @E as Int,
    @F as Int,
    @G as Int,
    @H as Int
)
RETURNS VarChar(20)
AS
BEGIN

DECLARE @Result as VarChar(999)

SET @Result = ''

IF (@A > 0) SET @Result = @Result + ',' + 'a'
IF (@B > 0) SET @Result = @Result + ',' + 'd'
IF (@C > 0) SET @Result = @Result + ',' + 'h'
IF (@D > 0) SET @Result = @Result + ',' + 'l'
IF (@E > 0) SET @Result = @Result + ',' + 'm'
IF (@F > 0) SET @Result = @Result + ',' + 'n'
IF (@G > 0) SET @Result = @Result + ',' + 's'
IF (@H > 0) SET @Result = @Result + ',' + 'v'

IF PATINDEX(',%', @Result) > 1 Set @Result = SUBSTRING(@Result, 2, LEN(@Result));

Return @Result

END
于 2013-04-24T06:38:43.230 に答える