MSSQL: Finding all items between two dates - sql

I am creating a sails.js REST-API based on data in a MSSQL-database. Each record have a start date and an end date. I want to supply a query-date and find all records that are "started", but not "ended". So conceptually I'm thinking:
SELECT * FROM table WHERE
record_start_date <= query_date
AND
record_end_date >= query_date
In Waterline/Sails it looks like this:
Model.find({
record_start_date: { 'lessThanOrEqual': req.query.q + ' 00:00:00.0'},
record_end_date: { 'greaterThanOrEqual': req.query.q + ' 23:59:00.0' }
})
However this only gives me records that fall between them, not on the start og end date.

Looks like you need to swap the times inside the FIND:
Model.find({
record_start_date: { 'lessThanOrEqual': req.query.q + ' 23:59:00.0'},
record_end_date: { 'greaterThanOrEqual': req.query.q + ' 00:00:00.0' }
})
Consider the case where the record has start date 3/20 and end date 3/25. If you pass query date of 3/25, the record_end_date would not satisfy the greater-than-or-equal condition if you append the '23:59' to it (as you initially did).

Related

How can this SQL query be improved?

This code actually works, but I guess it can be done in an easier way, or even more efficient (because I guess comparing strings like that is not a problem but may be). I tried to convert everything into datetime and not string but failed.
This code takes the date and time row from one table, CONCATing them and compares it to the datetime row from another table. The result is: 2019-12-02 09:00:00
It is just a regular Date, Time and Datetime parameters in the table. Like this Date 2019-11-17, Time 09:00:00, Datetime 2019-01-15 16:00:00
SELECT
MAX(CONCAT(f_ini, CONCAT( " ", h_ini)))
FROM posible_work
WHERE
fk_id_asigned = 100573 AND
(SELECT MAX(CONCAT(f_ini, CONCAT( " ", h_ini)))
FROM posible_work) >
(SELECT MAX(RIGHT(f_fin,19)) AS FechaAVI FROM next_work WHERE fk_id_worker = 100573)
Apart from the fact that you force concat() to be called twice when you can use it with 3 arguments (or more), one additional problem would be this condition:
(SELECT MAX(CONCAT(f_ini, CONCAT( " ", h_ini)))
FROM posible_work) >
(SELECT MAX(RIGHT(f_fin,19)) AS FechaAVI FROM next_work WHERE fk_id_worker = 100573)
Why not use in the left side of the inequality just concat(...) and you repeat SELECT...MAX(..)...?
I don't think that this breaks your code's logic:
SELECT
MAX(CONCAT(f_ini, ' ', h_ini))
FROM posible_work
WHERE
fk_id_asigned = 100573
AND
CONCAT(f_ini, ' ', h_ini) >
(SELECT MAX(RIGHT(f_fin,19)) AS FechaAVI FROM next_work WHERE fk_id_worker = 100573)
Also it could be better if you did not hardcode 100573 twice.
Just use aliases properly:
SELECT
MAX(CONCAT(p.f_ini, ' ', p.h_ini))
FROM posible_work p
WHERE
p.fk_id_asigned = 100573
AND
CONCAT(p.f_ini, ' ', p.h_ini) >
(SELECT MAX(RIGHT(f_fin,19)) AS FechaAVI FROM next_work WHERE fk_id_worker = p.fk_id_asigned)

MDX Date Formatting

