How to Fetch Month Part if Date is in mmYYY format(mm-->Month,YYYY--->Year)? - hive

I need to fetch 'month' from one column in which date stores in mmYYYY(032019) format.I need to fetch the month column and if months value is in 01,02,03(Jan,Feb,march) then ,i need to generate new column as 2018-19.if value is 072019 then need to generate new value as 2019-20. Im trying below code but not getting desired result. Can someone please guide me how should i implement this.
CASE WHEN month(to_date(from_unixtime(unix_timestamp(dt), 'dd-MM-yyyy'))) IN (01,02,03) THEN
CONCAT(CONCAT(year(to_date(from_unixtime(unix_timestamp(dt), 'dd-MM-yyyy'))))-1,'-'),
substr(year(to_date(from_unixtime(unix_timestamp(dt), 'dd-MM-yyyy')))),3,4))
ELSE CONCAT(CONCAT(year(to_date(from_unixtime(unix_timestamp(dt), 'dd-MM-yyyy')))),'-'),
SUBSTR(year(to_date(from_unixtime(unix_timestamp(dt), 'dd-MM-yyyy'))))+1,3,4)) end
because of my dt column is not in 'dd-mm-yyyy' format so its not giving correct value due to some scenario i don't want to use direct sub-string function to fetch month part from dt. I want to fetch it using month function or any other function which can fetch month part and then accordingly generate output column based on month part.
Sample Data.
dt=012019
output : 2018-19
dt=022019
output : 2018-19
dt=032019
output : 2018-19
dt=042019
output : 2019-20
dt=052019
output : 2019-20
dt=062019
output : 2019-20

below code worked for me.
CASE WHEN month(to_date(from_unixtime(unix_timestamp(dt,'MMyyyy')))) IN (01,02,03)
THEN concat(concat(year(to_date(substr(from_unixtime(unix_timestamp(dt, 'MMyyyy')),1,10)))-1,'-')
,substr(year(to_date(from_unixtime(unix_timestamp(dt, 'MMyyyy')))),3,4))
ELSE concat(concat(year(to_date(from_unixtime(unix_timestamp(dt, 'MMyyyy')))),'-'),
substr(year(to_date(substr(from_unixtime(unix_timestamp(dt, 'MMyyyy')),1,10)))+1,3,4)) END

Related

Extracting semester of a given date

I need to extract the semester of a given date and I ended up with this code:
df['semester']= df.my_date.dt.year.astype(str) + '-S'+ np.where(df.my_date.dt.quarter.gt(2),2,1).astype(str)
Can someone explain to me how this part works:
np.where(df.my_date.dt.quarter.gt(2),2,1)
Check numpy.where if condition is True, here Series.dt.quarter is greater by Series.gt like 2 set 2 else 1.
So it is same like:
np.where(df.my_date.dt.quarter > 2,2,1)

MDX Date Formatting

Can any one please tell me how to format date in MDX queries? We dont use SSRS to generate report ,we have our own customised reporting tool built on SSAS.Date filter sends date in yyyy/mm/dd format . As of now we dont have a date dimension. My date member looks like:
[CNB_DimSampleInfo].[COAReleasedON].&[2013-01-02T03:20:00].
How can I format date in STRTOmemeber? I have tried doing this. My question is how will the value coming from user suit my member format as below. I know ssrs does it easily but we are not using SSRS. Below is my Code
my code
SELECT
[Measures].[Result] ON COLUMNS
,NON EMPTY
{
[CNB_DimProduct].[ProductUcode].[ProductUcode].ALLMEMBERS*
[CNB_DimProduct].[ProductDesc].[ProductDesc].ALLMEMBERS*
[CNB_DimTest].[TestUcode].[TestUcode].ALLMEMBERS*
[CNB_DimTest].[TestName].[TestName].ALLMEMBERS*
[CNB_DimSampleInfo].[LotNo].[LotNo].ALLMEMBERS*
[CNB_DimSampleInfo].[BatchNo].[BatchNo].ALLMEMBERS*
[CNB_DimSampleInfo].[COAReleasedBy].[COAReleasedBy].ALLMEMBERS*
[CNB_DimSampleInfo].[COAReleasedON].[COAReleasedON].ALLMEMBERS*
[CNB_DimSampleInfo].[SampleReferenceNo].[SampleReferenceNo].ALLMEMBERS*
[CNB_DimSampleInfo].[AnalysedBy].[AnalysedBy].ALLMEMBERS*
[CNB_DimSampleInfo].[AnalysedOn].[AnalysedOn].ALLMEMBERS
} ON ROWS
FROM
(
SELECT
StrToMember
(
"[CNB_DimSampleInfo].[COAReleasedON].[" + Format("2013-01-02","yyyy MM")
+ "]:STRTOMember([CNB_DimSampleInfo].[COAReleasedON].["
+
Format
("2013-01-02"
,"yyyy MM"
)
+ "]"
) ON COLUMNS
FROM Cube001
);
There is also StrToSet which is better in your circumstance as you're using the : operator which returns a set:
...
...
FROM
(
SELECT
StrToSet
(
"[CNB_DimSampleInfo].[COAReleasedON].[" + Format("2013-01-02","yyyy MM")
+ "]:[CNB_DimSampleInfo].[COAReleasedON].["
+
Format
("2013-01-02"
,"yyyy MM"
)
+ "]"
,CONSTRAINED
) ON COLUMNS
FROM Cube001
);
does the following definitely return the date formatted the same as your key values?
Format("2013-01-02","yyyy MM")
Do your key values for dates look like this?...
[CNB_DimSampleInfo].[COAReleasedON].[2013 01]
Your date member
[CNB_DimSampleInfo].[COAReleasedON].&[2013-01-02T03:20:00]
looks like a bad candidate for a user date parameter, as it's precise down to the minute (perhaps the second). Unless the user exactly matches the time, they'll get nothing. But perhaps you're enforcing exact matches by using a LimitToList select list in the UI.
To get this member name, you can format the input string like this:
format([Your Input Parameter],'yyyy-MM-ddThh:mm:ss')
I have tried out using filter as below
SELECT
[Measures].[Result] ON COLUMNS
,NON EMPTY
{
filter([CNB_DimSampleInfo].[COAReleasedON].members,instr([CNB_DimSampleInfo].[COAReleasedON].currentmember.member_caption,"2013-01-02")>0 or instr([CNB_DimSampleInfo].[COAReleasedON].currentmember.member_caption, "2013-04-01")>0)
*[CNB_DimProduct].[ProductUcode].[ProductUcode].ALLMEMBERS*
[CNB_DimProduct].[ProductDesc].[ProductDesc].ALLMEMBERS*
[CNB_DimTest].[TestUcode].[TestUcode].ALLMEMBERS*
[CNB_DimTest].[TestName].[TestName].ALLMEMBERS
} ON ROWS
FROM Cube002

