select date between

ตัวอย่างการ select ข้อมูลจากตารางโดยใช้เงื่อนไขวันที่ เป็นตัวกำหนดเงื่อนไขการเลือกข้อมูล  ในที่นี้เป็นการใช้งาน parameter ร่วมด้วย เนื่องจาก SQL SERVER จะค่อนข้างมีปัญหาเรื่องรูปแบบของค่าวันที่ ที่เราส่งเข้าไปยังคำสั่ง SQL ดังนั้น จึงเขียนแบบใช้ parameter จะช่วยลดปัญหาได้ในส่วนหนึ่งค่ะ ตัวอย่างนะค่ะ ดังต่อไปนี้

strSQL = "select sum((price*amount)-discount) as tt from sale,saledetail where " & _

             " sale.saleid=saledetail.saleid and sale.saledate between @strDate1 and @strDate2"

comm =New SqlCommand(strSQL, conn)

comm.Parameters.Add("@strDate1", SqlDbType.DateTime).Value = Format("dd/mm/yy", txtDate1.Value)

comm.Parameters.Add("@strDate2", SqlDbType.DateTime).Value = Format("dd/mm/yy", txtDate2.Value)

comm.CommandType = CommandType.Text

dr = comm.ExecuteReader 

If dr.HasRows Then

dr.Read()

strSale1 = Val(dr(

"tt").ToString)

dr.Close()

 

EndIf