Conversion of String to Date/Timestamp in Hive - hive

I have a String value in the format "YYYY-MM-DD HH:MM:SS".
I want to change the data type of this from string to Date/Time.
I tried To_Date(),which gives me the date part of the string.
Is there a function in hive which converts this string to Date/Time Stamp?

I am not sure which version of Hive you are using.
Check this link
it contains all the functions in Hive till Hive 0.13
In your case you should be looking at these functions:
1.) unix_timestamp(string date, string pattern)
2.) unix_timestamp(string date)
3.) from_unixtime(bigint unixtime[, string format])
Looking at your date format, you can use 1.) or 2.) of you need only timestamp(long value in Hive). Also if you need date in any specific format, you can use 3.) along with 1.) or 2.)
Hope it helps...!!!

Related

sql date format "2019-10-30", "2019/10/30", "10-30-2019" and "10/30/2019"

I think it's no doubt that in sql "2019-10-30" is valid date format, and looks like "2019/10/30" accepted as well.
What about "10-30-2019" and "10/30/2019"?
I tried them in mariaDB and they're wrong format, but somehow I think I did see them in some sql tables. Please help me clarify the date format. Thanks
In MariaDB, the format for dates is yyyy-mm-dd. But MariaDB is quite lax and other formats are accepted; from the documentation:
A DATE string is a string in one of the following formats: 'YYYY-MM-DD' or 'YY-MM-DD'. Note that any punctuation character can be used as delimiter. All delimiters must consist of 1 character. Different delimiters can be used in the same string. Delimiters are optional (but if one delimiter is used, all delimiters must be used).
A DATE literal can also be an integer, in one of the following formats: YYYYMMDD or YYMMDD.
All the following DATE literals are valid, and they all represent the same value:
'19940101'
'940101'
'1994-01-01'
'94/01/01'
'1994-01/01'
'94:01!01'
19940101
940101
So, for the examples that you provided:
2019-10-30 -- ok: default format
2019/10/30 -- ok: format with alternative delimiter
10-30-2019 -- NOT ok
10/30/2019 -- NOT ok
You can translate a string to a date with function str_to_date().
str_to_date('10-30-2019', '%m-%d-%Y')
str_to_date('10/30/2019', '%m/%d/%Y')
What you are really doing is converting from a string representation of a date to an internal date time representation or data type. The format you are referring to is ISO 8601. The ISO date (2019-10-30 with hyphens or parentheses) format is usually the native or default string conversion that most databases use, that's why those formats work even if you don't specify the conversion. If you intend to convert from other formats you probably would need to specify them. Your default representation for your local settings might work too, but that depends on the specific database.

convert string value in ate format in java script

i have a problem in converting a string date value into valid date format in pentaho-data-itegration .could someone help me to convert string date'08-Mar-2017' into date format (2017-03-08 00:00:00) in pentaho.
Date do not have a format in PDI.
To convert the '08-Mar-2017' string to a date, use the Select value step, Meta-data tab, and tell your field should be of type Date, and the conversion format is dd-MMM-yyyy (type it, as it not on the drop down list).
To convert the date to the '2017-03-08 00:00:00' string, use the Select value step, Meta-data tab, and tell your field should be of type String, and the conversion format is yyyy-MM-dd hh:mm:ss.
However, you most probably do not need to do it.
The only time a date format is used is for data input and output. Plus it is transparent, most of the time.
If your data comes from a file or data grid or a computation or many other steps, you can specify its format. If is comes from a Input table, think about converting with the sql build-in function : to-date, convert, cast, date depending on your database system.
If your output is a file or an Excel or a script, you can specify its format (which by default is yyyy-MM-dd hh:mm:ss). If it's a sql insert or update, don't do anything and let the databases do the conversion.

Formatting a string to time on BigQuery?

I've got a huge (1.5GB) CSV file, with dates in it in the format 2014-12-25. I have managed to upload it to BigQuery with the format string for this column. I'm wondering if I can transform this in situ to a datetime format, without having to download the data, parse it and send it back?
I have used the BigQuery GUI (newbie) but am happy to use the CLI if this will make it easier.
You can use some of Date and time functions to "transform" string represented date to datetime
For example
SELECT '2014-12-25', TIMESTAMP('2014-12-25')
Added:
If you feel that you really need to have your data with date in timestamp format vs string and you have this data (string) already in BigQuery - you can do just similar to below query with writing to new table.
SELECT
TIMESTAMP(date_string) as date_timestamp,
< list all the rest of the fields >
FROM original_table

Error Storing Date in custom format using to_date

Aim:
I want to store date in database in dd/MM/yyyy format.
I have first formatted it using DateFormat which results in changing it to String datatype and then trying to convert back to date datatype by using to_date function but it gets stored in as 19-DEC-12 i.e dd-MON-yy format.
Code:
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
java.util.Date dob=JDateChooser.getDate();
java.sql.Date dobloc=new java.sql.Date(dob.getTime());
String dobloc1=df.format(dobloc);
String sql="INSERT INTO ADMIN.DEPARTMENT(date_of_birth) VALUES (to_date(?,'dd/MM/yyyy'))"
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, dobloc1);
Dates are not kept with the format you specify in any database (unless you use a string field) Most database systems store datetime values as a "double" value.
You should put the date into the database and worry about formatting when you are getting the data from the db or presenting it to the user.
I think you are misunderstanding how date actually works in Oracle.
The date is not stored like "19-DEC-12" or "19/12/2012", it is stored as 7-bytes.
See here for further details.
If you select a date column it is formatted using NLS_DATE_FORMAT.
This is the default mask used to format those 7-bytes.
Of course if you use a custom format mask like to_char(sysdate,'YYYY MM DD') the default NLS_DATE_FORMAT is ignored.
As I read in the above comments
Actually I am trying to store in a correct format because i am displaying a pure copy of my Database table on some other forms using JTable so the date formatting in which date is stored gets displayed directly. I dont want to convert Date Format on my large number of Jtables which may make me to change the Date format again and again as my tables goes on increasing
I suggest you also save the specific formatting mask used to format the dates in another column in your DB-Table.
Before you display your date in the JTable you format it with this mask using DateFormat.

Access SQL Date Format

How can I retrieve a record based on a Date property? I'm trying:
WHERE Meetings.[MDate] = '16/12/2011'
which is the format I use but I get :
"Data type mismatch in criteria expression"
Problem solved: It should have been:
WHERE Meetings.[MDate] = 16/12/2011
No quotation marks.
For where clauses use
columnName = #mm/dd/yyyy#
You'll want to use the SQL date format: '#2011-12-16#'
Use the cast to DATETIME function, CDATE(), which will honour the machine's regional settings. That said, it still a good idea to use an unambiguous date format and the ISO 8601 format is a good one.
Also note that Access doesn't have a date data type: its sole temporal data type is DATETIME and, as its name suggests, always has a time element accurate to one second time granule, even if that time happens to be midnight. Therefore, it is a good idea to always include a time value to one second time granule in all DATETIME literals e.g.
WHERE Meetings.MDate = CDATE('2011-12-16 00:00:00');
Another advantage to the above is that the Access UI will not attempt to reformat the DATETIME literal because it is held as a string.