SQL函数中的两个日期参数

时间:2011-03-16 07:30:32

标签: c# asp.net sql sql-server function

我有一个这样的页面

enter image description here

在我的SQL中,我想在这些日子之间计算一些值

这是我的代码;

strQuery = @"SELECT  B.HESAP_NO, A.TEKLIF_NO1 + '/' + A.TEKLIF_NO2 AS 'TEKLIF',
B.MUS_K_ISIM, CONVERT(VARCHAR(10),A.ISL_TAR,103) AS 'TARIH',
SUM(ISNULL(CAST(A.ODENEN_ANAPARA AS FLOAT),0)+ISNULL(CAST(A.FAIZ AS FLOAT),0)+
      ISNULL(CAST(A.BSMV AS FLOAT),0)+ISNULL(CAST(A.GECIKME_FAIZ AS FLOAT),0)+
      ISNULL(CAST(A.GECIKME_BSMV AS FLOAT),0)) 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 ICRA_TAR 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 HACIZ_TAR FROM TAKIP WHERE T_HESAP_NO = D.HESAP)
     ELSE '' END) AS 'HACİZ TARİHİ'
FROM YAZ..MARDATA.BIR_TAHSIL A, YAZ..MARDATA.S_TEKLIF B, AVUKAT D
WHERE A.TEKLIF_NO1 = B.TEKLIF_NO1
AND A.TEKLIF_NO2 = B.TEKLIF_NO2
AND B.HESAP_NO = D.HESAP
AND A.HESAP_NO = D.HESAP ";


    if (txtBoxText1 != "")
    {
        strQuery = strQuery + " AND A.ISL_TAR >= @S_TARIH_B";

      dt_stb = DateTime.Parse(txtBoxText1);
      myCommand.Parameters.AddWithValue("@S_TARIH_B", dt_stb);
    }

    if (txtBoxText2 != "")
    {
        strQuery = strQuery + " AND A.ISL_TAR <= @S_TARIH_S";
      dt_sts = DateTime.Parse(txtBoxText2);
      myCommand.Parameters.AddWithValue("@S_TARIH_S", dt_sts);
    }

    strQuery = strQuery + " GROUP BY B.HESAP_NO, A.TEKLIF_NO1 + '/' + A.TEKLIF_NO2,A.ISL_TAR,B.DOVIZ_KOD ,B.MUS_K_ISIM, D.AVUKAT, D.HESAP";

这是我的Function;

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

AS
BEGIN

RETURN( 
    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 ISL_TAR >= @BAS 
    AND ISL_TAR <= @BIT
)
END

我希望使用此函数在此代码中获取值。

@BAS是开始日期,@BIT是结束日期

如何关联@BAS with Textbox1@BIT with Textbox2

2 个答案:

答案 0 :(得分:1)

你已经对你的代码做了类似的事情。

strQuery = @"SELECT  fngcodeme(@HESAP, @BAS, @BIT, @DOV)";

dt_stb = DateTime.Parse(txtBoxText1);
myCommand.Parameters.AddWithValue("@BAS", dt_stb);
dt_sts = DateTime.Parse(txtBoxText2);
myCommand.Parameters.AddWithValue("@BIT", dt_sts)

// do it for @Hesap and @Dov

答案 1 :(得分:0)

使用与语言无关的表单为命令参数提供日期,因为无论登录用户的语言如何,这都将起作用。以下是用于指定日期参数的语言无关格式列表。

对于DATETIME使用:'YYYYMMDD hh:mm:ss.nnn','YYYY-MM-DDThh:mm:ss.nnn','YYYYMMDD' 对于SMALLDATETIME使用:'YYYYMMDD hh:mm','YYYY-MM-DDThh:mm','YYYYMMDD' 对于DATE使用“'YYYYMMDD','YYYY-MM-DD'

DateTime.Parse依赖于当前文化,当前文化可能会以不同方式解析TextBox中的字符串,因此使用与语言无关的表单会更好。