如何获取正确的日期

时间:2016-08-19 08:39:45

标签: sql vb.net

抱歉,我真的不知道如何标题我的问题。英语不太好。
但这是我的问题:

我想要获取员工的生日。从指定的月份 例如:对于" 8月和#34;我想从日期14 - 20

获取数据

我很难过,因为每个人都有不同的出生年份。

这是我尝试过的。但是不起作用,我也搜索互联网,但没有一个线索。

    Dim monthName As String = cb_month.Text 'from combobox that has list of months
    Dim monthNumber = DateTime.ParseExact(monthName, "MMMM", CultureInfo.CurrentCulture).Month
    Dim fromday = CInt(cb_dayfrom.Text)
    Dim todate = cb_dayto.Text  
    ' Here's my query
    query = "SELECT empID AS 'ID', Name, Cast(Birthday AS DATE) FROM empInfo BETWEEN '%" & monthNumber & "-" & fromday.ToString & "%' AND '%" & monthNumber & "-" & todate & "%'"

哦,另一个问题是:
Cast(生日日期)行返回日期和时间,我该如何摆脱这个?
sql中生日的数据类型为日期

1 个答案:

答案 0 :(得分:0)

最后!找到了我的问题的答案..

    Dim monthNumber As Integer = DateTime.ParseExact(cb_month.Text, "MMMM", CultureInfo.CurrentCulture).Month
    Dim fromdate As Integer = CInt(cb_dayfrom.Text)
    Dim todate As Integer = CInt(cb_dayto.Text)
    query = "SELECT empID AS 'ID', Name, CONVERT(VARCHAR(10),Birthday,101) FROM empInfo " & _
        "WHERE DATEPART (month, Birthday) = " & monthNumber & _
        " AND (DATEPART (day, Birthday) Between " & fromdate & " AND " & todate & ")"