Is there a way to convert a VARCHAR to a DATE in AWS Redshift? - sql

Attempting to convert a variable character data type field that is time related (eg '2015-Q1') to a timestamp (or time) data type field in order to compare with a different field.
Need to be able to use dateadd() on the field.

Use the TO_DATE function: https://docs.aws.amazon.com/redshift/latest/dg/r_TO_DATE_function.html
https://docs.aws.amazon.com/redshift/latest/dg/r_FORMAT_strings.html
Along with TO_TIMESTAMP
https://docs.aws.amazon.com/redshift/latest/dg/r_TO_TIMESTAMP.html
example:
select to_timestamp(to_date('2015-Q1', 'YYYY-MM-DD'), 'HH24:MI:SS');

Related

Query oracle Db for data between two dates

Trying to query an oracle db table having date in format: 2022-06-22T12:25:06.087 (LocalDateTime.now().toString()). Column type for created_time is varchar2.
Trying to query for data between two dates. I have tried the following but it results in error "date format not recognized":
select * from MY_TABLE
where to_date(created_time, 'yyyy-MM-ddTHH:mm:ss.SSS')
between to_date('2022-07-03T10:15:06.091', 'yyyy-MM-ddTHH:mm:ss.SSS')
and to_date('2022-07-03T10:15:06.091', 'yyyy-MM-ddTHH:mm:ss.SSS');
Can anyone help me correct this query?
Format you used looks like data (in CREATED_TIME column) is stored as a timestamp. If that's so, you shouldn't convert it to another datatype (you chose TO_DATE function) but leave it as is. If you stored data as a string (that's usually a huge mistake), then apply the same to_timestamp function with the same format model as the one in between clause.
Apart from that, format model for minutes is mi (not mm; that's month), while fractional seconds is ff3 (not sss).
SELECT *
FROM my_table
WHERE created_time
BETWEEN TO_TIMESTAMP ('2022-07-03T10:15:06.091', 'yyyy-MM-dd"T"HH24:mi:ss.ff3')
AND TO_TIMESTAMP ('2022-07-03T10:15:06.091', 'yyyy-MM-dd"T"HH24:mi:ss.ff3');
I guess column created_time is of data type DATE or TIMESTAMP. Never call TO_DATE() or TO_TIMESTAMP() to a values which is already a DATE
The DATE data type does not support fractional seconds, use TIMESTAMP instead. Format literals have to be enclosed by double quotes.
Format HH is the hour in 12-hour format. I assume you need 24-hour format, which is HH24. mm (or MM) is the Month, for Minute use MI. Format identifiers are not case-sensitive, so SSS is also wrong.
Try this one:
select *
from MY_TABLE
where created_time
between TO_TIMESTAMP('2022-07-03T10:15:06.091', 'yyyy-MM-dd"T"HH24:MI:ss.ff3')
and TO_TIMESTAMP('2022-07-03T10:15:06.091', 'yyyy-MM-dd"T"HH24:MI:ss.ff3');

How can i make a field with a datatype Varchar be a date in Snowflake?

I am using Snowflake and I have a field with a datatype VARCHAR and the values in that field are for example: 2/10/17, 9/7/18, 1/23/19.
I trying to convert that field into a Date using this script:
select To_Date(Field_name) from CONCUR
However i get this message:
Date '' is not recognized
You need a format specification as a second argument to to_date() (otherwise it defaults to session parameter DATE_INPUT_FORMAT, which is probably not what you want):
to_date(field_name, 'MM/DD/YYYY')
You may also want to use try_to_date(), that returns null when the conversion fails rather than raising an error as to_date() does.
To_date should be used with the format like below
select to_date('02/14/2014', 'MM/DD/YYYY'), date('02/14/2014', 'MM/DD/YYYY');
https://docs.snowflake.com/en/sql-reference/functions/to_date.html
Thanks
Palash

Converting records from 'YYYY-MM-DD' format to MM/DD/YYYY in PL/SQL

I am trying to convert a specified column which is in format 'YYYY-MM-DD' and I need to convert it in MM/DD/YYYY as a data warehousing task.
The specified column is in varchar2 format.
I've been trying to use to_date, to_char but haven't succeeded yet. Any ideas?
We can try first converting your text dates into bona fide dates using TO_DATE. Then, we can use TO_CHAR to convert them to the new format.
UPDATE yourTable
SET date_col = TO_CHAR(TO_DATE(date_col, 'YYYY-MM-DD'), 'MM/DD/YYYY');
-- and maybe a WHERE clause
This being said, it is bad practice to persist your date information as text. Rather, use a proper date or timestamp column if at all possible. You would be better off creating a new date column, and then stopping after calling TO_DATE.
Inside your function/procedure, you can try this:
some_var := to_char(to_date(column_to_convert,'YYYY-MM-DD'),'MM/DD/YYYY');
It converts first the data to a date, then back to varchar2 using the desired format. Just replace the identifiers accordingly.
this will work:
select to_char(to_date('2018-10-19','YYYY-MM-DD'),'MM/DD/YYYY')from dual;

How to change date format in hive?

My table in hive has a filed of date in the format of '2016/06/01'. but i find that it is not in harmory with the format of '2016-06-01'.
They can not compare for instance.
Both of them are string .
So I want to know how to make them in harmory and can compare them. Or on the other hand, how to change the '2016/06/01' to '2016-06-01' so that them can compare.
Many thanks.
To convert date string from one format to another you have to use two date function of hive
unix_timestamp(string date, string pattern) convert time string
with given pattern to unix time stamp (in seconds), return 0 if
fail.
from_unixtime(bigint unixtime[, string format]) converts the
number of seconds from unix epoch (1970-01-01 00:00:00 UTC) to a
string representing the timestamp of that moment in the current
system time zone.
Using above two function you can achieve your desired result.
The sample input and output can be seen from below image:
The final query is
select from_unixtime(unix_timestamp('2016/06/01','yyyy/MM/dd'),'yyyy-MM-dd') from table1;
where table1 is the table name present in my hive database.
I hope this help you!!!
Let's say you have a column 'birth_day' in your table which is in your format,
you should use the following query to convert birth_day into the required format.
date_Format(birth_day, 'yyyy-MM-dd')
You can use it in a query in the following way
select * from yourtable
where
date_Format(birth_day, 'yyyy-MM-dd') = '2019-04-16';
Use :
unix_timestamp(DATE_COLUMN, string pattern)
The above command would help convert the date to unix timestamp format which you may format as you want using the Simple Date Function.
Date Function
cast(to_date(from_unixtime(unix_timestamp(yourdate , 'MM-dd-yyyy'))) as date)
here is my solution (for string to real Date type):
select to_date(replace('2000/01/01', '/', '-')) as dt ;
ps:to_date() returns Date type, this feature needs Hive 2.1+; before 2.1, it returns String.
ps2: hive to_date() function or date_format() function , or even cast() function, cannot regonise the 'yyyy/MM/dd' or 'yyyymmdd' format, which I think is so sad, and make me a little crazy.

creating table in Oracle with Date

I want to create a table in Oracle 10g and I want to specify the date format for my date column. If I use the below syntax:
create table datetest(
........
startdate date);
Then the date column will accept the date format DD-MON-YY which I dont want.
I want the syntax for my date column to be MM-DD-YYYY
Please let me know how to proceed with this.
Regards,
A DATE has no inherent format. It is not simply a string that happens to represent a date. Oracle has its own internal format for storing date values.
Formats come into play when actual date values need to be converted into strings or vice versa, which of course happens a lot since interactively we write dates out as strings.
The default date format for your database is determined by the settings NLS_DATE_FORMAT, which you probably have set to DD-MON-YYYY (which I believe is the default setting for American English locales). You can change this at the database level or for a single session for convenience, but in general it is safer programming practice to be explicit so that you don't get errors or, worse, wrong results if your code is run in a different environment.
The simplest way to specify a date value unambiguously is a date literal, which is the word 'date' followed by a string representing the date in YYYY-MM-DD format, e.g. date '2012-11-13'. The Oracle parser directly translates this into the corresponding internal date value.
If you want to use a different format, then I recommend explicitly using TO_CHAR/TO_DATE with your desired format model in your code. Examples:
INSERT INTO my_table (my_date) VALUES ( TO_DATE( '11-13-2012', 'MM-DD-YYYY' ) );
SELECT TO_CHAR( my_date, 'MM-DD-YYYY' ) FROM my_table;
dates rdo not have a format like you're suggesting. they are stored internally as a 7 byte number. to format the date when selecting, please use TO_CHAR(yourdatefield, 'format')
where formats are all shown here: http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm#i34924
eg to_char(startdate, 'mm-dd-yyyy')