How to delete "" and change it to date format - sql

My dataset's date column (tweet_stamp) is shown as "2020-01-29 00-21-29" and it is STRING format.
I would like to have result as 2020-01-29.
How to delete "" in string and change it to date format?
I tried code as below
select to_date(from_unixtime(unix_timestamp(tweet_timestamp,'"yyyy-MM-dd 00-00-00"'), 'yyyy-MM-dd')) as tweet_date;
However, result is NULL.

You do not need unix_timestamp+from_unix_time conversion because date part of string is already in right format. Just remove double quotes, get substring and optionally convert to date:
select date(substr(regexp_replace('"2020-01-29 00-21-29"','"',''),1,10)) --returns 2020-01-29
Or even simpler using to_date function:
select to_date(regexp_replace('"2020-01-29 00-21-29"','"','')) --2020-01-29

Related

Getting null while converting string to date in spark sql

I have dates in the format '6/30/2020'. It is a string and I want to convert it into date format.
List of methods I have tried
Cast('6/30/2020' as date) #returns null
to_date('6/30/2020','yyyy/MM/dd') #returns null
I also tried splitting the string and then concatenating it into data.
After trying all this and putting all the possible combinations in the to_date function, I am still getting the answer as null.
Now I am confused as I have used all the functions to convert string to date.
Thanks in advance for your help.
The date format you used was incorrect. Try this:
select to_date('6/30/2020', 'M/dd/yyyy')
If you want to format your result, you can use date_format:
select date_format(to_date('6/30/2020', 'M/dd/yyyy'), 'yyyy/MM/dd')
Note that to_date converts a given string from the given format, while date_format converts a given date to the given format.

Convert date to month year format and pass it in the query

I have a BIRT report where user will be entering the dates in dd-mm-yyyy format however I need to convert dd-mm-yyyy to MON-YYYY format.
I have tried to use VARCHAR_FORMAT(FIELDNAME,'MON-YYYY') however it doesn't work.
select …….
where VARCHAR_FORMAT(fieldname,'MON-YYYY') = '2017-05-15';
User would end the date as
15/05/2017
The value present in the database for this field is 2017-05-15 07:30:00.0
update
Apparently the column is not a string but a datetime which means the conversion is only
to_date(fieldname, 'MON-YYYY')
But if the column is used in a Where clause it shouldn’t be converted at all.
——
Use to_date and to_char to first convert your string to a date and then back to a string with the right format
to_char(to_date(fieldname, 'DD-MM-YYYY'), 'MON-YYYY')
select *
from table (values
timestamp('2017-05-15-07.30.00')
) t(fieldname)
where
fieldname between to_date('15/05/2017', 'DD/MM/YYYY') and to_date('15/05/2017', 'DD/MM/YYYY') + 1 day
--date(fieldname) = to_date('15/05/2017', 'DD/MM/YYYY')
;
You may run it as is.
Both cases work, but to use the 2-nd one efficiently, you must create an index by the date(fieldname) expression (since db2 10.5) or add generated always column to the table with the same expression and index on it.

NULL values in a string to date conversion

I have a table with the following data:
logs.ip logs.fecha logs.metodo
66.249.93.79 19/Nov/2018:03:46:33 GET
All data columns are string and I want to convert logs.fecha into date with the following format: YYYY-MM-dd HH:mm:ss
I try the following query:
SELECT TO_DATE(from_unixtime(UNIX_TIMESTAMP(fecha, 'yyyy-MM-dd'))) FROM logs
Results of the query are NULL in all rows.
How can I make the conversion string to date for all rows? I know I must use ALTER TABLE but I don't know how to do it.
Thanks
The reason you get null is because the format of the input string is different from the input passed to unix_timestamp. The second argument to unix_timestamp should specify the string format of the first argument. In from_unixtime you can specify the output format desired. If nothing is specified, a valid input to from_unixtime returns an output in yyyy-MM-dd format.
The error can be fixed as below.
from_unixtime(unix_timestamp(fecha,'dd/MMM/yyyy:HH:mm:ss'),'yyyy-MM-dd HH:mm:ss')
You just have to tell Oracle the date format you are reading with TO_DATE.
Try:
SELECT TO_DATE(fecha,'DD/MON/YYYY:HH:MI:SS') FROM logs

Remove Square brackets in postgresql (SQL)

i have a date column that reads as string value [2018/04/09].i want to read it as date column 2018/04/09.How to do it in postgresql?
Use the function to_date with the date formatter.
Include additional string characters that appear on all dates as part of the format string, and the date can be parsed correctly.
WITH example (dt) AS (
VALUES ('[2018/04/09]')
)
SELECT to_date(dt, '[YYYY/MM/DD]') FROM example
Alternatively, if the object is to clean up the data as well, e.g. some dates have the square brackets while other dates don't, then it is better to replace all invalid characters and then parse as date.
example:
WITH example (dt) AS (
VALUES ('[2018/04/09]')
)
SELECT to_date(trim(both '[]' from dt), 'YYYY/MM/DD') FROM example
Try this:
SELECT TO_DATE(date_column, 'YYYY/MM/DD') as date
FROM tablename

Convert STRING to DATE (error 2666)

in Teradata SQL I need convert a string to date.
Currently the string looks like this: 2017-02-28T14:41:32.817Z
But I need it in this format as DATE: DD.MM.YYYY HH:SS
Any idea how to do this? Whenever I try to cast, I get the error 2666 ( Invalid date supplied for mytable.mycolumn)
Hope someone can help!
Best regards,
Both input and expected result are timestamps, not dates.
SELECT '2017-02-28T14:41:32.817Z' AS mycol,
-- string to timestamp
Cast(mycol AS TIMESTAMP(3)),
-- back to string with a different format
To_Char(Cast(mycol AS TIMESTAMP(3)), 'DD.MM.YYYY HH:SS')