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.
Related
I would like to convert times from pacific to UTC. However, I must first convert the times to either PST or PDT depending on the date. Is there a SQL function that can do this, or does anyone have any advice for creating this function?
The link you provided is pretty much useless as a guide to timestamps. If you are going to work with timezones then store your timestamps in a timestamp with time zone field. The timezone will not actually be stored but the timestamp will be stored as a UTC. Whenever a timestamp is entered it is rotated to a UTC value. This makes working with value easier down the road. If you want to take into account DST transitions you will need to use full timezone names e.g. US/Pacific, as they cover the two offsets(PST/PDT) that constitute the standard/daylight savings timezones. As you found using the offset PST(-08) or PDT(-07) gets you a fixed offset regardless of date.
I want to add to my query TIMESTAMP column in UTC.
I know BigQuery has CURRENT_TIMESTAMP() function but it doesn't say how to convert it to UTC.
In MYSQL I would have do:
SELECT UTC_TIMESTAMP()
What is the equivalent in BigQuery (Standard SQL only) ?
A TIMESTAMP does not store or use a time zone, as described in the documentation. When parsing it it may have a time zone which is converted from when saving.
A timestamp represents an absolute point in time, independent of any time zone or convention such as Daylight Savings Time.
And more specifically
A timestamp is simply an offset from 1970-01-01 00:00:00 UTC, assuming there are exactly 60 seconds per minute. Leap seconds are not represented as part of a stored timestamp.
When querying for CURRENT_TIMESTAMP() it also shows explicitly it’s in UTC by having zero time zone offset. So it is a timestamp in UTC based on this.
There may be a problem using it since it does not store anything in UTC and it does not care about leap seconds. The specification says this:
Leap seconds are only observable through functions that measure real-world time. In these functions, it is possible for a timestamp second to be skipped or repeated when there is a leap second.
So if these leap seconds are needed it may be required to use another data type in specific time zone to handle leap seconds.
The conversion is also explained:
If your input contains values that use ":60" in the seconds field to represent a leap second, that leap second is not preserved when converting to a timestamp value. Instead that value is interpreted as a timestamp with ":00" in the seconds field of the following minute.
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.
We are using a Vertica database with table columns of type timestamptz, all data is inserted according to the UTC timezone.
We are using spring-jdbc's NamedParameterJdbcTemplate
All queries are based on full calendar days, e.g. start date 2013/08/01 and end date 2013/08/31, which brings everything between '2013/08/01 00:00:00.0000' and '2013/08/31 23:59:59.9999'
We are trying to modify our queries to consider timezones, i.e. I can for my local timezone I can ask for '2013/08/01 00:00:00.0000 Asia/Jerusalem' till '2013/08/31 23:59:59.9999 Asia/Jerusalem', which is obviously different then '2013/08/01 00:00:00.0000 UTC' till '2013/08/31 23:59:59.9999 UTC'.
So far, I cannot find a way to do so, I tried setting the timezone in the session:
set timezone to 'Asia/Jerusalem';
This doesn't even work in my database client.
Calculating the difference in our Java code will not work for us as we also have queries returning date groupings (this will get completely messed up).
Any ideas or recommendations?
I am not familiar with Veritca, but some general advice:
It is usually best to use half-open intervals for date range queries. The start date should be inclusive, while the end date should be exclusive. In other words:
start <= date < end
or
start <= date && end > date
Your end date wouldn't be '2013/08/31 23:59:59.9999', it would instead be the start of the next day, or '2013/09/01 00:00:00.0000'. This avoids problems relating to precision of decimals.
That example is for finding a single date. Since you are querying a range of dates, then you have two inputs. So it would be:
startFieldInDatabase >= yourStartParameter
AND
endFieldInDatabase < yourEndParameter
Again, you would first increment the end parameter value to the start of the next day.
It sounds like perhaps Vertica is TZ aware, given that you talked about timestamptz types in your answer. Assuming they are similar to Oracle's TIMESTAMPTZ type, then it sounds like your solution will work just fine.
But usually, if you are storing times in UTC in your database, then you would simply convert the query input time(s) in advance. So rather than querying between '2013/08/01 00:00:00.0000' and '2013/09/01 00:00:00.0000', you would convert that ahead of time and query between '2013/07/31 21:00:00.0000' and '2013/08/31 21:00:00.0000'. There are numerous posts already on how to do that conversion in Java either natively or with Joda Time, so I won't repeat that here.
As a side note, you should make sure that whatever TZDB implementation you are using (Vertica's, Java's, or JodaTime's) has the latest 2013d update, since that includes the change for Israel's daylight saving time rule that goes into effect this year.
Okay, so apparently:
set time zone to 'Asia/Jerusalem';
worked and I just didn't realize it, but for the sake of helping others I'm going to add something else that works:
select fiels at time zone 'Asia/Jerusalem' from my_table;
will work for timestamptz fields
If part of the data that RestKit gets from my server is a timestamp formatted as 2013-05-27 20:32:26 UTC, and later I want to find out the difference between the current time and this date, I do
NSTimeInterval difference = [[NSDate date] timeIntervalSinceDate:dateFromRestKit];
But this always seems to be off for me by 1 hour, and I'm guessing this is because I'm on BST, which is UTC+1. So I think that RestKit is ignoring the timezone specified in the date string, and is instead parsing the date using the current system timezone.
I understand that there may be some technical reason it ignores the time zone, but I found all this time stuff really difficult to get my ahead around so I'm not sure.
How can I work out the correct difference between the times?
Even if your timestamp contains the UTC format , try to specify the timezone for it again.
If you want the resulting difference of two date timezone in 'UTC' then , convert the other one in UTC format and then find the time Interval.
Do let me know , if any query. Or if possible , post your code , to get more clear about your question. (: