Returning just the date portion of =NOW()-(WEEKDAY(NOW(),1)) in Excel 2007 - excel-2007

I need to return just the date portion of the following formula: =NOW()-(WEEKDAY(NOW(),1)). Any ideas? I am using this formula to get the current date and then return the previous Saturday's date, but I am also getting the time component and I need to drop that portion.

This is the solution I used:
=DATE(YEAR(NOW()),MONTH(NOW()),DAY(NOW()-(WEEKDAY(NOW(),1))))

Related

MS Access form date range of single date yields no results

The form uses begin date and end date text boxes formatted as "General Date" to filter results. They are generally filled using the calendar date picker tool as format m/d/yyyy. ODBC SQL table that it's pulling the date field from is also showing format "General Date" in Design View but the field includes date and time as format m/d/yyyy H:MM:SS AM/PM.
When the same date is entered in the form begin and end date it shows no results, but I am told by my team in the past it has shown results. I am assuming that this is due to the form date picker including no time and defaulting to 12 AM in both boxes when the query runs. If that is/may be the case, is there a way to edit the end date text box to default to 11:59:59 PM of the date selected?
10/28 EDIT: I was asked to provide the SQL that refers to the end date text box. For context: the command button on the form opens query dbo_GEN_INSP_Count_Daily which is based on query dbo_GEN_INSP_Count_Monthly where the text box is mentioned. See below.
query dbo_GEN_INSP_Count_Daily
SELECT DateValue([INSP_DTE]) AS Insp_Date, Count([INSP_DTE]) AS DailyCount
FROM dbo_GEN_INSP_Count_Monthly
GROUP BY DateValue([INSP_DTE]);
query dbo_GEN_INSP_Count_Monthly
SELECT dbo_VEHICLE.DMV_VIN_NUM, dbo_INSPECTION_GENERAL.INSP_DTE, dbo_INSPECTION_GENERAL.CI_NUM, dbo_INSPECTION_GENERAL.DMV_FACILITY_NUM, dbo_INSPECTION_GENERAL.VIP_UNIT_NUM
FROM dbo_INSPECTION_GENERAL INNER JOIN dbo_VEHICLE ON (dbo_INSPECTION_GENERAL.VIP_UNIT_NUM = dbo_VEHICLE.VIP_UNIT_NUM) AND (dbo_INSPECTION_GENERAL.DMV_FACILITY_NUM = dbo_VEHICLE.DMV_FACILITY_NUM) AND (dbo_INSPECTION_GENERAL.CI_NUM = dbo_VEHICLE.CI_NUM) AND (dbo_INSPECTION_GENERAL.INSP_DTE = dbo_VEHICLE.INSP_DTE)
WHERE (((dbo_INSPECTION_GENERAL.INSP_DTE) Between [Forms]![Form1]![StartDate] And [Forms]![Form1]![EndDate]));
I'm not sure how it would play with your ODBC connection, but in the date field in the form under the data tab in the property sheet there is a default value for the form, just like in the MS Access table. You could try to put your default time in there at 11:59:59, it will automatically put quotes around it and it should reflect in your form.
You could also create a default constraint on the database itself to reflect 11:59:59 for the end date if nothing is there. Going this route you would probably need to break up your time and date fields in the database and bring them together in MS Access with a function. That way if a date was placed there with no time it would default to 11:59:59.
When the user enters the same date for both StartDate and EndDate, you want the query to return all rows containing that date regardless of the time component stored with the date.
So, instead of your current Between ... And approach, make the endpoint target less than one day after EndDate.
WHERE
dbo_INSPECTION_GENERAL.INSP_DTE >= Forms!Form1!StartDate
And dbo_INSPECTION_GENERAL.INSP_DTE < (DateValue(Forms!Form1!EndDate) + 1)

VBA - Convert string to datetime to compare with date in SQL

