a varchar column is storing date value in table_a and it looks like '24-May-2021'
now i am trying to select to_date(varchar_column, 'DD/MM/YYYY') from table_a, but was throwing below error:
Can't parse '24-May-2021' as date with format 'DD/MM/YYYY'.
please help
looking the the date formatting table
You should use:
select to_date(varchar_column, 'DD-MON-YYYY') from table_a;
because May is not 05 which the MM format expect, and - is not that separator you have in your data.
Related
My table INV_STOCK_TIME with 4000 rows with unix epoch data in snowflake. That data needs to be converted to date. Here is sample data and the desired output should be in 2 columns as given below
STOCK_RAWTIME
1617772221333
1616217315003
1601376748863
STOCK_RAWTIME STOCK_DATE
1617772221333 2021-04-07
1616217315003 2021-03-20
1601376748863 2020-09-29
I could to convert value in column using either of the below command.
select to_char(to_timestamp_tz(1617772221333,3),'YYYY-MM-DD') from dual;
OR
SELECT to_char(dateadd('ms',timestamp_col,'1970-01-01'),'YYYY-MM-DD') from (select 1617772221333 as timestamp_col) as timetest;
I would like to pass the values of column1 STOCK_RAWTIME (unix epoch value)
to populate column2 STOCK_DATE. I'm unable to pass list of values to convert column1 records with below error
Format argument for function 'TO_TIMESTAMP_TZ' needs to be a string
I believe you can get this requirement with the following:
SELECT STOCK_RAWTIME as STOCK_RAWTIME, to_date(STOCK_RAWTIME)AS STOCK_DATE FROM (select STOCK_RAWTIME from INV_STOCK_TIME);
Resolved with this step in snowflake as to_date or to_timestamp works fine:
alter table inv_stock_time add column stock_date date;
update inv_stock_time set stock_date=to_date(STOCK_RAWTIME);
I need to convert the contents of a DATE colum and store it in another column.
Example
(25/01/2019) to Jan'19.
This following option is not a good one considering performance but you can get your desired result-
SELECT
REPLACE(CAST(FORMAT(CONVERT(DAte,'25/01/2019',103), 'MMM-yy') AS VARCHAR),'-','''')
Use of FORMAT is to get value is desired format.
if you have hardcode date you can use this one :
SELECT
REPLACE(FORMAT(CONVERT(Date,'25/01/2019',103), 'MMM-yy'),'-','''')
if you have column in database then you can use :
SELECT REPLACE(FORMAT(DateColumnHere, 'MMM-yy') ,'-','''')
FROM Table1
A date type column has no format but we can change the display format by converting it to a string. In your case, [myDate] should be a string type column such as varchar, and we'll cast it to date before converting the display format. So consider :
with tab([myDate]) as
(
select '25/01/2019'
)
select convert(varchar,
cast(concat(substring(myDate,7,4),'-',
substring(myDate,4,2),'-',
substring(myDate,1,2)) as date) ,6)
as [myDate]
from tab;
myDate
---------
25 Jan 19
Demo
My date column is stored as a CHAR and in YYYYMMDD format. The string I have to search from this column is in MM/DD/YYYY format. How could I change it to YYYYMMDD. Also if my parametr is passed NULL to the query I do not include the DOB search in the WHERE clause
select *
from ona
left join mnv on ona.xyz = mnv.xyz
where (coalesce(to_date(mnv.DOB,'YYYYMMDD'),to_date('1901-01-01','YYYY-MM-DD')) OR '{BirthDate}' IS NULL)
It is not suggested to store dates as string, mainly due to this particular reason. So try to change your column datatype if possible to avoid this in future.
For now, you can compare it, by converting both of the strings to date, with to_date function.
select something from some_table where
to_date(date_Column,'YYYYMMDD')=to_date(search_value,'MM/DD/YYYY');
As you asked in comments, it is possible to do it by manupulating the strings also however I think comparing by dates would be faster and less prone to inaccuracy.
Update after seeing your query.
You are not converting BirthDate to date and also there is no = to compare in your query. Assuming that BirthDate is a string with MM/DD/YYYY format, use something like below.
select * from ona left join mnv on ona.xyz = mnv.xyz
where
to_date(mnv.DOB,'YYYYMMDD') = to_date(BirthDate,'MM/DD/YYYY')
and mnv.DOB is not null
and BirthDate is not null.
I'm using a table that has the date as varchar. example dateformat:
17-Jun-2015
I have tried the following ways to convert and sort the date(dd-MMM-yyyy) to dateTime.
SELECT date, name, author
FROM sometable
ORDER BY CONVERT(DATETIME, date, 106) DESC
I have also tried converting the date in the select statement. doesn't work. The error is
Conversion failed when converting date and/or time from character string.
The conversion types through some similar questions but I have not found any solutions to the format I have. Is there some way of selecting the delimiter between the day month and year??
I also had a browse through this link which has the formats for datetime formats. 106 was the closest to my varchar date. Only my date in the table has '-' between day month and year.
https://msdn.microsoft.com/en-us/library/ms187928.aspx
Appreciate any help.
It should be absolutely fine to use 106 to convert your date format.
But I guess your table contains some of the invalid values in the column causes the error, try to spot them out by TRY_CONVERT:
SELECT date, name, author, TRY_CONVERT(datetime, date, 106) AS convertresult
FROM sometable
WHERE TRY_CONVERT(datetime, date, 106) IS NULL AND date IS NOT NULL
I would use date style 113 to convert to datetime format like this:
DECLARE #date VARCHAR(20) = '17-Jun-2015';
SELECT CONVERT(VARCHAR(20),CAST(#date AS DATETIME),113)
Good day!
I have 2 questions how to update a date data type column field using varchar and numeric column field
1.)mydate varchar(8)--> varchar column field
SELECT mydate from mytable
Result: 20141120
my question is how can I update my date column field using my varchar column field using cast or convert
update table2
set date = (select mydate from mytable)
which I get an error!!! and I'm stuck.
2.)mydate numeric(8) --> numeric column field
SELECT mydate from mytable
Result:
20101015
20140910
etc.......
update table2
set date = (select mydate from mytable a, mytable2 b
where a.id=b.id)
my question is how can I update my date column field using my numeric column field using cast or convert
I used different CAST and CONVERT but still I'm getting error!
What is the correct syntax for this?
Thank your for your help!
To convert a string to a date field you will need to use the CONVERT function:
CONVERT(datetime, mydate, 101)
This is expecting a string field, so if your mydate field is really a numeric field then you will need to CAST that to a string, so the CONVERT command will then look like:
CONVERT(datetime, CAST(mydate as VarChar), 101)
The third parameter of the function is determined by the format of the date in the previous parameter, you can find the full list on MSDN at http://msdn.microsoft.com/en-us/library/ms187928.aspx