webmatrix - sql query using DateTime.Now - sql

i am trying to write a sql query in Webmatrix to show the count of entries in my database within the last month. I have written the below query, but it's giving me a count of zero, when i know that's not true. I think i may be writing the wrong code to show the last 7 days?
var lastweek = DateTime.Now.AddDays(7);
var week = "SELECT COUNT (*) FROM PropertyViews WHERE PropertyID = #0 AND ViewTimestamp >= #1";
var qweek = db.QueryValue (week, rPropertyId, lastweek);

You are not looking at last week, but at the coming week (today + 7 days is in the future!)
Try
var lastweek = DateTime.Now.AddDays(-7);
You mention last month by the way, but i guess you mean week everywhere :)

Related

Return the number of months between now and datetime value SQL

I apologize, I am new at SQL. I am using BigQuery. I have a field called "last_engaged_date", this field is a datetime value (2021-12-12 00:00:00 UTC). I am trying to perform a count on the number of records that were "engaged" 12 months ago, 18 months ago, and 24 months ago based on this field. At first, to make it simple for myself, I was just trying to get a count of the number of records per year, something like:
Select count(id), year(last_engaged_date) as last_engaged_year
from xyz
group by last_engaged_year
order by last_engaged_year asc
I know that there are a lot of things wrong with this query but primarily, BQ says that "Year" is not a valid function? Either way, What I really need is something like:
Date() - last_engaged_date = int(# of months)
count if <= 12 months as "12_months_count" (# of records where now - last engaged date is less than or equal to 12 months)
count if <= 18 months as "18_months_count"
count if <= 24 months as "24_months_count"
So that I have a count of how many records for each last_engaged_date period there are.
I hope this makes sense. Thank you so much for any ideas
[How to] Return the number of months between now and datetime value [in BigQuery] SQL
The simples way is just to use DATE_DIFF function as in below example
date_diff(current_date(), date(last_engaged_date), month)

Ms Access query Where

I am trying to find have to filter my query, in order to show me result from this month and the last month.
WHERE t.AllDate Between Date() - 60 and Date()
My code so far is this, but it roll back 60 days.
I need to have result for all the last month.
Thank you.
It might be simpler this way:
WHERE DateDiff("m", t.AllDate, Date()) < 2
If the question is
How to get the date of the first day of the previous month ?
You can do this in MS Access SQL :
DateSerial(Year(DateAdd('m',-1,Date())), Month(DateAdd('m',-1,Date())),1)
Right now it returns 1/04/2018
Your query becomes :
WHERE t.AllDate Between DateSerial(Year(DateAdd('m',-1,Date())), Month(DateAdd('m',-1,Date())),1)
and Date()
With this trick you will always get the right year and month, even if you run it in January.

How do I add a column in PowerBI that evaluates the difference of Latest End time and Earliest Start Time from a large dataset?

I am new to PowerBI. I am trying to make a report of the number of days consumed for a test to complete. There is a large fleet of tests that are run across a week and I would like to subtract the Earliest Start Time from the Latest End time, excluding Saturdays and Sundays and then display the result as a new column next to the Latest actual end as shown in the picture below.
Pardon for any errors above. The data was fetched from a SQL Server using a query (if that helps). Thank you.
query in sql server
select
*
--number of days
,DATEDIFF (day,[Earrliest startTime], [Latest actualend]) diffday
from TestTable
--excluding Saturdays and Sundays
where datepart(weekday,[Earrliest startTime]) not in (6,7)
and datepart(weekday,[Latest actualend]) not in (6,7)
SQL Fiddle
Hope it help you :-)
Create a New Table DateTable to be your calendar table.
DateTable = CALENDARAUTO()
Add a weekday column so you can filter out weekends.
Weekday = WEEKDAY(DateTable[Date])
Now you can create a measure that counts the days between your first and last day:
DayCount = COUNTX(DateTable,
IF(DateTable[Date]+1 > MIN(StartEnd[startTime]) &&
DateTable[Date]+1 < MAX(StartEnd[acutalend]) &&
NOT(DateTable[Weekday] IN {1,7}),
1, BLANK()))
The +1 are there to give you end of day rather than beginning of day.

How to handle Dates correctly in SSMS

Came across this issue in our scripts since the New year.
We have several scripts which look back 1 month from current UTC date.
Since the New Year, these scripts return zero results because, I assume, Sql doesn't see month 12 as being minus 1 from month 1.
Is there a way to force sql to see the year as the rotating data it is rather than an incremental line?
**Edit
Apologies, I forgot to add examples.
So, one line reads
Select .... Where ...
And month (timevalue) = month (getutcdate ()) -1
This gives zero results despite working correctly until January.
I've changed it temporarily to be = 12 which does return the correct data.
**Edit 2
Yep, thanks for the link and sample code. Works perfectly so far. I guess with our current setup it's not using daytime as I assumed, but instead using the month value as an int?
Check below, I hope it will help:
SELECT MONTH(DATEADD(MONTH, -1, GETUTCDATE())) AS PreviousMonth
returns:
PreviousMonth
12
So your query should be:
Select .... Where ... And month (timevalue) = MONTH(DATEADD(MONTH, -1, GETUTCDATE()))

Calculating Months

I have an application where the user selects the dates of a first statement and a last statement. Example, first statement = 1/1/08, last statement = 12/1/08, should equal 12 statements.
However, when using the following code, the result is 11:
numPayments = DateDiff(DateInterval.Month, CDate(.FeeStartDate), CDate(.FeeEndDate))
Is there another way to calculate this, or do I have to be stuck with adding 1 to the result?
Add 1, as you write. ;)
The difference between 1/1/2008 and 12/1/2008 is 11 months. No changing that. ;)
Yes, you'd always have to add one though you may be able to add one to the end date or subtract one from the start date to also get this effect. Consider the case where the start and end dates are the same. Their difference is 0 but you'd still want 1 statement to show just to note one odd case.
Well, the number of months between Jan 1st and Dec 1st is 11... what you're looking for is the difference of months +1. So just add one :)
Also, the DateDiff function you're using is a VB6 hold-over. Better to express it like this:
numPayments = (Date.Parse(.FeeEndDate) - Date.Parse(.FeeStartDate)).TotalMonths + 1
You could try this one. Hope this is very helpful.
Dim myDate As Date
Dim dateNow As Date
Dim nextMonth As Date
myDate = Now
dateNow = Format(myDate, "MM/dd/yyyy")
nextMonth = DateAdd(DateInterval.Month, 5, dateNow) 'compute the next 5 months from date now. Let say, #12/6/2012# the result will be #5/6/2013#
MessageBox.Show(DateDiff(DateInterval.Month, dateNow, nextMonth) & "months==> " & nextMonth)
'This will count the number of months interval. The result will be 5 months=>> #5/6/2013 because we count december to may.