How to convert a date to string in SAP HANA calculated column? - hana

I want to add months to a date (YYYYMMDD), which is a string currently '20190101'.
to add months i use:
addmonths(date('20190101'),12)
which would give me "2020-01-01" as a date
But I only want "202001" as my final output. I tried doing the below but doesn't seem to work!

TO_VARCHAR(ADDMONTHS(DATE('20190101'), 12), 'YYYYMM')
Similar to https://stackoverflow.com/a/46985953/18667225

Related

Extract year from timestamp in hive

I am writing the query to show the data entries for a specific year. Date is stored in dd/mm/yyyy hh:mm:ss.(Date TIMESTAMP - e.g. 12/2/2014 0:00:00).
I am trying to display the two columns(name, orderdate) filtered by a specific year(year from orderdate). The requirement is to enter the specific year(2010 or 2020 etc) not the entire date. I tried using date_format() and regexp_replace() with WHERE but nothing helped.
Can someone help me?
If your are storing the date -- incorrectly -- as a string, then you can use string functions to do what you want:
where orderdate like '__/__/2010%'
However, you should really put your effort into storing the date using a correct format -- YYYY-MM-DD for strings at least.

How to convert string YYYYMM to date in hive sql

I have column as string in the format "yyyymm" in hive table, and i would like to convert this to date with day as first of each month. for example 202105 string i would like this to be as date "2021-05-01".
Appreciate your help in this matter.
SELECT TO_DATE(CONCAT(SUBSTR('YYYYMM',1,4),'-',SUBSTR('YYYYMM',5,2),'-01'))
You can try using the following, replace "YYYYMM" with your date value string.

Oracle SQL: Transpose Columns to Rows After Using Extract

I have a table with column DATE. Date is 'dd/mm/yyyy' and I want only days. So I try with extract and return what I need, but I what using transpose for column to row.
The select statement is:
select EXTRACT (DAY FROM "DATE") DAY
from people;
Is this thing possible?
Thank you!
If you have a string, then just use the leftmost two characters:
select substr("DATE", 1, 2) as day
That said, you should not be storing dates as strings. It is wrong, wrong, wrong. You cannot use the built-in date/time functions. You cannot use inequality comparisons either. Fix your data model.
The date format doesn't matter. It is linked to your NLS local settings and this is how you see this.
To have it generic and extract DAY from the date do this:
select to_char(sysdate, 'DD') from dual;
Would return 07 since it's September 7th 2020.

Want week number from different columns of Year, Month & Day

I want week number from different columns of Year, Month & Day in same table of Hive QL.
IF you can give logic on SQL, that's also fine.
Concatenate the date, month and year into a proper date format and apply weekofyear().
Select weekofyear(cast(concat(year,"-",month,"-",date) as date)) from tablename.
Please note that I have used cast to convert the concatenated string into date.Howver, you might need to use different method based on your date format. Please refer to the below answer on handling string conversion to date formats.
Hive cast string to date dd-MM-yyyy

Storing just Month and Year in SQL Server 2008?

Is it possible to save the month/year in SQL Server 2008 with a regular datetime? I don't need a time stamp and the column is going to store unique mo/yr combinations (i.e. 10-11, 11-11, 12-11, etc,.).
Any recommendations?
Without knowing the intended use, it is not possible to advise correctly. However, storing month and year is easily done in at least three ways:
use a date field, but always store into it the first day of the month at midnight; then always custom format the date field for display
add integer year and month fields, and populate them by splitting a date
add an integer field where you encode it as year * 100 + month or some other useful scheme
Sql Server will store the datetime data in its entirety (year-month-day hour:min:sec.milliSec) no matter what.
When you store your dates, you can make the day the 1st of the month. Just format the dates how you want when you do your queries.
http://www.sql-server-helper.com/tips/date-formats.aspx
From SQLServer 2008 and onwards:
Save as a Date column and add the following check constraint to make sure the value is always the first of the month:
datepart(month, MyDate)<>datepart(month,dateadd(day, -1, MyDate))
You cant only store year and month in a DateTime column. Well, what you can do is default the rest of the values. ie: 2011.10.1.1 , 2011.11.1.1 like that.
Or you can store it as string.
SELECT RIGHT(CONVERT(VARCHAR(10), GETDATE(), 105), 7) AS [MM-YYYY]
You should try this