Can any one please tell me how to format date in MDX queries? We dont use SSRS to generate report ,we have our own customised reporting tool built on SSAS.Date filter sends date in yyyy/mm/dd format . As of now we dont have a date dimension. My date member looks like:
[CNB_DimSampleInfo].[COAReleasedON].&[2013-01-02T03:20:00].
How can I format date in STRTOmemeber? I have tried doing this. My question is how will the value coming from user suit my member format as below. I know ssrs does it easily but we are not using SSRS. Below is my Code
my code
SELECT
[Measures].[Result] ON COLUMNS
,NON EMPTY
{
[CNB_DimProduct].[ProductUcode].[ProductUcode].ALLMEMBERS*
[CNB_DimProduct].[ProductDesc].[ProductDesc].ALLMEMBERS*
[CNB_DimTest].[TestUcode].[TestUcode].ALLMEMBERS*
[CNB_DimTest].[TestName].[TestName].ALLMEMBERS*
[CNB_DimSampleInfo].[LotNo].[LotNo].ALLMEMBERS*
[CNB_DimSampleInfo].[BatchNo].[BatchNo].ALLMEMBERS*
[CNB_DimSampleInfo].[COAReleasedBy].[COAReleasedBy].ALLMEMBERS*
[CNB_DimSampleInfo].[COAReleasedON].[COAReleasedON].ALLMEMBERS*
[CNB_DimSampleInfo].[SampleReferenceNo].[SampleReferenceNo].ALLMEMBERS*
[CNB_DimSampleInfo].[AnalysedBy].[AnalysedBy].ALLMEMBERS*
[CNB_DimSampleInfo].[AnalysedOn].[AnalysedOn].ALLMEMBERS
} ON ROWS
FROM
(
SELECT
StrToMember
(
"[CNB_DimSampleInfo].[COAReleasedON].[" + Format("2013-01-02","yyyy MM")
+ "]:STRTOMember([CNB_DimSampleInfo].[COAReleasedON].["
+
Format
("2013-01-02"
,"yyyy MM"
)
+ "]"
) ON COLUMNS
FROM Cube001
);
There is also StrToSet which is better in your circumstance as you're using the : operator which returns a set:
...
...
FROM
(
SELECT
StrToSet
(
"[CNB_DimSampleInfo].[COAReleasedON].[" + Format("2013-01-02","yyyy MM")
+ "]:[CNB_DimSampleInfo].[COAReleasedON].["
+
Format
("2013-01-02"
,"yyyy MM"
)
+ "]"
,CONSTRAINED
) ON COLUMNS
FROM Cube001
);
does the following definitely return the date formatted the same as your key values?
Format("2013-01-02","yyyy MM")
Do your key values for dates look like this?...
[CNB_DimSampleInfo].[COAReleasedON].[2013 01]
Your date member
[CNB_DimSampleInfo].[COAReleasedON].&[2013-01-02T03:20:00]
looks like a bad candidate for a user date parameter, as it's precise down to the minute (perhaps the second). Unless the user exactly matches the time, they'll get nothing. But perhaps you're enforcing exact matches by using a LimitToList select list in the UI.
To get this member name, you can format the input string like this:
format([Your Input Parameter],'yyyy-MM-ddThh:mm:ss')
I have tried out using filter as below
SELECT
[Measures].[Result] ON COLUMNS
,NON EMPTY
{
filter([CNB_DimSampleInfo].[COAReleasedON].members,instr([CNB_DimSampleInfo].[COAReleasedON].currentmember.member_caption,"2013-01-02")>0 or instr([CNB_DimSampleInfo].[COAReleasedON].currentmember.member_caption, "2013-04-01")>0)
*[CNB_DimProduct].[ProductUcode].[ProductUcode].ALLMEMBERS*
[CNB_DimProduct].[ProductDesc].[ProductDesc].ALLMEMBERS*
[CNB_DimTest].[TestUcode].[TestUcode].ALLMEMBERS*
[CNB_DimTest].[TestName].[TestName].ALLMEMBERS
} ON ROWS
FROM Cube002

How do I get day number from a date in abap?

I need to convert a date in 'MM/DD/YYYY' format to a number that say which day in the year it is. I.E '01/01/YYYY'=1 and '12/31/YYYY'=365. Is there any built in function to do this in ABAP? I've tried googling but I couldn't find any functions which did this
Here you go in one line of code:
DATA(g_day) = p_date - CONV d( p_date(4) && '0101' ) + 1.
It is absolutely unnecessary to rely on any function module that may or may not be present in your system. Just use basic built-in language elements:
DATA: l_my_date TYPE d, " note that the data type D is YYYYMMDD
l_jan_01 TYPE d, " This will be jan 1 of the needed year
l_day TYPE i.
l_my_date = ...whatever...
l_jan_01 = l_my_date.
l_jan_01+4 = '0101'. " or any other means to get the first day of the year.
l_day = l_my_date - l_jan_01 + 1.
You can use this function module: HR_AUPBS_MONTH_DAY.
You have to pass an initial date and an end date, and it will return the number of days in between (this is what you want):
CALL FUNCTION 'HR_AUPBS_MONTH_DAY'
EXPORTING BEG_DA = P_BEGDA " Here you should put the first day of the year
END_DA = P_ENDDA " Here you put the date
IMPORTING NO_CAL_DAY = P_CAL_DAY. " This is what you want

Write Query to Compare time in google fusion table

