0

I have a page like this

Ödeme

Baş Tar. --> Start Date Bit. Tar --> End Date

I have a SQL like this for this page,

SELECT  B.HESAP_NO, B.TEKLIF_NO1 + '/' + B.TEKLIF_NO2 AS 'TEKLIF',B.MUS_K_ISIM,
dbo.fngcodeme(B.HESAP_NO, B.DOVIZ_KOD, B.TEKLIF_NO1 + '/' + B.TEKLIF_NO2,'01/01/2011', '11/03/2011') AS 'YATAN',
(CASE WHEN B.DOVIZ_KOD = 21 THEN 'EUR' WHEN B.DOVIZ_KOD = 2 THEN 'USD' WHEN B.DOVIZ_KOD = 1 THEN 'TL' END) AS 'KUR',
D.AVUKAT, 
(CASE WHEN D.HESAP IN (SELECT T_HESAP_NO FROM TAKIP) THEN 
(SELECT CONVERT(VARCHAR(10),ICRA_TAR,103) FROM TAKIP WHERE T_HESAP_NO = D.HESAP)
ELSE ' ' END) AS 'ICRA TARİHİ', 
(CASE WHEN D.HESAP IN (SELECT T_HESAP_NO FROM TAKIP) THEN 
(SELECT CONVERT(VARCHAR(10),HACIZ_TAR,103) FROM TAKIP WHERE T_HESAP_NO = D.HESAP)
ELSE '' END) AS 'HACİZ TARİHİ'
FROM YAZ..MARDATA.S_TEKLIF B, AVUKAT D
WHERE B.HESAP_NO = D.HESAP
AND B.KAPANIS_TAR IS NULL
AND ISNULL(dbo.fngcodeme(B.HESAP_NO, B.DOVIZ_KOD, B.TEKLIF_NO1 + '/' + B.TEKLIF_NO2,'01/01/2011', '11/03/2011'),0) > 0
GROUP BY B.HESAP_NO, B.TEKLIF_NO1 + '/' + B.TEKLIF_NO2,B.DOVIZ_KOD ,B.MUS_K_ISIM, D.AVUKAT, D.HESAP

And this is my dbo.fngcodeme Function,

ALTER FUNCTION [dbo].[fngcodeme]
(
    @HESAP INT, @DOV INT, @TEKLIF VARCHAR(10), @BAS  VARCHAR(10), @BIT  VARCHAR(10)
)
RETURNS FLOAT

AS
BEGIN

DECLARE @Result FLOAT

SET @Result = (SELECT SUM(TUTAR)
    FROM YAZ..MARDATA.M_GHAREKET
    WHERE TEMEL_HESAP = @HESAP
    AND DOVIZ_KOD = @DOV
    AND REF_KOD = 'GC'
    AND BACAK_GRUP = 'PERT'
    AND ISL_KOD = 1
    AND ACIKLAMA LIKE '%' + @TEKLIF + '%'
    AND ISL_TAR >= CONVERT(DATETIME, + '' + @BAS + '',103)
    AND ISL_TAR <= CONVERT(DATETIME, + '' + @BIT + '',103)
    )

RETURN @Result

END

What i want is

IF `Bit. Tar. (End Date)` is less or equal then 28/02/2011 --> Use M_HAREKET(table)
IF  Baş. Tar. (Start Date) is great or equal then 01/03/2011 --> Use M_GHAREKET(table)

How can i do that?

Should i change SQL or Function? Which one?

2
  • You have two conditions that are not complimentary. What if end date is > 28/2 and start date is < 28/2 ? Your 2 cases does not cover it Commented Mar 16, 2011 at 22:20
  • @Richard this don't matter for this page. The "User" will enter only ..... - 28/02/2011 or 01/03/2011 - ..... date range. Commented Mar 17, 2011 at 6:09

1 Answer 1

2

If @BAS and @BIT are dates, pass them in as dates. Don't use VARCHAR.

ALTER FUNCTION [dbo].[fngcodeme]
(
    @HESAP INT, @DOV INT, @TEKLIF VARCHAR(10), @BAS datetime, @BIT datetime
)
RETURNS FLOAT

AS
BEGIN

DECLARE @Result FLOAT

IF CONVERT(DATETIME, @BIT,103) <= '20110228'
    SET @Result = (SELECT SUM(TUTAR)
        FROM YAZ..MARDATA.M_HAREKET
        WHERE TEMEL_HESAP = @HESAP
        AND DOVIZ_KOD = @DOV
        AND REF_KOD = 'GC'
        AND BACAK_GRUP = 'PERT'
        AND ISL_KOD = 1
        AND ACIKLAMA LIKE '%' + @TEKLIF + '%'
        AND ISL_TAR >= CONVERT(DATETIME, @BAS,103)
        AND ISL_TAR <= CONVERT(DATETIME, @BIT,103)
        )
ELSE
    SET @Result = (SELECT SUM(TUTAR)
        FROM YAZ..MARDATA.M_GHAREKET
        WHERE TEMEL_HESAP = @HESAP
        AND DOVIZ_KOD = @DOV
        AND REF_KOD = 'GC'
        AND BACAK_GRUP = 'PERT'
        AND ISL_KOD = 1
        AND ACIKLAMA LIKE '%' + @TEKLIF + '%'
        AND ISL_TAR >= CONVERT(DATETIME, @BAS,103)
        AND ISL_TAR <= CONVERT(DATETIME, @BIT,103)
        )

RETURN @Result

END

As for the calling function, don't use region specific date formats. Stick to YYYYMMDD or one of the ISO/XML ones. So your query would be

SELECT  B.HESAP_NO, B.TEKLIF_NO1 + '/' + B.TEKLIF_NO2 AS 'TEKLIF',B.MUS_K_ISIM,
dbo.fngcodeme(B.HESAP_NO, B.DOVIZ_KOD, B.TEKLIF_NO1 + '/' + B.TEKLIF_NO2,'20110101', '20110311') AS 'YATAN',
......
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! But in my sql i having an error like this "The conversion of a varchar data type to a datetime data type resulted in an out-of-range value."
In my aspx page i have this SQL heypasteit.com/clip/UWL but I'm getting again this error "The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. "
Why are you doing this - CONVERT(VARCHAR(10),@S_TARIH_B,103)? is @S_TARIH_B a date value or a varchar? If it is varchar, you want to convert(datetime not convert to varchar again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.