How to format yyyy-MMMM in Hive? - sql

I'm trying to format date column ("craeteddate") as yyyy-mmmm, i.e.: 2017-November.
select unix_timestamp(createddate, 'yyyy-MMMM') from cc_vw_case
This SQL throws "Bad date/time conversion format: yyyy-MMMM".
Is it possible format as yyyy-MMMM in Hive? I'm using Cloudera Hadoop.

If your input value is a Unix timestamp, then you'll need to use FROM_UNIXTIME instead of UNIX_TIMESTAMP. This gets the correct answer in Hive:
SELECT FROM_UNIXTIME(1510218382, 'yyyy-MMMM')
Answer: 2017-November

unix_timestamp is trying to parse your column, not format it.
Probably throws an error because it is not in the given format
You need to use a combination of from_unixtime(unix_timestamp(col, "format from"), "format to") to convert between datetimes

Related

How to convert a string in format YYYYMMDD to date without string manipulations in a consistent way in AWS redshift and AWS athena

I'm looking to convert a string into a date in a consistent way in both AWS redshift and AWS athena without string manipulation.
The string that I need to convert into a date is in the format YYYYMMDD.
(I know the format YYYY-MM-DD would be easier to work with, however, I am unable to change the source format).
The only solution that I have currently found that works in both is:
SELECT CAST(SUBSTRING('20220101',1,4) || '-' ||SUBSTRING('20220101',5,2) || '-' ||SUBSTRING('20220101',7,2) AS DATE)
I.e. manipulating the string into a more useable format from YYYYMMDD to YYYY-MM-DD, then casting as a date.
However, this feels overcomplicated, and I'm hoping there is a conversion to date function that I have missed.
I have tried a few things, but no luck so far.
For example, in redshift, but neither work in Athena:
SELECT '20220101'::DATE
or
SELECT CAST('20220101' AS DATE)
And the closest I can get to in athena is as follows:
SELECT CAST(date_parse('20220101','%Y%m%d') AS DATE)
This isn't the solution though as date_parse isn't available in redshift.
Athena and Redshift have different SQL flavors (Redshift is based on PostgreSQL and Athena is based on Presto/Trino).
For Redshift use the TO_DATE function:
select to_date('20010630', 'YYYYMMDD');

Facing an issue while converting invalid date string to valid date string in SQL11g getting error ORA-01858

I am facing an issue when I was converting invalid date format to valid date format using the TO_DATE function, Here I explained my problem in the easiest way possible but in reality, the data is huge and having this problem. If you can provide me with a solution to this problem then it will be much helpful.
I tried this ->
Select TO_DATE('TWENTY-THREE,JANUARY,1998' , 'FMDDSP,MONTH,YYYY') From DUAL;
As I am having input date string as - DD in spelled format as you can see i.e. twenty-three, twenty-four. I want to convert that into valid date format to dd-mon-yy So that I can store them into the database.
Right now I am getting Error -
ERROR at line 1:
ORA-01858: a non-numeric character was found where a numeric was expected.
I am using oracle 11g (SQL*plus)
Sorry, afaik TO_DATE does not support the conversion from format sp or spth. That only works the other way round, with TO_CHAR. So you are left here with the only option of writing your own conversion function in PL/SQL. or Java.

changing dateformat sql

On loading a date column from salesforce, I receive it in this format: 2017-01-31 22:00:00. I want convert this to the german format and load into sql table without time.
Also, which datatype should I initialize in creating column in table?
try using the Convert function if you're using MSSQL
select Convert(nvarchar(10),GETDATE(),101)
This will create mm/dd/yyyy
if you need more samples please look at w3schools to convert time in different formats
https://www.w3schools.com/sql/func_sqlserver_convert.asp

SQLLDR Oracle Data Import, Date Format = yyyy-mm-dd

I am trying to import data from a flat file into Oracle 11gr2 using SQLLDR, and i have 3 columns with date formats like "yyyy-mm-dd". Oracle keeps throwing errors when I try to import the data like this. Is there a quick solution to just import the data in this format? I would prefer to not have to change the format of the data in the file since it is very large.
Here is an example of the error listed in the log.ct:
Record 51: Rejected - Error on table LINEITEM, column L_SHIPDATE.
ORA-01861: literal does not match format string
Thanks!
Trying casting the date format as mentioned below, in your control card (Control file)
YourColumn DATE "YYYY-MM-DD"
Set the environment setting NLS_DATE_FORMAT
#>export NLS_DATE_FORMAT='YYYY-MM-DD'

SQL Syntax question

I'm trying to remember the syntax to change a date field into a string. I know I'm close but not 100% correct. Here is what I'm using so far: TO_CHAR(FIELD_NAME). I'm using an Access database. The error I'm getting is: undefined expression. Any help would be very much appreciated.
Use either CStr(dateField) or Format(dateField) to convert.
Additionally you can add parameters to Format() to show it in a different format, such as:
Format(dateField, "general date") 9/12/2010
Format(dateField, "long date") Monday, September 12, 2011
Given that you're using MS Access and its a date field you're probably not just looking to convert to string but to also format the Date. If that is indeed the case you'll want the Format function
SELECT Format ([DateCreate], "yyyy/mm/dd") AS Foo
FROM MSysObjects;
If you're using SQL Server, try out CAST or CONVERT
You can use the CONVERT function, like this:
CONVERT(VARCHAR, DateField, 100)
Here's a link that shows the different date formats you can use:
http://www.sql-server-helper.com/tips/date-formats.aspx
I assume SQL Server as you're questions in the past are .NET Questions.
Use CONVERT
http://msdn.microsoft.com/en-us/library/ms187928.aspx