I want to get all the entries from google fusion table which satisfies the condition in a given date
Time column > specific time
https://www.googleapis.com/fusiontables/v2/query?sql=SELECT * FROM 1WjowbI77j1WFcn3IEtbwBymhVZh8jfmP_dg1epd9 WHERE Date = '2015-02-23' AND Time > '10:25:04'&key=AIzaSyCALoSz00ZY3zTL1D_xUTD9GMb3T1ocBdU
But it gives me all the entries as result..
fusion table :
https://www.google.com/fusiontables/data?docid=1WjowbI77j1WFcn3IEtbwBymhVZh8jfmP_dg1epd9&key=AIzaSyCALoSz00ZY3zTL1D_xUTD9GMb3T1ocBdU#rows:id=1
It is assumed that the type of Time column is Date/Time and format is H:mm:ss AM/PM
In that case, it seems the filtering on Date/Time column is not supported.
According to Row and Query SQL Reference documentation:
Filtering on DATETIME
When filtering on a column of type DATETIME, the <value> should be
formatted as one of the following supported formats:
MMM dd, yy
MM/dd/yy
MM-dd-yy
MMM-dd-yy
yyyy.MM.dd
dd-MMM-yy
MMM/yy
MMM yy
dd/MMM/yy
yyyy
Having said that, you could consider to apply filtering to the returned results as demonstrates the following JavaScript example:
var key = 'AIzaSyCALoSz00ZY3zTL1D_xUTD9GMb3T1ocBdU'
var sql = "SELECT * FROM 1WjowbI77j1WFcn3IEtbwBymhVZh8jfmP_dg1epd9 WHERE Date = '2015-02-23'";
var requestUrl = "https://www.googleapis.com/fusiontables/v2/query?sql=" + sql + "&key=" + key;
var timeKey = '10:25:04';
$.getJSON(requestUrl, function(data) {
var filteredRows = data.rows.filter(function(row){
var dtCur = Date.parse(row[3] + ' ' + row[7]);
var dtKey = Date.parse(row[3] + ' ' + timeKey);
if (dtCur > dtKey) {
return row;
}
});
//print
var output = JSON.stringify(filteredRows, null, 2);
$("#output").text(output);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre style="background-color: #c0c0c0" id="output"></pre>
I can't tell you exactly what's going on there, the only issue I see that you've omited the AM(PM)
The documentation mentions the DateTime-Formats supported for filtering, but I think this part of the documentation is outdated(see: Date query with the current date between two date_time columns ).
For me it works as expected when I make a copy of the table(you may do the same):
https://www.googleapis.com/fusiontables/v2/query?sql=SELECT * FROM 12tl0d4jZRkhPD9ZdBzP2QzK4TyEu-U_Dnn8kimft WHERE Date = '2015-02-23' AND Time > '10:25:04 AM'&key=yourKey
Maybe they run a validation when a table will be copied and store DateTime-columns internally in another format.

Difference between postgresql time and rails 3 app time

Scenario: I want to get the nearest time in relation to my system time.
I have written the following method inside my model:
def self.nearest_class(day, teacher_id, hour_min)
day_of_week = '%' + day + '%'
if hour_min == "06:00:00"
where('frequency like ? AND teacher_id = ? AND (start_time >= ?)', day_of_week, teacher_id, hour_min).order('start_time ASC')
else
where('frequency like ? AND teacher_id = ? AND (start_time >= localtime(0))', day_of_week, teacher_id).order('start_time ASC')
end
end
When I tested the above method in rails console using Lecture.nearest_class('Monday', 1, 'localtime(0)'), the following query was returned together with some data:
SELECT "lectures".* FROM "lectures" WHERE (frequency like '%Monday%' AND teacher_id = 1 AND (start_time >= localtime(0))) ORDER BY start_time ASC
But I am expecting no record because my system time is greater than any start_time recorded in the database. I have used the query from the console to pgadmin3 to test if the results are same. However, pgadmin3 showed no results, as expected.
Are there differences in postgresql time and rails app time? How can I be able to check these differences? I tried Time.now in the console and it is the same as SELECT LOCALTIME(0) from pgadmin3. What should be the proper way to get the records for nearest system time?
To further set the context, here's my controller method:
def showlectures
#teacher = Teacher.login(params[:id]).first
#lecture_by_course = nil
if #teacher
WEEKDAY.each do |day|
hour_min = "06:00:00"
if day == Time.now.strftime("%A") # check if day of week is same e.g. 'Monday'
hour_min = "localtime(0)" # system time with format: HH:MM:SS
end
#lecture_by_course = Lecture.nearest_class(day, #teacher.id, hour_min).first
if #lecture_by_course
break
end
end
end
render json: #lecture_by_course, include: { course: { only: [:name, :id] }}
end
I solved this by just passing the server Time.now instead of having a SQL check. { :| d'oh }