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>
Related
I need to get all the values from a SQL Server database by day (24 hours). I have timestamps column in TestAllData table and I want to select the data which only corresponds to a specific day.
For instance, there are timestamps of DateTime type like '2019-03-19 12:26:03.002', '2019-03-19 17:31:09.024' and '2019-04-10 14:45:12.015' so I want to load the data for the day 2019-03-19 and separately for the day 2019-04-10. Basically, it is needed to get DateTime values with the same date.
Is this possible to use some functions like DatePart or DateDiff for that?
And how can I solve such problem overall?
As in this case, I do not know the exact difference in hours between a timestamp and the end of the day (because there are various timestamps for 1 day) and I need to extract the day itself from the timestamp. After that, I need to group the data by days or something like this and get block by block. For example:
'2019-03-19' - 1200 records
'2019-04-10' - 3500 records
'2019-05-12' - 10000 records and so on
I'm looking for a more generic solution not supplying a timestamp (like '2019-03-19') as a boundary or in a where clause because the problem is not about simply filtering the data by some date!!
UPDATE: In my dataset, I have about 1,000,000 records and more than 100 unique dates. I was thinking about extracting the set of unique dates and then kind of run a query in the loop where the data would be filtered by the provided day. It would look in such a way:
select * from TestAllData where dayColumn = '2019-03-19'
select * from TestAllData where dayColumn = '2019-04-10'
select * from TestAllData where dayColumn = '2019-05-12'
...
I might use this query in my code, so I may run it in the loop from Scala function. However, I am not sure that in terms of performance it would be ok to run separate unique dates extraction query.
Depending on whether you want to be able to work with all the dates (rather than just a subset), one of the easiest ways to achieve this is with a cast:
;with cte as (SELECT cast(my_datetime as date) as my_date, * from TestAllData)
SELECT * FROM cte where my_date = '2019-02-14'
Note when casting datetime to date, times are truncated, ie just the date part is extracted.
As I say though, whether this is efficient, depends on your needs, as all datetime values from all records will be cast to date, before the data is filtered. If you want to select several dates (as opposed to just one or two), however, it may prove overall quicker, as it reads the whole table once and then gives you a column upon which you can much more efficiently filter.
If this is a permanent requirement, though, I would probably use a persisted computed column, which effectively would mean that the casting is done once initially and then only again if the corresponding value changed. For a large table I would also strongly consider an index on the computed column.
I have a flat file as source which contains two columns named "Event begin time" and "event end time" that has both date and time in it .
How can I calculate MOU(minutes of usage) for it using
Informatica.
Please help me..
Thanks
Vinay
The DATE_DIFF function can be used for calculating the time duration:
DATE_DIFF( Event_End_Time, Event_Begin_Time, MI)
First you need Informatica to know that each of the 2 dates from the flat file are indeed dates and the format from the INCOMING date fields, you will do this by passing them to an expression transformaton i.e. if they are in 'DD/MM/YYYY HH24:MI:SS' then the expression to turn them into date/time in informatica will be TO_DATE (EVENT_BEGIN_TIME, 'DD/MM/YYYY HH24:MI:SS') (You'll have to do same for event end time... I've used name with underscores instead of spaces as informatica doesnt allow spaces in port names)
Then you'll use datediff to subtract the begin time from the end time... lets say you named the 2 variable ports which contain the above calculation as v_BEGIN and v_END, the calculation for minutes will be DATE_DIFF(v_BEGIN, v_END, 'MI')
Simplest way of achieving it :
Consider T1 and T2 as Start time and end time (Ensure that,both are in DATE Format).
In variable calculate T2-T1 : This will give you difference in days.
Multiply it by ( 24*60 ) will give you number of minutes.
So, 24*60*(T2-T1).
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.
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.)
I am trying to insert a time only value, but get the following error
ex {"SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM."} System.Exception
From the front end, the time is selected using the "TimeEdit" control, with the up and down arrows. The table in SQL Server has the fields set as smalldatetime. I only need to store the time. I use the following to return data to the app
select id,CONVERT(CHAR(5),timeFrom,8)as timeFrom,CONVERT(CHAR(5),timeTo,8)as timeTo
FROM dbo.Availability
where id = #id
and dayName = #weekday
How do I pass time only to the table?
Edit ~ Solution
As per Euardo and Chris, my solution was to pass a datetime string instead of a time only string. I formatted my result as per Time Format using "g".
Thanks
You can set the date to 1/1/1753 wich is date min value for datetime in MSSQL and then add the hour you want to store. Of course you have to consider this every time you need to get the value, but you can wrap that with some helpers.
Or you can use MSSQL 2008 and use the new TIME datatype.
Pick a date that is in the range(ie, 1/1/1970) and use it for everything you insert.
If you are only keeping track of the time, think about storing it in an int as an offset from midnight in whatever granualarity you need (seconds, minutes, hours, ...). You can then convert it to a TimeSpan in your code using the appropriate TimeSpan.From??() method. To go back the other way, you can use TimeSpan.Total?? and truncate if need be. If you need to do manual queries you can write a SQL function that will convert hours/mins/seconds to the equivalent offset.
I prefer this over using a datetime and picking an arbitrary day as it makes the purpose of the field clearer, which reduces confusion.
there is no such thing as Time in SQL, there is only DateTime.
For your purpose, I would use something like this to only return the time portion.
SELECT (GETDATE() - (CAST(FLOOR(CAST(GETDATE() as FLOAT)) AS DateTime)))
where GETDATE() is the datetime you want to filter.
When setting the time in the database, you will have to add '01/01/1901' or '01/01/1753' to the time.
Dont use CAST and Convert to varchar when working with datetime, its slow. Stick to floating numerical operations.