How can I get timestamp in minutes using PostgreSQL - database

When I send a form on my web, the insert query saves the current date on my DB using the function now().
Now, I'm trying to get this column in minute format to calculate other thinks that I need, but I don't know how to do that.
For example, I have this:
"2013-05-08 08:30:00"
And I want this (now 8.50):
"20" <- In minutes
Thanks

OK, let's suppose you have a table with a timestamp:
CREATE TABLE ex (t timestamp);
INSERT INTO ex VALUES ('2013-05-08 8:30'::timestamp);
And you want the difference in minutes between the column t and now(). You can get that using the extract function:
SELECT extract(epoch from (now() - ex.t)) / 60 FROM ex;
epoch is the number of seconds from the "epoch" for date and timestamp types, but is't just the number of seconds in the interval for interval types. By dividing it by 60 you get what you want (if you want an integer number of minutes just trunc it.)

Related

Error scatter plot time vs wind speed (data from a table) MATLAB

I am trying to make a simple scatter plot in MATLAB with time on the x-axis and wind speed on the y-axis. I loaded in my data from a text file as a table and then tried to use table2array to plot since it needs numeric values not table data. I also tried using double and got another error.
Error Message: Error using scatter (line 55) Input arguments must be numeric or objects which can be converted to double.
Error in windconversions (line 18) scatter(time,wnd_TS)
I'm not sure if having the times as strings will also be an issue.
T = readtable('allunderway.txt', 'HeaderLines', 2);
%A = table2array(T)
date = T(:,1);
time = T(:,2);
wnd_TD = T(:,10);
wnd_TS = T(:,11);
table2array(wnd_TS);
table2array(time);
%double(wnd_TS);
scatter(time,wnd_TS)
A simpler way to access the data contained within the table is to use the dot notation, as T.VarN, where N is the number of the column you are interested in.
In your code you are using only 'time' for the plot, however this consists of hours, minutes and seconds only. I suspect that for your graphical analysis you require the combination of both the date and the hours.
It is possible to perform arithmetic addition on datetimes, however it is required that the two variables have the same format. By converting both dates to format 'MM/dd/yyyy HH:mm:SS' you are actually modifying the data of the variables. However, as stated in the documentation:
Since the data in the first column of the file ("date") have no time information, the time of the resulting datetime values default to midnight. Since the data in the second column of the file ("time") have no associated date, the date of the datetime values defaults to the current date.
When you add the variables date and time together together, you can add the date ('MM/dd/yyyy') of date to the time ('HH:mm:SS') of time.
An example of datetime conversion and addition follows.
Variables date and time before conversion:
date = 05/04/2011
time = 00:00:42
After conversion:
date = 05/04/2011 00:00:00
time = 06/01/2018 00:00:42
Adding the two:
05/04/2011 00:00:42
The code which reads the table and plots the scatter graph:
%Read table.
T = readtable('allunderway.txt', 'HeaderLines', 2);
%Access data of interest from table.
date = T.Var1;
time = T.Var2;
wnd_TS = T.Var11;
%Convert variable time to datetime.
time = datetime(time,'Format','HH:mm:SS');
%Add hours, minutes and seconds to variable date.
date = datetime(date,'Format','MM/dd/yyyy HH:mm:SS');
%Add month, day and year to variable time.
time = datetime(time,'Format','MM/dd/yyyy HH:mm:SS');
%Combine date and time variables.
fullt = date+timeofday(time);
scatter(fullt,wnd_TS);
The output of the code is the required scatter graph:
You can find more information on combining date and time from separate variables here.
Why do so much extra work. You could have simply used scatter(datenum(T.time),T.wnd_TS). That should do the job and save all the extra effort.

show alert if months/years in two strings are different

i am creating a web app in mvc-5 angularjs,
i need to show data of the following date entered by the user(in textboxes(i have two textbox)), but the user can only fetch the data of 1 month like, if i want to see data i can enter(01-01-2016) to (31-01-2016) or (29-01-2016) to (31-01-2016)
i a user enter the date like (31-01-2016) to (03-02-2016) then the alert will be shown with the error or if he enters the date like (01-01-2016) to (31-01-2017), then also the alert should appear,
my webservice will validate the data in following strings(frmdate and dateto)
what i need to do here?
What you need to do is to find the difference between the From date and To date and check whether the difference is 30 days or 31 days depending upon your logic.
Save those date strings as Date objects.
var fromDate = new Date($scope.fromDate);
var endDate = new Date($scope.toDate);
In order to use a Date object in any sort of calculation, we must first retrieve the Date's internal millisecond value, which is stored as a large integer since you can't just add or subtract Date objects together.
// Convert both dates to milliseconds
var fromDate_ms = fromDate.getTime();
var endDate_ms = endDate.getTime();
Then Calculate the difference in milliseconds
var difference_ms = endDate_ms - fromDate_ms;
To obtain the number of days for a given number of milliseconds, we would divide by 86,400,000, the number of milliseconds in a day (1000 x 60 seconds x 60 minutes x 24 hours):
var differene_days = Math.round(difference_ms/86400000);
If differene_days is equal to 30 or 31 show the report else show error message.

Removing total days from Hhh:mm:ss

I have a varchar field that stores a duration for connections that occasionally goes into the days. I'm trying to separate the total number of days from the hh:mm:ss in this field so I can store them separately but I'm getting an out of range error whenever I try to convert this data to any type of datetime variation in order to datepart the days from the value and then deduct. Is their any way of doing this or will I need to resort to a crude charindex(':' just to remove the days?
You'll have to chop it up since it's not in a valid date or time format:
DECLARE #timeString VARCHAR(50) = '114:48:50'
SELECT Day_CT = LEFT(#timeString ,CHARINDEX(':',#timeString )-1)/24
,Tm = CAST(CAST(LEFT(#timeString ,CHARINDEX(':',#timeString )-1)%24 AS VARCHAR(12))+STUFF(#timeString ,1,CHARINDEX(':',#timeString )-1,'') AS TIME)
This is one of the many reasons why dates and times shouldn't be stored in string data types, it makes using them ugly.

Adding Time Intervals SQL

I need to add time entries from a table. The time entries are stored as
P2H30M (2 Hours 30 Minutes)
What would be the best way to go about this?
Are you adding time intervals to other time intervals? Or time intervals to another column that's defined as a datetime?
Either way, you'd want to create a function to convert those values to an integer (minutes) and then add the integers together and use another function to convert them back to your proprietary character format.
If you're wanting to add them to a datetime column you could then use:
UPDATE YourTable
SET YourDateTimeCol = DATEADD(MI, YourDateTimeCol, <yourminuteinteger>)
WHERE <whatever your where clause would be>

how to get diff b/w 2 columns which is in time format

I've 2 columns called record time and unload time which is in time format AM/PM and I require a new column called total time where I need to find difference between unload time and record time...
for example here is my table
record time unload time
11:37:05 PM 11:39:09 PM
11:44:56 PM 1:7:23 AM
For this I require a new column which finds the difference between these 2 columns.
Cab anyone suggest a query for this please?
why you cant go with datediff system function in SQL SERVER
select datediff(mi,'11:37:05 PM','11:39:09 PM')
mi/n is for minute
If you're doing timespan calculations within one 24 hour period, anishmarokey's response is correct. However, I'd add the date to the time field as well, if you're going to have cases where the load and unload might occur over midnight between two or more days.

Resources