I am handling JSON data containing a date as per this example 'MON 2014-01-03 13:00:00 +GMT0000'
I need to compare records on date.
Is it best to load as strings and manipulate as and when required?
A requirement will be to select the highest and lowest dates for a particular criteria, and calculate the difference in seconds.
Thanks for looking.
Best solution for your problem is to use unixtimestamp (seconds since standard epoch of 1/1/1970)
Following is an example query, as to how you parse the timestamp-strings to unixtimestamp.
select unix_timestamp(REGEXP_REPLACE('MON 2014-01-03 13:00:00 +GMT0000','GMT',''),
"EEE yyyy-MM-dd HH:mm:ss Z") as unixtime from reqtable;
You will have more details here https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-DateFunctions
Also you should take a look into Java SimpleDateFormat to match the exact timestamp string pattern.
Related
I know that in BigQuery one can convert a timestamp by DATETIME(timestamp, timezone). But the names of the timezones in SQL are very poorly organized. I was wondering if there is a function or a way to convert from a time in
UTC to some other timezone using a string of number like "+00:04" or "4" where the number would indicate the amount of hours the timezone is ahead or behind the UTC time.
Thank you!
You can specify a timezone by supplying its UTC offset using the following format:
(+|-)H[H][:M[M]]
For example:
-07:00
SELECT CURRENT_DATETIME('-07:00'), DATETIME(CURRENT_TIMESTAMP(), '-07:00')
I have a date as a timestamp . I have to replace it every day on the date of beginning of 7:00:00 hours a day from the date of timestamp for example . 11-07-2016 7:00:00
For example, I have a date after changing 837072216 837241200 it will need a scalar function .
Your question is very unclear. In fact, there is no actual question...
Never-the-less, you appear to be asking about a conversion from timestamp to a date or time. This is not possible; despite its name, timestamp does not represent a date(time). It is used for versioning of rows, and it's deprecated and replaced by rowversion. Source.
I currently have a timestamp in this format Tue Jun 03 17:17:05 +0000 2014 in one column in my table. I want to count the number of records happening in specific intervals (15 minutes). I have tried to follow the answer found in Group records by time. Although my timestamp is in a different format and I haven't seen any support function available in SQLite to convert this. Is this possible in SQL?
The SQLite date and time functions can be used to convert a timestring to a canonical format, or to a Julian Day Number. Unfortunately, the SQLite date and time functions only accept timestring in a limited number of formats:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
If your timestring format has fixed field widths, you can use the substr function and the || string concatenation operator to convert it to a format SQLite understands. You'll have to use a case expression to convert the month names to numbers; here's an example.
You may use NEW_TIME in Oracle to convert the time to a specific timezone. Here is an example. This example is converting SYSDATE from PDT to GMT.
SELECT NEW_TIME (SYSDATE, 'PDT', 'GMT') FROM DUAL;
This thread is detailing how to add required minutes to your timestamp.
How to get date and time difference between 10/12/2010 07:35:02 PM and 2010-11-19 21:51:01.713. Where first date is in MM-DD-YYYY format and Second date is in YYYY-MM-DD Format Rest is time it is also in different format as first format has "pm" in it. Please let me know how to write a query in sql 08 to calculate date and time difference?
The datetime data type in SQL Server is actually a 8-byte number. It may be represented in different formats to please humans but the format has no meaning to SQL Server itself.
To calculate the time difference between to datetime values you can use the built-in DATEDIFF function, which you can find details about here: http://technet.microsoft.com/en-us/library/ms189794.aspx
This will work thanks to the SQL Server ability to parse formatted dates for us:
select datediff(day, '10/12/2010 07:35:02 PM', '2010-11-19 21:51:01.713')
-----------
38
I have 2 questions:
I want to compare a field whose data type is "Date" against a given date. The DB is oracle and being a mysql guy I'm finding it difficult to come up with simple queries.
The field("date_closed") stores date in UTC format (24-Aug-2011 18:55:11 for example) and I want to convert it to PST for comparison.
I tried this query but it returns some extra rows in the data set(obviously):
select * from table1 where trunc(date_closed)=to_date('2011-08-24','yyyy-mm-dd')
How do I covert to PST format before comparison?
In the same query how do I compare "date_closed" against the current date?
You need the NEW_TIME function
Dates don't include timezone in Oracle, and are assumed to be in the database timezone (which may by UTC but probably isn't). You should look at the TIMESTAMP WITH TIMEZONE data types.
Also, bear in mind that if you are comparing to the current date - I assume that you want to strip off the timestamp and compare only the day.
So, if new_time(date_closed,'GMT','PST') translates the date , your where clause will be comparing something like
trunc(new_Time(date_closed,'GMT','PST')) = trunc(sysdate)
to get all records with date_closed on the current day in PST.