Quantcast
Channel: SCN : All Content - All Communities
Viewing all articles
Browse latest Browse all 3212

Date derivation from Year in HANA- SQL

$
0
0

Hi All,

 

I am struggling with a requirement where I have 2 table - Source and target table.

 

My source field has one a field called Year which is of type Integer,

and my target has 3 field Year type Integer, "Record start date", and "Record End Date" type Date

 

Record start date will be the start of the year of Year field record i.e. if the Year record field is '2014' "Record start date" will be '2014-01-01' and "Record End Date" will be '2014-12-31'

 

to achieve this I have used SQL command for Select statement As 

 

----First Day

Select ADD_MONTHS(NEXT_DAY(LAST_DAY(TO_CHAR("Year"))),-1) from <Schema>.<Table1>

 

----Last Day

Select ADD_MONTHS(LAST_DAY(TO_CHAR("Year")),11) from <Schema>.<Table1>

 

and it shows me the expected value as

----First Day

Jan 1, 2014


----Last Day

Dec 31, 2014

 

When the record has Year value as  2014

 

But when I Insert it into a the field of the table2, it throws an error as

invalid DATE, TIME or TIMESTAMP value: Error while parsing -1 as DATE at function __typecast__()

 

SQL for the same is

Insert Into <Schema>.<Table2>( F1, F2....."Year", "Record start date", "Record End Date")

Select F1, F2.. , "Year", ADD_MONTHS(NEXT_DAY(LAST_DAY(TO_CHAR("Year"))),-1) , ADD_MONTHS(LAST_DAY(TO_CHAR("Year")),11)

from <Schema>.<Table1>;

 

Can some one tell me what function should I try to make it work?

 

I have tried TO_DATE(.....) it didn't worked same error as above, or any other Suggestion is welcome.

 

Regards,

Abhi


Viewing all articles
Browse latest Browse all 3212

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>