I have a string field that is being pulled from a table and I'm trying to cast that as a date in a view I created. I keep getting an error when trying to Cast as a date though. The format of the field looks like this:
July 19, 2020 or
August 8, 2020 etc..
I get an error that states
"Failed to parse input string "July 19, 2020"
or one of the other dates in the data when trying to use DATE_Parse. Or I get
"Invalid Date:August 8, 2020"
if I try to use the CAST function.
Below is my query when trying to CAST the date:
select
noteattributes.value.name as name_type, noteattributes.value.value as name_value, CAST(noteattributes.value.value as DATE) as DATE_TEST, order_number
from test.orders,
unnest(note_attributes) as noteattributes
where noteattributes.value.name = 'Pickup-Date'
Convert String to Date
Below is for BigQuery Standard SQL
You should use PARSE_DATE instead of CAST as in below example
PARSE_DATE('%B %d, %Y', date_as_string)
You can test, play with this using example below
#standardSQL
WITH `project.dataset.table` AS (
SELECT 'July 19, 2020' date_as_string UNION ALL
SELECT 'August 8, 2020'
)
SELECT PARSE_DATE('%B %d, %Y', date_as_string) AS date_as_date
FROM `project.dataset.table`
with output
Row date_as_date
1 2020-07-19
2 2020-08-08
Related
In my google sheet I have a column with dates but its in a text format. here an example what I have:
Oct 01, 2021
Dec 25, 2020
...
...
I want to convert it to a date format
01/10/2021
25/12/2020
....
I need to find the number of days from the dates in this column, by using "date in column" - now(). This does not work with the format "Oct 01, 2021" since its a text, and I am getting an error from Googlesheet.
Thanks in advance
IS
Try this formula in F2:
=ARRAYFORMULA(IFERROR(DATEDIF(
DATE(
RIGHT(E2:E,4),
MATCH(LEFT(E2:E,3),{"Jan";"Feb";"Mar";"Apr";"May";"Jun";"Jul";"Aug";"Sep";"Oct";"Nov";"Dec"},0),
MID(E2:E,5,2)),
NOW(), "D")))
Update
Revised the formula, which goes in F1 and fills the column, to:
={"Days Left";ARRAYFORMULA(
IFERROR(-1 * DATEDIF( DATE( RIGHT(E2:E,4), MATCH(LEFT(E2:E,3),{"Jan";"Feb";"Mar";"Apr";"May";"Jun";"Jul";"Aug";"Sep";"Oct";"Nov";"Dec"},0), MID(E2:E,5,2)), NOW(), "D"),
IFERROR(DATEDIF( NOW(),DATE( RIGHT(E2:E,4), MATCH(LEFT(E2:E,3),{"Jan";"Feb";"Mar";"Apr";"May";"Jun";"Jul";"Aug";"Sep";"Oct";"Nov";"Dec"},0), MID(E2:E,5,2)), "D"))))}
which reverses the date difference values. It also handles date differences for dates either in the future, or in the past.
Use the DATEVALUE() function on a date string, then use DATEDIF() to find the difference between two dates.
=DATEDIF(DATEVALUE("Oct 01, 2020"), DATEVALUE("Dec 25, 2020"), "D")
UPDATE: To find the date between today and a date string in another cell use this example:
=DATEDIF(DATEVALUE(A2), NOW(), "D")
If cell A2 contains string Oct 01, 2020, it will return 70 for today 2020-12-10
Below is a script i am trying to run in Presto; Subtracting today's date from an integer field I am attempting to convert to date. To get the exacts days between. Unfortunately, it seems the highlighted block does not always convert the date correctly and my final answer is not correct. Please does anyone know another way around this or a standard method on presto of converting integer values to date.
Interger value in the column is in the format '20191123' for year-month-date
select ms, activ_dt, current_date, date_diff('day',act_dt,current_date) from
(
select ms,activ_dt, **CAST(parse_datetime(CAST(activ_dt AS varchar), 'YYYYMMDD') AS date) as act_dt**, nov19
from h.A_Subs_1 where msisdn_key=23480320012
) limit 19
You can convert "date as a number" (eg. 20180527 for May 27, 2018) using the following:
cast to varchar
parse_datetime with appropriate format
cast to date (since parse_datetime returns a timestamp)
Example:
presto> SELECT CAST(parse_datetime(CAST(20180527 AS varchar), 'yyyyMMdd') AS date);
_col0
------------
2018-05-27
You can use below sample query for your requirement:
select date_diff('day', date_parse('20191209', '%Y%m%d'), current_timestamp);
I want to show date in string format as current month name(String) and year(e.g -> SEPTEMBER, 2019). How to write query for this in PostgreSQL?
I tried this query:
select
date(date_trunc('month', current_date));
but it gives me only current months starting date.
Try to_char() and add the year formatter to the string like this:
SELECT to_char(current_date, 'MONTH YYYY')
This will return:
SEPTEMBER 2019
Here's a sqlfiddle
If you want to format the output of a DATE value, use to_char()
select to_char(current_date, 'Month, yyyy');
we can do it like this also in Postgresql -
SELECT TO_CHAR(NOW() :: DATE, 'Month , yyyy');
output - September, 2019
I have a text field that is in this format May 21 2013 9:45AM. How would I convert that to datetime? I tried the following
UPDATE WaterRevLienInfo
SET LienDate = CONVERT(DATETIME, CONVERT(VARCHAR(30), LienDate), 101)
It works as a select and comes out like 2013-05-21 09:45:00.000 but not as an update. Any help would be great.
You appear to be "round tripping" the the LienDate field by casting it to a varchar and then back to a datetime. I'm not sure what this is accomplishing.
However, if you have a text value in the format you specified (which is, by default, how SQL represents datetime fields when casted to a varchar), you can just do a straight convert:
DECLARE #DateText varchar(30) = 'May 21 2013 9:45AM';
UPDATE WaterRevLienInfo
-- Convert the DateText string value for storage in a datetime field.
SET LienDate = CONVERT(datetime, #DateText);
This should also work with other fields in the same table.
UPDATE WaterRevLienInfo
SET LienDate = CONVERT(datetime, DateTextField);
The datetime hasn't any format it just a data. The presentation must care about the format.
I have a datetime field and what to covert it to "MonthName DAY, YEAR" format.
For example, the following date:
2013-01-16 00:00:00.000
will be convert to:
January 16,2013
I am doing this with the following statement:
CAST(DATENAME(MONTH,DateTime) AS VARCHAR(12)) + ' ' + CAST(DATEPART(DAY,DateTime) AS VARCHAR(2)) + ','+ CAST(YEAR(DateTime) AS VARCHAR(4))
As you can see I am using separate date function for each part of the datetime value and converting it to string in order to concatenate it with the other parts.
Is there a more clear and faster way, using only covert/cast function?
How about this:
select
CONVERT(VARCHAR(12), dt, 107) AS [Mon DD, YYYY]
from temp
Output:
Jan 16, 2013
Live Demo