I am trying to convert a varchar field into date field using cast(date as date) but it is throwing an error as "Amazon Invalid operation: Error converting text to date".
I tried converting the same column using to_date(date,'Mon DD yyyy') function and it worked fine.
what could be causing this error in cast function and what can I do to rectify it?
I want to use cast specifically for this conversion.
SELECT DISTINCT cast(activity as date)
from akhil_date_conversion;
my activity field has values like:
Nov 24, 2002 9:02 AM
Jan 21, 2002 9:00 AM
Nov 17, 2002 9:00 AM
Nov 5, 2002 9:00 AM
Feb 17, 2002 10:00 AM
Jan 16, 2009 9:03 AM
Apr 20 2002 13:02
May 11 2002 19:34
Aug 11, 2002 12:00 PM
some have AM/PM and some do not!
There is a cluster parameter called datestyle that can be used to set the default date / time format for the cluster. The default is ISO and what you have is closest to POSTGES. This is used in outputting dates but also in interpreting them. However, you don't have a consistent datestyle in your data (12 and 24 hour format changes) so I don't think this will work in your case. You will need to write some SQL get your data into a single format before CAST will work by default.
Related
I have a date column in text format of "May 30 2022 9:30PM" and it needs to convert to "2022-05-30 21:30:00" in Snowflake SQL
I tried below SQL and its not working.
SELECT cast('May 30 2022 9:30PM' as datetime);
For Snowflake, try the following:
select to_timestamp_ntz('May 30 2022 9:30PM', 'MON DD YYYY HH12:MIAM');
Reference:
https://docs.snowflake.com/en/sql-reference/functions-conversion.html
I need to convert string to Timestamp.
The problem is that the input is coming from a csv file and contains date-time values such as:
Mar 3 2022 8:30AM
Apr 27 2022 7:37AM
If I use the following conversion:
to_timestamp(to_timestamp(trim(DateColumn), 'MMM dd yyyy h:mma'), 'yyyy-MM-dd HH:mm:ss')
It converts the date Apr 27 2022 7:37AM correctly, but throws error while converting Mar 3 2022 8:30AM because of the extra space between the Month and Date values and that the date 3 is not 03.
Is there a way to convert these 2 strings formats into Datetime?
It is recommended that you first uniformly replace multiple spaces with a single space, and then convert to timestamp.
val df1 = df.withColumn("ts", to_timestamp(regexp_replace(trim(col("ts")), "\\s+", " "), "MMM d y h:mma"))
I have varchar like this "Wed Jan 26 2022"
I need to convert this to date in sql. How can i do this
for Sql Server:
convert(date, substring('Wed Jan 26 2022',5,11),9)
we ignore the Day name (superfluous), and convert the rest using format 9 indicating Mon dd yyyy format.
SQL*plus server (Here's my code) -
If I am right, you want such type of string which is an invalid one to convert that into a valid one so that you can store valid data into the database. then this code you can use->
SELECT TO_DATE('WED JAN 26 2022','DY MON DD YYYY')FROM DUAL;
(Explanation)->
Code will convert invalid date datatype to a valid date data type which is used in Oracle(SQL).
DY = Abbreviated Week Day
DD = Month day indicator
MON = Abbreviated month
YYYY = Four-digit year indicator
When i select date as sept 16,i get to see sept 16 6am and sept 17 data until 5:59 AM.
It is treating 24 hours from sept 16 am to sept 17 6am.
Is there any issue with date field or report filter issue?
BigQuery's date functions work based on UTC timezone.
The UI you are using probably converts them into your local timezone, and you are seeing the 6 hours difference.
For visibility purposes I am posting my comment as an answer:
As #Pentium10 mentioned:
BigQuery's date functions work based on UTC timezone. The UI you are using probably converts them into your local timezone, and you are seeing the 6 hours difference.
According to this Data Studio uses UTC standard time, but if your data set does not use UTC you can use the TODATE function to convert the date field to UTC.
I have difficulties understanding the function DATEDIFF. When querying
SELECT DATEDIFF(YEAR, 0, getdate())
I get difference between current year and year 1900 resulting 111. I think that the starttime should be time, not integer like 0. How 0 can be used? Why the start year in 1900, not 1753 as it should be when format is datetime?
It works in datetime because of implicit conversion of 0 to 1st January 1900.
Why not 1900? Why does 0 = 31 Dec 1899 for MS Access? Why are unix timestamps from 01 Jan 1970?
1753 is fairly arbitrary too: it's the major switch to the Gregorian calendar but it isn't consistent. SQL Server 2008 goes back to 01 Jan 0001 with the newer types too.