How to fetch the correct dates - sql

guys sorry I don't really know how to title my question. and not so good in English.
but here's my problem:
I want to fetch the birthdays of employees. from a specified month
eg: For the month of "August" I want to fetch data from dates 14 - 20
I'm having a hard time because each has different birth years.
Here is what i have tried. but doesn't work, i also search the internet but don't have a single clue.
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 & "%'"
and oh, Another one problem is that:
the line Cast(Birthday AS DATE) returns date with Time, how do i get rid of this?
The data type of Birthday in sql is Date

At Last! found an answer to my problem ..
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 & ")"

Related

VBA UDF for list of MM.YYYY (months) between two dates

I am trying to list the dates between two given months: a) 1/1/2021; b) 6/1/2021 in format: 01.2021; 02.2021; 03.2021; 04.2021; 05.2021; 06.2021
I was able to find and use this UDF:
Function MONTHRANGE(startDate As Date, endDate As Date, _
Optional Delim As String = "; ", _
Optional dFormat As String = "MM.YYYY") As String
MONTHRANGE = Join(Evaluate("TRANSPOSE(TEXT(ROW(" & CLng(startDate) & ":" & CLng(endDate) & ")," & Chr(34) & dFormat & Chr(34) & "))"), Delim)
End Function
The output of this is repeated dates (for each day of the month) in the format I want - how can I return just the unique values (one - per month)?
Something like the following gets the job done:
Option Explicit
Private Sub Test()
Debug.Print GetMonths(CDate("1/1/2021"), CDate("6/1/2021"))
End Sub
Private Function GetMonths(ByVal StartDate As Date, ByVal EndDate As Date) As String
Do While StartDate <= EndDate
GetMonths = GetMonths & Format(Month(StartDate), "00") & "." & Year(StartDate) & "; "
StartDate = DateAdd("m", 1, StartDate)
Loop
GetMonths = Left(GetMonths, Len(GetMonths) - 2)
End Function

Access vba sql query with date in format dd/mm/yyyy

I searched how to solve my issue in many post but can't find an asnwer.
I need to perform a select in sql where one of the criteria is a date that is between other two dates.
My problem is that my date field in the database is a text area and i need a date to work with
If i run my sql sentence with the date writen it works, but there's a problem when i use variables, here i show the code used.
Thanks in advance
Dim fechaM As Date
Dim fechaAnt As String
Dim fechaPost As String
fechaM = Format(CDate(Nz(rs!fecha_m)), "dd/mm/yyyy")
fechaAnt = Format(CDate(Nz(rs!fecha_m)) - 7, "dd/mm/yyyy")
fechaPost = Format(CDate(Nz(rs!fecha_m)) + 7, "dd/mm/yyyy")
Set rsAguas = Db.OpenRecordset("SELECT table_user.nombre FROM table_user INNER JOIN M_A ON " & _
"table_user.localizacion = M_A.localizacion WHERE " & _
" fechaM " Between " & fechaAnt & " AND " & fechaPost & " " & _
" AND ((M_A.estado)=1)")
When i run this code i don't get error, but it doesn't retrieve data
The bast way to deal with International Dates in Access is to use Allen browne SQLDate Function Allen browne site .
the function
Function SQLDate(varDate As Variant) As String
'Purpose: Return a delimited string in the date format used natively by JET SQL.
'Argument: A date/time value.
'Note: Returns just the date format if the argument has no time component,
' or a date/time format if it does.
'Author: Allen Browne. allen#allenbrowne.com, June 2006.
If IsDate(varDate) Then
If DateValue(varDate) = varDate Then
SQLDate = Format$(varDate, "\#mm\/dd\/yyyy\#")
Else
SQLDate = Format$(varDate, "\#mm\/dd\/yyyy hh\:nn\:ss\#")
End If
End If
End Function
You can use the inbuilt DateValue and DateAdd functions:
WHERE
(M_A.estado = 1) AND
DateValue(fechaM) Between DateAdd("d", -7, DateValue(fechaM)) AND DateAdd("d", 7, DateValue(fechaM))
You are mixing it a little. Try:
Dim fechaM As Date
Dim fechaAnt As String
Dim fechaPost As String
fechaM = Nz(CVDate(rs!fecha_m.Value), Date)
fechaAnt = Format(DateAdd("d", -7, fechaM), "yyyy\/mm\/dd")
fechaPost = Format(DateAdd("d", 7, fechaM), "yyyy\/mm\/dd")
Set rsAguas = Db.OpenRecordset("SELECT table_user.nombre FROM table_user INNER JOIN M_A ON " & _
"table_user.localizacion = M_A.localizacion WHERE " & _
"(fechaMfield Between #" & fechaAnt & "# AND #" & fechaPost & "#) " & _
"AND (M_A.estado = 1)")
Here, fechaMfield is the name of your date field of the table.
If fechaMfield is text, use DateValue([fechaMfield]) in the query.