To get last month from user selected date

I want to select last month from user. But it shows only data empty set. Doesn't return any data. What is wrong with the below code?
{$select->where("DATE_FORMAT(DATE(receive_dt),'%Y-%m') = DATE_SUB(DATE('".$v."'),INTERVAL -1 MONTH)");}
This is ok for about question want.so i updated it like this....
$select->where("DATE_FORMAT(DATE(receive_dt),'%Y-%m') = DATE_FORMAT(DATE(DATE_SUB(DATE('" . $v . "-1'),INTERVAL 1 MONTH)),'%Y-%m')");

Logical operator sql query going weirdly wrong

I am trying to fetch results from my sqlite database by providing a date range.
I have been able to fetch results by providing 3 filters
1. Name (textfield1)
2. From (date)(textfield2)
3. To (date)(textfield3)
I am inserting these field values taken from form into a table temp using following code
Statement statement6 = db.createStatement("INSERT INTO Temp(date,amount_bill,narration) select date,amount,narration from Bills where name=\'"+TextField1.getText()+"\' AND substr(date,7)||substr(date,4,2)||substr(date,1,2) <= substr (\'"+TextField3.getText()+"\',7)||substr (\'"+TextField3.getText()+"\',4,2)||substr (\'"+TextField3.getText()+"\',1,2) AND substr(date,7)||substr(date,4,2)||substr(date,1,2) >= substr (\'"+TextField2.getText()+"\',7)||substr (\'"+TextField2.getText()+"\',4,2)||substr (\'"+TextField2.getText()+"\',1,2) ");
statement6.prepare();
statement6.execute();
statement6.close();
Now if i enter the following input in my form for the above filters
1.Ricky
2.01/02/2012
3.28/02/2012
It fetches date between these date ranges perfectly.
But now i want to insert values that are below and above these 2 date ranges provided.
I have tried using this code.But it doesnt show up any result.I simply cant figure where the error is
The below code is to find entries having date lesser than 01/02/2012 and greater than 28/02/2012.
Statement statementVII = db.createStatement("INSERT INTO Temp5(date,amount_rec,narration) select date,amount,narration from Bills where name=\'"+TextField1.getText()+"\' AND substr(date,7)||substr(date,4,2)||substr(date,1,2) < substr (\'"+TextField2.getText()+"\',7)||substr (\'"+TextField2.getText()+"\',4,2)||substr (\'"+TextField2.getText()+"\',1,2) AND substr(date,7)||substr(date,4,2)||substr(date,1,2) > substr (\'"+TextField3.getText()+"\',7)||substr (\'"+TextField3.getText()+"\',4,2)||substr (\'"+TextField3.getText()+"\',1,2)");
statementVII.prepare();
statementVII.execute();
statementVII.close();
Anyone sound on this,please guide.Thanks.
you need to use an Or clause together with brackets:
WHERE name='....' AND (yourDateField<yourLowerDate OR yourDateField>yourHigherDate)

Storing Select Query result in Variable Ruby on Rails

I wanted to know how we could store the result of my select query on a variable.
#ppt2 = Ppt.select('slide_name').where('id=?',4)
#ppt1 = Ppt.update_all({:time2=>#ppt2},['id like ?','1'])
Here, slide_name and time2 are both text attributes of the same table ppt.
What happens on the above execution is that the time2 field in id=1 gets the value "#ppt2" whereas I want it to get the value of slide_name from id=4 which does not get stored in #ppt1.
In other words, how do I store the value of the select query in #ppt2 so that it can be used in the next line?
Any help is appreciated.
Call the slide_name method on your first result.
#ppt2 = Ppt.select('slide_name').find(4).slide_name