Starttime and endtime - sql

i have paramters fields as starttime(=DateAdd("d",-1,now())) and endtime (now()) and status( as dropdown ex:running,succededtec,.) in my report.... when i run the report with the whole timestamp it is working as expected but if i give only date in both the fields without time then am not getting last one days data( for example if starttime is 6/14/2017 and endtime is 6/18/2017 am getting data till 6/17/2017 only) .....one more scenario is when I have no endtime (like status is running we have no endtime) then am getting that.Can anyone help me out.
Thanks

The time defaults to 00:00:00 if not included.
So basically when you're comparing say
06-18-2017 07:52:11 <= 6-18-2017
you're actually comparing
06-18-2017 07:52:11 <= 6-18-2017 00:00:00
Since the time defaults to zero, and you will never get any 6-18 results returned unless they happened at exactly midnight. Set the timestamp in your compare, or use 6-19 as your compare date instead (with the risk of including a random record that happened at exactly midnight on 6-19).

Related

Modifying SYSDATE function

In one of my SQL queries, I am using
[... and z.READ_TIMESTAMP > TIMESTAMP_TO_EPOCH(TRUNC(SYSDATE-3)]
If I want the date to be exactly 5/31/2017, will I use 'SYSDATE' (date-n) function or some other expression? or how can a modify my query for 5/31/2017
If you want the date to be exactly 5/31/2017 then use TO_DATE() or TO_TIMESTAMP() depending on which data type you need (date or timestamp). As you are using SYSDATE already the the date data type should work.
-- e.g.
select
to_date('5/31/2017','mm/dd/yyyy')
, to_timestamp('5/31/2017','mm/dd/yyyy')
from dual
...
and z.READ_TIMESTAMP > TIMESTAMP_TO_EPOCH(to_date('5/31/2017','mm/dd/yyyy'))
HOWEVER
I suspect you may want more than just a way to establish a fixed date. For example are you asking for "how do I get that last day of the previous month?" which perhaps can be satisfied by using >= and the first day of current month like this:
...
and z.READ_TIMESTAMP >= TIMESTAMP_TO_EPOCH(trunc(sysdate,'MM'))
or if it really is the last day of the previous month can be achieved with a combination of LAST_DAY() and ADD_MONTHS()
and z.READ_TIMESTAMP >
TIMESTAMP_TO_EPOCH( last_day(add_months(trunc(sysdate,'MM'),-1)) )
Without knowing a great deal more about the nature of your data and query purpose please do note that each date you use when "truncated" also has the time set to 00:00:000 - so IF you data contains time within a day other than 00:00:00 then these 2 queries might NOT produce the same result
.... datetimecolumn > to_date('05/31/2017','mm/dd/yyyy') -- "a"
.... datetimecolumn >= to_date('06/01/2017','mm/dd/yyyy') -- "b"
For example "a" the entire 24 hour duration of 05/31/2017 would be included in the results, but for example "b" that same 24 hour duration would be excluded from results. In my experience the last day of any month isn't really the best method for locating date/time based data, instead usually it is the first day of the next month that produces the correct result.

xsl if test if the timestamp is within the past 24 hours

I would like to check if the processed date is today.
xsl:if test="ProcessedDate = 'Today'" won't work since the value of processed date is a timestamp more like 2016-06-07T09:06:54.827z
How can I mathematically convert it to check the processed date to see if it's in the past 24 hrs.
Perhaps within a ().

SQL WHERE timestamp BETWEEN MIDNIGHT AND MIDNIGHT

For my website I must select all messages sent between midnight (the previous night) and midnight (the next night). Basically, it's a 24 hours range.
I don't know how to do that as I store the date in a timestamp format in my DB.For example, last message was posted on 2013-10-18 11:23:35.
What I want is all message posted between 2013-10-18 00:00:00 and 2013-10-18 23:59:59.
Is that possible, if yes, how could I do that ?
You can calculate the required time in T-SQL as :
-- for previous day mid night as start time
declare #start_datetime datetime,#end_datetime datetime
Select #start_datetime = DATEADD(d,0,DATEDIFF(d,0,GETDATE()))
-- for current day mid night as end time
Select #end_datetime = DATEADD(SS,86399,DATEDIFF(d,0,GETDATE()))
select #start_datetime, #end_datetime
and then use you column name to check whether it exists between these two values.
To find out what happened between DatetimeA and DatetimeB, the sql keyword between is not your friend. It generally causes one to miss records. This construct is better.
where YourDateTimeField >= StartDateTime
and YourDateTimeField < JustAfterTheEndDateTime
In your case, you can simplify it with
where YourDateTimeField >= DateA
and YourDateTimeField < TheDayAfterDateA

SQL Query to pick data from a certain time range

I have two fields in my DB.
1) Invoice_Date and Invoice_Time
They recored date and time from my invoices.
I want to pick data under two queries:
1)Using From_Time DateTimePicker and To_Time DateTimePicker (they display as 19:20:18 PM)
Here I want to pick data for say between 13:10:12 PM to 18:10:20 PM, no matter what date it is.
2) Secondly I want data for a specific data range (from datatimepickers) to specific time range (time datatimepickers)
Please advise how to do it.
I tried:
Where InvTime between #Time1 and #Time2
But it did not generate any data, while data is there for the given time range.
Please help.
Thanks
I think if I understand you correctly you want you want to find all invoices between a start and stop time based on your invoice time. I will assume the invoice time is a date time field that may have different date values.
Where CONVERT(DATETIME(CONVERT(CHAR(5),invoice_time)) BETWEEN #StartTime AND #StopTime
You could also do the same conversion to your start and stop time. The key here is that all the datetime values you are comparing have the same date Since all we care about is time the date will just be the default.

datetime manipulation: replace all dates with 00:00 time with 24:00 the previous day

I have a table described here: http://sqlfiddle.com/#!3/f8852/3
The date_time field for when the time is 00:00 is wrong. For example:
5/24/2013 00:00
This should really be:
5/23/2013 24:00
So hour 00:00 corresponds to the last hour of the previous day (I didn't create this table but have to work with it). Is there way quick way when I do a select I can replace all dates with 00:00 as the time with 24:00 the previous day? I can do it easily in python in a for loop but not quite sure how to structure it in sql. Appreciate the help.
All datetimes are instants in time, not spans of a finite length, and they can exist in only one day. The instant that represents Midnight is by definition, in the next day, the day in which it is the start of the day, i.e., a day is closed on its beginning and open at its end, or, to phrase it again, valid allowable time values within a single calendar date vary from 00:00:00.00000, to 23:59:59.9999.
This would be analogous to asking that the minute value within an hour be allowed to vary from 1 to 60, instead of from 0 to 59, and that the value of 60 was the last minute of the previous hour.
What you are talking about is only a display issue. Even if you could enter a date as 1 Jan 2013 24:00, (24:00:00 is not a legal time of day) it would be entered as a datetime at the start of the date 2 Jan, not at the end of 1 Jan.
One thing that illustrates this, is to notice that, because of rounding (SQL can only resolve datetimes to within about 300 milleseconds), if you create a datetime that is only a few milleseconds before midnight, it will round up to midnight and move to the next day, as can be seen by running the following in enterprise manager...
Select cast ('1 Jan 2013 23:59:59.999' as datetime)
SQL server stoers all datetimes as two integers, one that represents the number days since 1 Jan 1900, and the other the number of ticks (1 tick is 1/300th of a second, about 3.33 ms), since midnight. If it has been zero time interval since Midnight, it is stll the same day, not the previous day.
If you have been inserting data assuming that midnight 00:00:00 means the end of the day, you need to fix that.
If you need to correct your existing data, you need to add one day to every date in your database that has midnight as it's time component, (i.e., has a zero time component).
Update tbale set
date_time = dateAdd(day, 1, date_time)
Where date_time = dateadd(day, datediff(day, 0, date_time), 0)