Specific time range between dates - sql-server

I want to write a query in SQL Server 2014 that will show me all the rows in specified date range between specific times. I have column DateCreated which contains date and time together.
I can easily filter date but I need all rows from these days in specific time range.
Thank you

Adding to #Larnu's comment, you can add time range criteria in addition to the desired date range so that results are filtered by the time period as well. Below is an example using inclusive start and exclusive end range criteria:
WHERE
DateCreated >= '20200801' AND DateCreated < '20200831')
AND CAST(DateCreated AS time) >= '08:00:00' AND CAST(DateCreated AS time) < '16:00:00')

Related

Postgres partitioning use case for business to be implemented

Please find my use case below and suggest the appropriate solution:
We have done declarative partitioning over our table A based on some key and further range sub partitioned each partition on the basis of createddate range of 90 days.
In the search query to display last 90 day data , I give both column B and createddate which may pick any of the required partitions.
But how can I display last 90 days data in which a record which is modified last is also shown at the top but its created date might not lie in the last 90 days range..
Basically, I need to partition my huge table based on some key and also on date to split the data into smaller dataset for faster query . The UI needs to display the latest modified records first but the created date also needs to be given in the query to pick the right partition...
Can we do partition differently to achieve this?
Should i find the max modified date and give it in the range of the created date.
Yes I have a btree index on the modified_date column currently.
SELECT * FROM partitioned_table where
A ='Value'
AND created_date >= '2021-03-01 08:16:13.589' and created_date <= '2021-04-02 08:16:13.589' ORDER BY viewpriority desc OFFSET 0 ROWS FETCH NEXT 200 ROWS ONLY;
Here viewpriority is basically a long value containing created_date in milliseconds.
Issue here is : I want to somehow include modified_date also in this query to get the records sorted by modified_date . But that sorting will happen only in the specified created_date range only. I want those latest modified records also whose created_date might not lie in this range.

BigQuery/Google Data Studio: Counting orders from first purchase to max date on date range filter

I am using BigQuery and Google Data Studio to build a table that shows customers total orders from their first purchase to the max date selected on the date range filter.
This is the sample dataset I have
This is the output I want
Not sure if Data Studio supports this. Is it an option to always use some old date like 01/01/1970 (set it as default) as the first date on the date range? That way you can just add Customer Id and First Order Date as Dimensions and count(Order Date) as Metric.

Filtering table record based on time availability in SQL Server

I have one table tblAvailability in which I have two column StartTime time and EndTime time.
There I set StartTime=7 AM and EndTime=10.
Now I have filter in front end which send time range like 6 TO 10 OR 7:30 TO 10 like this.
I wrote the SQL query like below.
Where CONVERT(varchar, StartTime, 108) >= CONVERT(varchar, #startTime, 108)
AND CONVERT(varchar, EndTime, 108) <= CONVERT(varchar, #endTime, 108)
Now I would like to filter out rows based on start and end time range, means would like to get all records table whose start and end time ranges between selected time range.
As mentioned in the comments above, you are not clear whether you are using time of date or date and time. Can you please post more information.
You should be aware that date types and ranges in SQL Server can differ to a client language such as C# (although you don't mention your client language). For example, if you are using SQL Server "datetime" you can restrict the client date range in C# by using the appropriate type, e.g.
System.Data.SqlDbType.DateTime
You say you have written a query you want to use as to filter some data, which I assume you run within SQL Server.
Assume using C# as the client, an efficient way of returning data is to write a View in SQL Server and add this to an Entity Framework model. To query this data at the client, use LinqSQL which will convert your client code into SQL and runs surprisingly efficiently.
var filteredData = (from i in _db.vw_SomeData where i.StartDate >= fromDate && i.EndDate < toDate select i).ToList()
Be careful of your data endpoints (inclusive and exclusive values). If you use SQL Server datetime and you want all times within a day, then I use (date >= today and date < tomorrow) which is guaranteed to work. trying to test against the last second of the day with a "less than or equals" won't work where there is a time between the last second and midnight.
To sum up, comparing date and or time is best performed natively for those types. Only where some of the comparison data is a string should you convert one to the other. The only easily sortable and comparable date and time is yyyymmdd and HHmmss

How can I get the number of OverDue tasks based on date field in Power BI?

I need to create a Measure that would display the number of tasks that are over due, based on today's date.
So basically I need to translate this into DAX in power BI?
SUM(If DueDate is greater then today then count it as OverDueTask)
I tried to do something like that but when trying to display it it says:
Cant display the visual
OverDueTasks = CALCULATE(SUM(TotalCounts[DueDate]), FILTER(TotalCounts,[DueDate]>TODAY()))
I tried your formula and the issue appears to be the SUM. You are summing dates so Power BI is going to treat them like dates, but the formula goes past the threshold for dates. Change the SUM to a COUNT and it should work.
OverDueTasks = CALCULATE(
COUNT(TotalCounts[DueDate]),
FILTER(
TotalCounts,
[DueDate] > TODAY() &&
[CompletedDate] = ""
)
)
you should first create a calculated column with formula
=IF(Table1[DueDate]>TODAY()|1|0)
Then a measure to sum this up
Overdue:=SUM(Table1[Calculated Column 1])

DAX Date Range Syntax - Count one Row More Than Once?

I'm working with a SQL 2014 tabular model and I want to create a measure for count based on a date range.
My fact table will have a start and ending date range which can span multiple months. I want the user to be able to select a date range to get a count of records. The catch is that each month a record spans needs to be captured as separate in the count.
For example: Record 1 - 1/1/2014 - 8/21/2014. If the user selects a date range of 3/1 to 5/1, I want the count to return as 3 (March, April and May). If the user selects 6/4/ - 6/4, I want the count to return as 1.
Is there a way I can do this with DAX or should I go the route of creating a record for each month?
Your model is not exactly clear; but assuming that your StartDate and EndDate are in the same row - you are in effect, looking to count the number of month between these 2 dates and that can be achieved as follows;
=(YEAR([EndDate])-YEAR([StartDate]))*12+MONTH([EndDate])-MONTH([StartDate])
Your measure would then SUM() the results of this calculated column.

Resources