我相信我的编码有问题。据我所知,这不是按日期过滤的。请帮忙
SQL代码:
SELECT
CONVERT(char(80), i.InvDate,3) AS InvDate,
i.InvoiceNo,
i.EmployerCode,
i.TaxAmount + i.SubTotal AS Amount,
'' AS Payment,
pd.GivenName
FROM
dbo.Invoice i
INNER JOIN
dbo.PatientDetails pd ON (pd.MedicalRecordID = i.MedicalRecordID)
C#代码:
SqlCommand objCmd = new SqlCommand("SELECT CONVERT(char(80), i.InvDate,3) AS InvDate,
i.InvoiceNo,
i.EmployerCode,
i.TaxAmount + i.SubTotal AS Amount,
'' AS Payment,
pd.GivenName
FROM
dbo.Invoice i
INNER JOIN dbo.PatientDetails pd ON (pd.MedicalRecordID = i.MedicalRecordID)
WHERE (InvDate >= CONVERT(datetime, '" + dtpFrom.Text + "', 105 ))
AND (InvDate <= CONVERT(datetime, '" + dtpTo.Text + "', 105))," , objConn);
这就是我想要实现的目标:
http://i47.tinypic.com/167trt3.png
这就是我得到的:
http://i50.tinypic.com/28m2z5x.png
在我从这里被踢之前。我会做一些修改。
编辑:
DateTime是105格式(dd-mm-yyyy)
WHERE (InvDate >= CONVERT(datetime, '" + dtpFrom.Text + "', 105 ))
AND (InvDate <= CONVERT(datetime, '" + dtpTo.Text + "', 105))
这应该从特定日期中选择。 EG:2012年10月11日至2012年10月15日。
即使没有DateTimepicker,输出也不会显示“GivenName”
答案 0 :(得分:1)
最好的方法是以dd / MMM / yyyy格式格式化输入。它更好地帮助SqlServer识别月和日。我强烈建议您在与SqlServer通信时使用MMM格式。
DateTime fromDate, toDate;
fromDate = DateTime.Parse(dtpFrom.Text);
toDate = DateTime.Parse(dtpTo.Text);
SqlCommand objCmd = new SqlCommand("SELECT CONVERT(char(80), i.InvDate,3) AS InvDate,
i.InvoiceNo,
i.EmployerCode,
i.TaxAmount + i.SubTotal AS Amount,
'' AS Payment,
pd.GivenName
FROM dbo.Invoice i
INNER JOIN dbo.PatientDetails pd ON (pd.MedicalRecordID = i.MedicalRecordID)
WHERE datediff(d, '" + fromDate.ToString("dd/MMM/yyyy") + "', i.InvDate) >=0
AND datediff(d, '" + toDate.ToString("dd/MMM/yyyy") + "', i.InvDate) <=0", objConn);
或者如果你有任何约束要做,如上所述,使用CONVERT()
转换输入SqlCommand objCmd = new SqlCommand("SELECT CONVERT(char(80), i.InvDate,3) AS InvDate,
i.InvoiceNo,
i.EmployerCode,
i.TaxAmount + i.SubTotal AS Amount,
'' AS Payment,
pd.GivenName
FROM dbo.Invoice i
INNER JOIN dbo.PatientDetails pd ON (pd.MedicalRecordID = i.MedicalRecordID)
WHERE datediff(d, CONVERT(datetime, '" + dtpFrom.Text + "', 105 ), i.InvDate) >=0
AND datediff(d, CONVERT(datetime, '" + dtpTo.Text + "', 105 ), i.InvDate) <=0", objConn);
答案 1 :(得分:-1)
问题是由于将日期类型转换为char,过滤可能无法按预期工作。尝试使用datediff,它可以解决问题。
SqlCommand objCmd = new SqlCommand("SELECT CONVERT(char(80), i.InvDate,3) AS InvDate,
i.InvoiceNo,
i.EmployerCode,
i.TaxAmount + i.SubTotal AS Amount,
'' AS Payment,
pd.GivenName
FROM dbo.Invoice i
INNER JOIN dbo.PatientDetails pd ON (pd.MedicalRecordID = i.MedicalRecordID)
WHERE datediff(d, '" + dtpFrom.Text + "', i.InvDate) >=0
AND datediff(d, '" + dtpTo.Text + "', i.InvDate) <=0", objConn);