I have two time value like
Time1=23:59:59:999
Time2=23:59:59:999
when i add up these two time (Time1+Time2) and i want result will be 47:59:58 rather than 23:59:58.
How can i do this? please suggest!!
Let's look at times and durations:
A datetime is a moment in time, say June 28, 1978 at 15:23.
A time without a datepart is a repetitive moment in time, e.g. "I get up every day at 7:00". (This is what you are using. But I get up at 8:00 and go to sleep at 23:00 doesn't make 8 + 23 = 31. It makes no sense to add times.)
Then there is timespan (e.g. from 2016-01-01 3:00 to 2016-01-02 13:00).
And then there is duration (e.g. six minutes). This is what you want to deal with.
You are storing a duration in a time, which is not really the appropriate data type. As SQL Server does not provide a special data type for a duration, you can use a numeric type for this and store seconds or microseconds or whatever you think appropriate. These you can easily add (provided both values have the same unit, e.g. microseconds).
As to displaying the duration you can write a function providing you with the format you like best for a duration (e.g. '913 days, 9 hours, 5 minutes, and 55.123 seconds').
Actually, that is really hard in SQL Server. How about '1 23:59:58'? If so:
select cast(time1 as datetime) + time2
If you actually want the format in HH:MM:SS format, then you will need to do a lot of string manipulation.
Related
I can't seem to find a question/answer that works for what I'm trying to achieve. Currently, this is how my DB outputs a timestamp:
2015-08-18T19:43:04.738-06:00
However, I would like it to appear as such in the column:
2015-08-18T19:43:04.738 America/Denver
Google has recently changed their formatting options and instead of downloading the output and performing a find/replace, I want an output that doesn't require additional work. I looked on SO and have tried using trim and replace but having no luck.
Thanks for the help in advance!
For whatever reason, the one we've used since February (third from the bottom) no longer works.
2015-08-18T19:43:04.738-06:00 is not quite the right format. Google does not accept milliseconds (which is annoying if they don't just ignore it). You need to send 2015-08-18T19:43:04-06:00. They may have become more strict in what they accept.
Try date_trunc('second', yourtime).
It's not possible to accurately translate an offset like -0600 to a time zone like America/Denver. They say two different things.
-0600 says, with absolute certainty, that this time is 6 hours behind UTC. 12:00:00-06:00 and 18:00:00Z (Z represents UTC) are the same time.
America/Denver means to interpret this timestamp under the rules applicable to the city of Denver, Colorado, USA at that time. To figure out what time it is in UTC you need to look up the offset rules for Denver, Colorado, USA. The offset will change depending on the time of year, usually because of daylight savings time. Because the rules change, it's important to apply the rules as they were at that time.
For example, 2006-03-15 12:00 America/Denver is -0700. But the next year on 2007-03-15 12:00 America/Denver is -0600. Between 2006 and 2007 the daylight savings time rules in the US changed.
Whereas -06:00 avoids all that and simply says the time is offset from UTC by six hours.
You could fake it by simply replacing the offset with America/Denver. So long as you're only sending recent times that should work. You'll be off by at most an hour. But don't do that.
Unless Google Ads specifically needs a time zone there's no point in sending them one. Internally, Postgres is storing your times in UTC anyway and translating them to your server's time zone, America/Denver. Send Google UTC. And, as noted above, chop off the milliseconds.
select date_trunc('second', '2015-08-18T19:43:04.738-06:00'::timestamp with time zone at time zone 'UTC') as datetime;
datetime
---------------------
2015-08-19 01:43:04
I'm pulling a time that is seconds from midnight (example: 55800). This is supposed to be 3:30pm (55800/3600) = 15.50 which is 1530 in military time, which is 3:30pm.
I can probably figure out how to do this in straight SQL but the query itself is a function so the format I need I believe is different. What would be the correct syntax in SSRS to make that time 3:30 PM?
A somewhat quick and crude way of handling this is in an expression:
Format(DateAdd(DateInterval.Second, Fields!Seconds.Value, CDate("1970-01-01")), "hh:mm tt")
Breaking it down, it doesn't matter what date you choose since you are only concerned with the time value. You also don't need to set the time to midnight as it'll default to midnight.
The expression adds your seconds from midnight using DateAdd. The result of this would look something like:
1970-01-01 15:30:00
Finally, Format with the custom format string hh:mm tt will take the time part of the datetime value. tt specifies whether AM/PM is included in the final formatted output.
I am collecting the Worked Hours from users, the format will be like:
1:30
0:45
2:15
8:00
6:19
In SQL SERVER, what kinda of datatype should I use to store that value? I will need to sum this value in some point.
Do not store worked hours, store worked minutes. Your values would become
Time int
---- ---
1:30 90
0:45 45
2:15 135
8:00 480
6:19 379
You will be able to convert it to display as necessary by dividing by 60 and taking the remainder.
Summing, on the other hand, will be very easy.
SQL Server unfortunately doesn't have a data type that represents a time span1. I would normally suggest an int column and a bit of naming so that it's clear that it's in seconds/minutes/hours - whatever the granularity is that you require.
1It has a time data type but that represents a time of day, not a time span. It doesn't make sense to add two times of day together, and this being so, the time data type doesn't support addition.
If I want to store date and time, is it better to store them in a separate date and time or use a single datetime?
When should we use date and time instead of a single datetime?
I want to filter my queries either using date or time.
When you are talking about a moment in time, whether a universal moment, or a specific date and time on someone's local calendar, you use a datetime. If you want to be sure that you are talking about an exact moment in time, regardless of the observer, then you use a datetimeoffset.
If you are storing just a date then you mean a date without a time component, meaning "any time on this date".
If you are storing just a time then you mean a time without a date component, meaning "this time on any date", or "this time on a date determined by some other means".
There is no practical purpouse to having both a date and a time that are about the same thing, sitting on the same row. Just use a datetime for that.
In SQL Server 2008 you have date and time data types so this becomes a non issue.
If it is good choice it really depends by your business and how you will query you data.
If for example you want to know all the orders places between 1 and 2 PM for any day using a separated Date and Time column will make it quicker
If you intentionally do not care about the time, it's more efficient to store this data as a date datatype. Think a customer birthday column, there's not too many cases I can think of that would use this time. If there happens to be a time attached to it (often a bug), this needs to be removed via a convert statement in order to do a compare. It also consumes additional space if you don't need these values (3 bytes compared to 8).
I think it's similar to having a status code table with the id as a bigint instead of a tinyint or the like (depending on how many status codes you would plan to have).
It's just a matter of what you're using the data for, if you think there's a good chance you'll ever need the that data, then use datetime, otherwise use date.
Nothing brilliant about separating date and time,
Better you save date and time in Same column,
Here they have discussed the same issue check it : are-there-any-good-reasons-for-keeping-date-and-time-in-separate-columns
you can also get date and time separately by query
SELECT
CONVERT(VARCHAR(10),GETDATE(),111) as DatePart,
convert(varchar(15), getdate(), 108) TimePart
I need to get the current date, but ignoring minutes and seconds, and then calculate an interval.
For example say that it's 7:30am right now, and I need to see how many hours it is until 5pm the next day, not counting the minutes, which means 7:30am should equal to 7am in this scenario, and the result should be 22 hours.
This could be easily done with
- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate
assuming that I'm able to create both ends of the interval.
The only option I thought of is using NSDateFormatter to output the current date into a string without the minutes, and then parse it back. But that doesn't seem like a very elegant and efficient solution.
What I basically need to do is trim a NSDate to a specific precision, such as days, and trim the rest (7:50 to 7:00, etc.).
Or is there any simple way I can do this the smarter way, where 7:50 gets roudned to 8:00 instead of 7:00?
I need to do this on iOS 5.
The calculations can be done with NSCalendar, instead of parsing you can use NSDateComponents.
When you take the hours and minutes from NSDateComponents, you can easily apply your rounding and convert the components back to a date.