Probably a simple question, but I haven't been able to find an answer on any forums yet.
I am using VBA to push data into SQL from an open source, and what I need my code to do is check the timestamp of the most recent data in SQL to determine if it needs to retrieve more data, or it is most up to date.
By using ADODB I can retrieve the latest date from SQL as follows
print latest_date
13/07/2021 8:14:50 PM
And the date from the data I want to collect as
print resource_date
'2021-07-13 20:14:50'
As you can see, the resource_date is a string, and the latest_date is an item from an ADODB recordset. These can't be compared in an If statement to see if they're the same.
I've tried using CDate() but I get mismatched type errors.
Any help is appreciated.
Thanks
Convert the text date to a true date value and use DateDiff to compare, for example by the second ("s"):
If DateDiff("s", print latest_date, CDate(resource_date)) > 0
' latest_date is earlier than resource_date.
End If

Incrementing a date in openrefine

I have a date in format of YYYY-MM-DDThh:mm:ss
Please provide a GREL expression that increments date to 1 month from the present date value for all cells in the column in openrefine. Thanks!
First you need to make sure the data in the cells is of type 'date' - if the text in the cell is in green then the data is already 'date' type. Otherwise you will need to convert it using the GREL:
value.toDate()
Screenshot - before converting to date
Screenshot - after converting to date
Once you have the data as Date type, then you can use the following GREL to increment by one month:
value.inc(1,'month')
For more on using dates in OpenRefine see https://github.com/OpenRefine/OpenRefine/wiki/GREL-Date-Functions

Not able to get month from current date

Am trying to extract month from the current date but in vain. I am using the code:
Format(Today.Date, "mmmm")
However when I try to run it to display month like January it instead displays 00. I thought that this would work but it isn't. What can I do to get the month from current date in vb.net using a simple approach like a single function?
Try This :
DateTime.Today.ToString("MMMM")
Check out This for detailed date formatting options.
try this
MonthName(Now.Date.Month(),true)
msdn: Custom Date and Time Format Strings
Format(Today.Date, "MMMM")
EXTRACT(MONTH FROM NOW()) , getting month of current date

how to get excel to treat a date as a date not a string when doing CopyFromRecordset

I have an SQL query from SQL Server which returns dates as a string in the format "YYYY-MM-DD".
If I enter a date in this format into a cell, it's recognised as a date.
But when I populate a worksheet with CopyFromRecordset, it seems to be treated as a string.
Any formula which uses the cell converts it to a date first. For example, if my dates are in col A and I make a new column B filled with a formula =A1 + 0
the formula returns my date, as a date.
The problem:
I use the Recordset data for a few things, one of them being a pivot table.
The pivot table does not see my dates as dates. I can't group as dates, for example. My hack is to make a new column which is basically =A1 + 0
I'm going to change my macro to automate this adding a zero, but I wonder if there's a way to get it right from the moment the CopyFromRecordset is performed.
The easiest way would be to do the conversion on the SQL server e.g.
SELECT CAST(date_text AS DATE) FROM TestExcelDates;
CopyFromRecordset is well known for causing data type / cell formatting issues in Excel.
I think I remember reading somewhere this is because the datatype of the recordset is ignored and Excel attempts to work out the format of each column itself based on a subset of the data in the recordset.
The best way round this is to set the cell formatting in the destination range before performing the CopyFromRecordset.
I had this problem after I had changed a view on my SQL Server database. I had changed the data type to DATE; formerly it was on an older version which didn't support DATE so I had used DATETIME. I suspect Excel doesn't always recognize the Date datatype through the SQLOLEDB provider, but it does recognize DATETIME. The field of interest is meas_date. So I altered the view by changing this to a cast SELECT CAST(meas_date AS DATETIME) AS meas_date, ... and refreshed the query in Excel. Worked!
Use the CDate() function when populating cells with dates from the recordset. This will convert the string to a date value.
Edit
That works for setting individual cell values. For using CopyFromRecordset I think you need to do the conversino in the SQL query, so the column returned by the query is a date type rather than a string.
I had this problem too importing data from Teradata, and got around it by first formatting the date columns with NumberFormat = "m/d/yy h:mm;#" (24 hr date) then stepping through the date fields afterwards with VBA and doing ws.cells(iRow, iCol).value = ws.cells(iRow, iCol).value, it forces Excel to reevaluate the string into a date/time field.
This probably will not be the answer but will surely helps you finding the right solution for your problem
String stringCellValue = myCell.toString();
here myCell has datatype as CELL which I've converted to String format.
If u want it in desired Date format, then u can try this-
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD");
myCellDate = sdf.parse(stringCellValue );
Hope it helps in solving your problem...