Month name to number in hive - calendar

How can I convert month name to month number in Hive ? Is there any inbuilt function available in Hive?
Eg: If I have "January" in one of the column, it should replaced to "01" using another table or using any other properties or Udf.

Yes, date_format(date/timestamp/string ts, string fmt)
Converts a date/timestamp/string to a value of string in the format specified by the date format fmt (as of Hive 1.2.0). Supported formats are Java SimpleDateFormat formats – https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html.

Related

TSQL Translation into Snowflake

I have SQL which I run against the on premises database. In the WHERE clause I am narrowing it down to year
WHERE YEAR(SRCSYS_ADD_DATE_TIME)=2020
and I get the results
In Snowflake work sheet I am using
WHERE EXTRACT(YEAR FROM TO_DATE(SRCSYS_ADD_DATE_TIME))=2020
and I am getting this message
Date '2020-05-28-20.42.09.724117' is not recognized'
Please need help.
Snowflake's automatic date-time parsing isn't recognizing your period separated timestamp formatting.
You'll need to explicitly specify a format to read the string during conversion:
EXTRACT(
YEAR FROM
TO_DATE('2020-05-28-20.42.09.724117', 'YYYY-MM-DD-HH24.MI.SS.FF')
) = 2020
bkan.
The problem is the timestamp format. If you don't specify a timestamp format, Snowflake will try to autodetect. In this case it can't, so you'll have to specify the timestamp format:
select year(to_timestamp('2020-05-28-20.42.09.724117', 'YYYY-MM-DD-HH24.MI.SS.FF6'));
If you don't want to specify the timestamp format, your best option is using YYYY-MM-DD HH24:MM:SS.FFFFF (or some other precision of fractional seconds) like this:
select year('2020-05-28 20:42:09.724117'::timestamp);
The second option reformats the timestamp to the standard format, and the ::timestamp syntax casts it to a timestamp.

Month and date position got interchanged after using Data conversion

I am trying to load some data from a .csv file into my SQL Server. There is a column for date which has datatype Unicode (WSTR) in the .csv file and the column for storing that date in SQL Server is of Datetime data type.
When I used DATA CONVERSION transformation to convert WSTR data to DBTIMESTAMP data, it got changed but with an error that it interchanged the month and date which gives me the wrong date.
The date should be like 2019-09-03 (for 3rd Sep 2019), but I get 2019-03-09.
Please suggest what the issue is that I am facing?
Problem
This may occurs when converting a string to a date value without specifying the date format. Reffering to the SSIS data conversion transformation official documentation:
If you are converting data to a date or a datetime data type, the date in the output column is in the ISO format, although the locale preference may specify a different format.
Assume that the data is stored in the csv file with the following date format MM/dd/yyyy and the default date format in the regional settings is dd/MM/yyyy then date values will not be converted properly.
Solution
In order to specify the date format you have to use a Script Component or a Derived column.
Derived Column
You have to reorder the date part manually, you can use TOKEN() and TOKENCOUNT() function to do that, the following expression convert dd/MM/yyyy format into yyyy-MM-dd universal format:
TOKEN([DateColumn],"/",3) + "-" + RIGHT("0" + TOKEN([DateColumn],"/",2),2) + "-" +RIGHT("0" + TOKEN([DateColumn],"/",1),2)
Script Component
You can use DateTime.ParseExact() function to parse a date based on a specific format:
DateTime.ParseExact(Row.DateColumn,"MM/dd/yyyy",System.Globalization.CultureInfo.InvariantCulture");

SQLite: Extract time from string using strftime

I have database column with time (string is used as data type of the column) in this format '30.09.2014 09:03:01'. I'd like to get timestamp from that column using:
SELECT id, strftime('%d.%m.%Y %H:%M:%S', date) AS time FROM table;
But I get empty column with time. I think there is a problem with dots as a separators. Can anyone help me with that?
The strftime format string is used to specify the output format for printing a date that is supplied in one of the standard formats. The input formats accepted are one of the following:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD
You may need to do some string manipulation to reformat the date strings into a format that sqlite can interpret. Often it is convenient to reformat the date when inserting the data initially.
Reference sqlite documentation.

How to get DatePart out of datetime result column in SQLite

I have a column StartDateTime which has the Value 2/10/2008 6:30:12
I need only the Date part i.e 2/10/2008 to be retrieved.
I tried using strftime() and date() functions in sqlite.
This is not one of the supported date/time formats.
Store your date/times in the format 2008-10-02 06:30:12 instead.

regarding ms sql data type

I got a column in which I wanna store date formats that is dd/mm/yy, mm/dd/yy , etc. What data type do I need to use for this column in my table ? DateTime??
oh and also if I want to store Time format like 24.00 hrs..What data type then ?
I need to store the FORMATS like mm/dd/yy or dd/mm/yy not dates like 2010-12-01 or whatever..SO I should use DATeTime only ?
Store the dates as datetime or timestamp or any of the built-in date or date/time datatypes. Don't worry about formatting the value in the db itself. You can format the date using the language of your choice (whatever language you're using to retrieve the information).
If you want to store date and time you need DateTime column type.
To convert it to desired format you can use CAST and CONVERT functions.
To store formats as strings you can use VARCHAR - 'mm/dd/yy'.

Resources