Selecting phone calls in where date is today - sql

I'm trying to retrieve phone calls from our Avaya database in SQL 2012 and i can't seem to get the dates on my code right.
cmdCalls = String.Format("SELECT * FROM dbo.tblCallList WHERE CreateDate > cast('" & DateYesterday & "' as date)")
This is my query string
Dim DateYesterday As Date = Date.Today.AddDays(<insert number of days>).ToString("yyyy-MM-dd")
I can't seem to pull the data i need from the database, it does subtract the days from the date variable but the problem is its using the days as months on the query and it shows all the records from the month instead of day
The Dates formats on the database are '2017-06-08 12:04::00:000' for example

Use the current date in the database:
SELECT *
FROM dbo.tblCallList
WHERE CreateDate >= CAST(getdate() as date);

Related

Access Date Query Problem for Today's Date

I have a set of data that includes a field called ReleaseDate, type of Date/Time, that gets written using NOW() at the time a record is added.
I can open the table and filter for today's date and get a result of 152 records.
I can run this following query, enter today's date at the prompts, and also get a result of 152 records.
SELECT ProductData.ReleaseDate, ProductData.Shift, ProductData.ExtrusionLine, ProductData.RollDensity
FROM ProductData
WHERE (((ProductData.ReleaseDate) Between [Please Enter Start Date mm/dd/yyyy] & " " & #12/30/1899# And [Please Enter End Date mm/dd/yyyy] & " " & #12/30/1899 23:59:59#));
If I run the following query no records are retrieved. I cannot figure out why.
SELECT ProductData.ReleaseDate, ProductData.Shift, ProductData.ExtrusionLine, ProductData.RollDensity
FROM ProductData
WHERE (((ProductData.ReleaseDate)=Date()));
From what I have read Date() as a criteria should return any records with today's date. Prompting for the date is not an option.
In addition to Gutav's answer, better use:
WHERE ProductData.ReleaseDate >= Date() AND ProductData.ReleaseDate < DateAdd("d", 1, Date())
as this can use an index on ProductData.ReleaseDate (faster). See my answer on Unable to use today's date as a criteria in a query
It's because ReleaseDate contains a time part (from Now()). You can strip that:
WHERE Fix(ProductData.ReleaseDate) = Date();

Pull data from table based on the date of the first record

I am querying some data from a sql table based on dates entered by the user as below :
dt = as.Date(some_date)
# Manipulate dates
end_date = as.Date(dt)
begin_date = as.character(as.Date(end_date) - 364)
What happens after this is that all records in the table where the date field falls between the begin and end date are pulled.
qry <- paste0("select * from table
where date>= '", begin_date, "' and date <= '", end_date; ")
But sometimes it might happen that I do not have 1 years of data but only 10 or 9 or 8 months.
So I want to be able to change the 364 value as per the first date in the table.
So is there any way in R by which I can pull the records starting with the begin date as end_date - 364 and if that date does not exist in the table change the begin date to the first available date and run the query again.
I understand that this will require two passes of the dates and the query but I want to be able to do it iteratively without manually checking for the dates.
Your query will give you one year of data or all the data available in table, which seems to be your requirement. However, if you need to know if there is more than one year of data before selecting the data, than you can use
SELECT MIN(date) FROM table
to get the earliest date available.

VBA queries return data beyond end date

I am running a VBA query which returns data beyond end date .
The code is as follows:
startdate = Format(Sheets("sheet1").Range("B2").Value, "####")
enddate = Format(Sheets("sheet1").Range("B3").Value, "####")
Set conn = New ADODB.connection
Set rs = New ADODB.Recordset
connection = "provider = sqloledb ; data source = &user; initial catalog = &data ; integrated security = sspi ; "
conn.Open connection
Set rs = conn.Execute("select * from sale_table where datetime >= " & startdate & " and datetime <= " & enddate & "")
If Not rs.EOF Then
Sheets(1).Range("I12").CopyFromRecordset rs
Else
MsgBox ("no record")
End If
if enddate = 31/08/2016
result shown upto 02/09/2016 ( two days beyond end date)
Your date formatting is the problem:
startdate = Format(Sheets("sheet1").Range("B2").Value, "####")
enddate = Format(Sheets("sheet1").Range("B3").Value, "####")
These will produce date serials, or the number of days elapsed since January 1st, 1900. For 31/08/2016 (08/31/2016 for those in the US), your enddate will be 42613 when using Excel.
In SQL Server, you can either use DATEADD(DAY, ..., ...) or CAST( ... AS DATETIME) to convert this to a date. Implicitly, when you perform a logical operation against a date serial i.e. <= enddate or >= startdate it'll convert that serial into a datetime.
However, if you have tried DATEADD or CAST on 42613 in SQL Server you'll notice it returns 02/09/2016 (US: 09/02/2016) which is why your returned data is beyond the end date. Also, if you tried DATEDIFF(DAY, '2016-08-31', '1900-01-01') it returns 42611 on SQL Server; 2 days difference to Excel's interpretation of 31/08/2016 date serial.
Why is this?
Mr Chip Pearson explains it in Dates and Times in Excel, but I'll summarize it. Keep in mind that when I say date serial I mean days elapsed since 01/01/1900. Both in Excel and SQL Server, default date value is 01/01/1900. However:
Excel stores dates and times as a number representing the number of days since 1900-Jan-0
00/01/1900 (US: 01/00/1900) is not a valid date. So already we have to offset any Excel date serial by -1. Why so?
The number 1 represents 1900-Jan-1. It should be noted that the number 0 does not represent 1899-Dec-31. Actually, this number is one greater than the actual number of days. This is because Excel behaves as if the date 1900-Feb-29 existed. It did not. The year 1900 was not a leap year (the year 2000 is a leap year). In Excel, the day after 1900-Feb-28 is 1900-Feb-29. In reality, the day after 1900-Feb-28 was 1900-Mar-1 . This is not a "bug". Indeed, it is by design.
So, in your particular case, since date serial 1 represents 01/01/1900 in Excel (because date serial 0 is 00/01/1900) which is first of your extra 2 days and the leap year 2000 has 366 days rather than 365 which is your second extra day, that's why it's returning data up to and including 02/09/2016.
In short, you need to account for Excel's date representation as being days from 00/01/1900 rather than from 01/01/1900 and also any leap years when using an Excel date serial in a SQL Server statement.

Sales Year To Date From End Of Previous Week AND For Last Year

I am trying to write a query in Access to poll year to date sales records to build a report. I think I need to write a VBA module to calculate these dates.
I need a date range for Year to Date from Last Saturday (ex. from 1/1/2015 to 10/24/2015 for this week's report). I also need to poll the records from Last Year To Date (ex. from 1/1/2014 to 10/25/2014 for this week's report).
Any suggestions on how I can query this date range?
Thanks!
Consider using a temporary table such as a YTDDates of two fields (ID, SalesDate) that holds the date range which can be cleaned out per needed use. Then join your sales data to this table for daily reporting.
VBA can run a loop through all days from beginning (1/1/2015) to end (10/24/2015) and iteratively append to temp table using the DateAdd() function.
Dim db As Database
Dim startDate As Date, endDate As Date, d As Date
Dim diff As Integer, i as Integer
Set db = CurrentDb()
startDate = #1/1/2015#
endDate = #10/24/2015#
diff = DateDiff("d", startDate, endDate)
' CLEAN OUT PRIOR DATE RECORDS
db.Execute "DELETE FROM YTDDates", dbFailOnError
For i = 0 To diff
d = DateAdd("d", i, startDate)
db.Execute "INSERT INTO YTDDates ([SalesDate]) VALUES (#" & d & "#);", dbFailOnError
Next i
Set db = Nothing
Instead of a temp table I would just use a formula in your query using a BETWEEN clause on your date field with a formula like this:
vba.DateSerial(Year(Now()),Month(Now()),Day(Now())-Weekday(Now(),vbSunday))
The Weekday function returns an integer from Sunday (vbSunday). So on a Monday it will return '2' and on Saturday it will return '7' which will give you the previous week's Saturday date.
For getting the beginning of the year date you could use a function like this:
vba.DateSerial(Year(Now()),1,1)

How to check Day & month from date which is in Database table

I have problem here...
I want to check Day & month of date which is in database.
There is date field is in database which is mm/dd/yyyy format If I use "select" query then whole Date is access but I want only these date's which having today's day & month
Example:
if today's date is 02/02/2010 then select only those date whose day is 02 & month is February.(Dont consider Year)
Please be free to ask if you have any problem for understand.
SELECT *
FROM {tablename}
WHERE DAY({datecolumn})=DAY(getdate())
AND MONTH({datecolumn})=MONTH(getdate())
I would use DATEPART to pull out the values you're looking for.