I want to extract date of each week's Monday for each week in hive. I can do that with Presto by using date_trunc function however in hive I don't have a direct function which can extract Monday Date.
My Presto code is similar to this,
select
a.abc_id
,cast(date_trunc('week', from_unixtime(p.xyz_date)) as timestamp) as xyz_week
from PQR
The output is for each id I will get Week but in the form of date format which is nothing but the date of every Monday of past weeks (user can specify how many week's he wants to assess).
I searched all the date functions of hive but is there a function which can replace date_trunc?
what you need is the date function : next_day(string start_date, string day_of_week)
For example: next_day('2018-02-22', 'Mon') = '2018-02-26''
My hive version is 1.4.
Related
I have
${date}= Get Current Date result_format=%Y-%m-%d
but I need yesterday date... how can I calculate -1day? I try format %d-1 but not work
The DateTime library has an Add Time To Date keyword which should do what you need:
${CurrentDate}= Get Current Date result_format=%Y-%m-%d
${newdatetime} = Add Time To Date ${CurrentDate} -1 days
I'm trying to figure out the cleanest way to do a comparison in Teradata SQL Assistant. I have the scheduled start date (TimeStamp), the Schedule start time (varchar), actual start and end times (TimeStamp). I need to consolidate the scheduled start date and time and be able to compare it to the actual start and end date and time without modifying the original data (because it's not mine). I realize that the Scheduled Start Time [SST] is in a 24 hour time format with a AM/PM suffix, but like I said before, I can't change that.
I tried to do select cast(substr(scheduled_start_date,1,5) as TIMESTAMP(0)) from DB.TBL but am getting the "Invalid timestamp" error. There is example table data below.
Sch Start Date Sch Start Time Actual Start Actual End
09/11/2017 00:00:00 11:30 AM 09/11/2017 11:34:16 09/11/2017 11:58:00
05/26/2017 00:00:00 15:30 PM 05/26/2017 15:40:00 05/26/2017 15:55:15
11/06/2017 00:00:00 19:30 PM 11/06/2017 21:25:00 11/06/2017 21:45:00
Thanks!
You need to cast the schedule start time as an Interval, then you can easily add it to the start date:
scheduled_start_date
+ Cast(Substr(scheduled_start_time, 1,5) AS INTERVAL HOUR TO MINUTE)
A start date which is a timestamp seems to indicate this was ported from Oracle/SQL Server?
And a 24 hour time format with a AM/PM suffix is also quite strange.
A couple things to try:
Convert the separate Scheduled Date and Scheduled Time fields into strings, concatenate them, and feed that into a TIMESTAMP CAST. Something like:
SELECT
CAST(CAST(Scheduled_Date AS DATE) AS VARCHAR(25)) AS Date_String,
CAST(CAST(Scheduled_Time AS TIME FORMAT 'HH:MM BB') AS VARCHAR(25)) AS Time_String,
CAST(TRIM(Date_String) || ' ' || TRIM(Time_String) AS TIMESTAMP(0)) AS MyTimestamp
Cast the Scheduled Time field as a TIME data type. Cast the Scheduled Date field as a DATE data type. Then somehow combine the two into a TIMESTAMP field -- either with a CAST or some kind of timestamp constructor function (not sure if this is possible)
Option 1 should work for sure as long as you properly format the strings. Try to avoid using SUBSTRING and instead use FORMAT to cast as DATE/TIME fields. Not sure about Option 2. Take a look at these link for how to format DATE/TIME fields using the FORMAT clause:
https://www.info.teradata.com/HTMLPubs/DB_TTU_16_00/index.html#page/SQL_Reference%2FB035-1143-160K%2Fmuq1472241377538.html%23wwID0EPHKR
https://www.info.teradata.com/HTMLPubs/DB_TTU_16_00/index.html#page/SQL_Reference/B035-1143-160K/cmy1472241389785.html
Sorry, I don't have access to a TD system to test it out. Let me know if you have any luck.
In SQLite, I have a 'date' field that's varchar(20), as an example dates currently read '12-AUG-15'. I need to split the date field into 4 separate fields: 'day', 'month', 'year', and 'month_num'. Problem is I don't know how to convert month names into numbers as well as the year into a 4 digit year. I'm thinking of using case and string concatenation functions? Any advice how to start it is well appreciated.
SQLite has very limited date and string functions. The date functions understand ISO 8601. You're better off in the long run converting to ISO 8601 and then using the date functions. It will sort better, and most everything understands ISO 8601 format.
SQLite doesn't have an easy way to do this, but you can write a user defined function to do the conversion. Most SQLite drivers allow you to write it in whatever language you're using. Here's an example in Ruby.
require "sqlite3"
require "date"
# Open a database
db = SQLite3::Database.new "test.db"
# Create a SQLite function date_to_iso()
db.create_function( "date_to_iso", 1 ) do |proxy, date|
# Convert 12-AUG-15 to 2015-08-12 (ISO 8601 format)
iso = Date.strptime(date, "%d-%b-%y");
# This is how you return the result via a FunctionProxy
proxy.result = iso.to_s
end
# Now you can use date_iso_iso() in SQLite queries in this process.
db.execute("UPDATE stuff SET date = date_to_iso(date)")
Then you can use strftime to query the individual date parts.
select
strftime("%d", date) as day,
strftime("%m", month) as month,
strftime("%Y", year) as year
from whatever;
Unfortunately, SQLite doesn't have a way to convert to the month name. You could write a big CASE statement.
select case strftime("%m", month)
when 1 then 'JAN'
when 2 then 'FEB'
...
end
from whatever;
Or you could add another user defined function to do the conversion.
But since you now have a standard date format, it's probably better to just return the whole ISO 8601 date and let the application do what it wants with it.
I am using SQLite Database and in one my table has field purchased_date (TEXT ,since DATE is not in SQLLite)
Now I want to run query that return me all the results where user purchased in Month of February 2012
I am storing Dates in following format
Tue Mar 27 09:38:31 BST 2012
Is it possible to run query for above date format or do I need to put in different format ?
You can use the strftime built-in function to extract the month from the stored text value and group by this.
A full list of the datetime functions can be found here http://sqlite.org/lang_datefunc.html
I have a Report that I send a parameter to as 'WeekStart'. This is based on a selection a user makes on a datepicker.
I'm using the following to extract the week of the year:
=DatePart("ww", Parameters!WeekStart.Value)
The problem I'm having is that when I pick the day 03/01/2012 (dd/MM/yyyy format), the week of the year is returned as 9, which would technically be true had the date been 03/01/2012 with a dateformat of MM/dd/yyyy.
I've tried using CDate, FormatDateString etc but nothing seems to be working. I either get #Error or it returns as the 9th week of the year.
Any suggestions?
What you can do is this:
use YourDatabase
go
set dateformat dmy
go
That will set the date format for your database, and that should get your DATEPART function working as expected.
SET DATEFORMAT (MSDN)