Generating quarters based on start date and end date

I am trying to generate a table that contains all quarters based on a start date and an end date. I wrote already a function but unfortunately is not calculating the quarters correctly. For example I have:
- start date: 01.06.2018
- end date: 01.03.2019
My function generates Q2, Q3, Q4 and normally and correctly should generate Q2, Q3, Q4, Q1.
Does anyone have sugestion?
Thank you!
Here is my function:
Private Function calculateQuartars(ByVal startDate As Date, ByVal endDate As Date) As Collection
Dim iDate As Date, i As Integer
Dim quartal As String
Dim quartalListe As Collection
iDate = startDate
i = 0
Set quartalListe = New Collection
If (IsDate(startDate) And IsDate(endDate)) Then
Do
quartal = "Q" & DatePart("q", iDate) & " " & DatePart("yyyy", iDate)
quartalListe.Add quartal, "Q" & i
iDate = DateAdd("m", 3, iDate)
i = i + 1
Loop While iDate < endDate
Else
quartal = ""
quartalListe.Add quartal, "Q" & i
End If
Set calculateQuartars = quartalListe
End Function
The problem is with the below line
Loop While iDate < endDate
Your loop exits if the dates are equal
Change it to
Loop While iDate <= endDate
I solved it!
If (IsDate(startDate) And IsDate(endDate)) Then
Do
quartal = "Q" & DatePart("q", iDate) & " " & DatePart("yyyy", iDate)
iDate = DateAdd("m", 1, iDate)
If LastQuartal = quartal Then
Else
quartal = "Q" & DatePart("q", iDate) & " " & DatePart("yyyy", iDate)
quartalListe.Add quartal
LastQuartal = quartal
End If
Loop While iDate <= endDate

How to stop the Month and Day from changing in the BindingSource.Filter?

This is the code for my BindingSource.Filter
Dim FilterStartMonth As Date = DateTimePickerTodaysDateTime.Value.Date
Dim Filter2MonthsBack As Date = DateTimePickerTodaysDateTime.Value.AddMonths(-2).Date
ClockInTimesBindingSource.Filter = "EmployeeID = " & ComboBoxOfficeEmployeeFilter.SelectedValue & " and Date <= '" & Filter2MonthsBack & "' and Date >= '" & FilterStartMonth & "'"
When i go through it step by step as it runs these are the values:
Filter2MonthsBack = 1/2/2017 12:00:00 AM
FilterStartMonth = 3/2/2017 12:00:00 AM
Which is how i want them, but the BindingSource.Filter reads:
ClockInTimesBindingSource.Filter = "EmployeeID = 49 and Date <= '02/01/2017' and Date >= '02/03/2017'"
I can't work out why it is swapping the month and day around??
Any help would be much appreciated.
I have tried using .ToString("d") but it still chnges around in the filter string.
Your query says:
filter where the date is <= Jan 2, 2017 AND date is >= Mar 2, 2017
Query works, since that isn't possible.
You probably meant to switch the equation around:
" and Date >= '" & Filter2MonthsBack & "' and Date <= '" & FilterStartMonth & "'"

How to insert current year and next year using sql query in vb.net

Dim sCon As New SQLConnect
strSql = " select distinct DATEPART(yyyy,fl_date) as fl_date " & _
" from attendancemast" & _
" where 1 = 1" & _
" order by fl_date"
'" union " & _ commented
'" select year(GETDATE())" commented
sCon.sqlCmd.CommandText = strSql
sCon.sqlReader = sCon.sqlCmd.ExecuteReader
cboYear.Items.Clear()
cboYear.Items.Add(New ListItem("", ""))
While sCon.sqlReader.Read
cboYear.Items.Add(New ListItem(sCon.sqlReader("fl_date")))
End While
sCon.sqlReader.Close()
in this sql query i am selecting year 2004 to 2013 from my database and i want to fill dropdown by current year and next year in this query by union mehtod
I question the need to open a SQL connection to get the current year and next year, but this will do that.
SELECT YEAR(GetDate()) as fl_date
UNION SELECT YEAR(GetDate()) + 1