Conversion of 01 to jan , 02 to feb including year in the field - oracle

I have a field containing 201402, 201404, here I want to convert 02 to Feb and 04 to April. Is it possible to do that? This field contains entries of all months. Please help.

There is a lot that you can do in terms of datetime format models within Oracle. A simple example below (replace the string literal within the TO_DATE(...) with your field if it is a string or replace the entire TO_DATE with your field if it is already of data type DATE):
SELECT TO_CHAR(TO_DATE('201402','YYYYMM'),'YYYY-MONTH') FROM DUAL;
SELECT TO_CHAR(TO_DATE('201404','YYYYMM'),'YYYY-MONTH') FROM DUAL;

Related

Grabbing all rows from a database that have specific column values in laravel

I am dealing with a table that has a bunch of short names, ex.
some_id
long_name
short_name
94
March
mar
19
April
apr
0
June
jun
2
September
Sep
I want to grab only a few supported short names, ex. mar, sep, jun.
How would I go about doing it in laravel?
Currently I have something like this:
$this->result = DB::table('table_above')->get();
But this just grabs every table. I was thinking of adding the where command, but not sure how to check for multiple values.
$this->result = DB::table('table_above')->where('short_name', [somehow say either mar, sep, jun])->get();
At the end I'd like an array that holds a dictionary of rows (i.e $result) with the supported short names (i.e here mar, june, sept).
Try this query
DB :: table ('table_above')->whereIn('short_name', ['mar', 'sep'])->get()

Failed to parse input string "31-Dec-2019" ERROR

Data Type of my date column in the big query table is String.
Format of the date: 31-Dec-2019
I have records for 2018, 2019, 2020 years.
Now I want to filter out the data after 2020-01-01.
With the below code, where-condition is not working here and returning all the records of the table.
Select
*
from table T
where date > '2020-01-01'
Tried the below and their respective errors:
PARSE_DATE('%d-%m-%Y',SUBSTR(date,1,12)) - Failed to parse input string "31-Dec-2019"
CAST( date as DATE) > '2020-01-01' -- returning all the records in the table/where condition is not working
Can someone please help me with this?
You need to use %b for abreviated month name.
select PARSE_DATE('%d-%b-%Y',SUBSTR('31-Dec-2019',1,12))

I have a varchar value 1st March 2017 that i need as a date 01-MAR-2017 in pl/sql

SELECT TO_DATE('1st March 2017','DD MON YYYY') from dual
It's ok with SELECT TO_DATE(01 March 2017','DD MON YYYY') from dual , doesn't like the 'st'
I think you need something like regex:
SELECT TO_DATE(
regexp_replace('1st March 2017','^(\d+)\w+','\1')
,'DD MON YYYY') from dual
Alas, you can't do it directly: https://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm#BABGDDFB
Notes on date format element suffixes:
When you add one of these suffixes to a datetime format element, the return value is always in English.
Datetime suffixes are valid only to format output. You cannot use them to insert a date into the database.
Which actually means they work only with to_char() to transform a date into a string; they don't work with to_date() to convert a string to a date.
So you will have to play dirty tricks - perhaps regexp_replace to get rid of st. Like Michael has shown already.
Hope here is your answer
SELECT to_date('1st march 2017','dd"st" month yyyy') "Date" FROM dual;
Date
01-MAR-17
SELECT TO_CHAR(to_date('1st march 2017','dd"st" month yyyy'),'dd-mon-yyyy') "Date"
FROM dual;
Date
01-mar-2017

How to load key value pairs into hive table?

Below is my data set:
Jun name="balaji" id=101
Mar name="kumar" id=102
Created table:
create table sample(month string,name string, id int)
row format delimited fields terminated by 'space' map keys terminated by '=';
Result:
select * from sample;
JUN name="balaji" NULL
Mar name="kumar" NULL
Expected result:
JUN balaji 101
Mar kumar 102
Please help me on this.
Create tables like this:
create table sample(mnth string,names map<string,string>,ids map<string,int>)
row format delimited fields terminated by ' ' map keys terminated by '=';
Select query should be:
select mnth,names["name"],ids["id"] from sample;
result:
Jun "balaji" 101
Mar "kumar" 102
If you fire select * from sample:
Jun {"name":"\"balaji\""} {"id":101}
Mar {"name":"\"kumar\""} {"id":102}
For accessing each value in map you need to pass like names["name"].

How to convert a Date String from UTC to Specific TimeZone in HIVE?

My Hive table has a date column with UTC date strings. I want to get all rows for a specific EST date.
I am trying to do something like the below:
Select *
from TableName T
where TO_DATE(ConvertToESTTimeZone(T.date)) = "2014-01-12"
I want to know if there is a function for ConvertToESTTimeZone, or how I can achieve that?
I tried the following but it doesnt work (my default timezone is CST):
TO_DATE(from_utc_timestamp(T.Date) = "2014-01-12"
TO_DATE( from_utc_timestamp(to_utc_timestamp (unix_timestamp (T.date), 'CST'),'EST'))
Thanks in advance.
Update:
Strange behavior. When I do this:
select "2014-01-12T15:53:00.000Z", TO_DATE(FROM_UTC_TIMESTAMP(UNIX_TIMESTAMP("2014-01-12T15:53:00.000Z", "yyyy-MM-dd'T'hh:mm:ss.SSS'Z'"), 'EST'))
from TABLE_NAME T1
limit 3
I get
_c0 _c1
0 2014-01-12T15:53:00.000Z 1970-01-16
1 2014-01-12T15:53:00.000Z 1970-01-16
2 2014-01-12T15:53:00.000Z 1970-01-16
Your system timezone CST doesn't matter for converting UTC to EST in Hive. You should be able to get the correct results with:
TO_DATE(FROM_UTC_TIMESTAMP(UNIX_TIMESTAMP(T.date, "yyyy-MM-dd'T'hh:mm:ss.SSS'Z'") * 1000, 'EST'))
Note that because UNIX_TIMESTAMP returns seconds, you will lose the millisecond component of your timestamp.
This converts to CST with the daylight savings hour shift:
to_date(FROM_UTC_TIMESTAMP(UNIX_TIMESTAMP(eff_timestamp, "yyyy-MM-dd'T'hh:mm:ss.SSS'Z'") * 1000, 'CST6CDT'))

Resources