I have text column in table, where save date, date saved in 21.02.2012 and 2012-02-21 format, and i want convert all date to 2012-02-21
How i run this script with error exception
update AdvancedFieldsValues set value=(CONVERT(date, value, 104)) where field_id=801
update AdvancedFieldsValues
set value = convert(char(10), convert(date, value, 104), 120)
where field_id=801
Related
I am trying to write dynamic sql query in metabase-
I added date filter like that (using snowflake for query):
select * from my_table where date > {{date_filter}}
after that I needed to set default value of yesterday to date_filter, so I write this:
select * from my_table where date > [[{{date_filter}} #]]dateadd(DAY, -1, GETDATE())
and it worked, when I ran this query- I got the right output.
BUT- when I changed the date in the calendar that metabase provided for date types- I got this error:
SQL compilation error: syntax error line 1 at position 142 unexpected '#D'. syntax error line 1 at position 156 unexpected '-'. syntax error line 1 at position 172 unexpected '('.
How can I change date_filter default value without getting this error?
finally found a workaround that solve the problem.
in UI settings, define date_filter as REQUIRED and assign static default value- for example 01.01.2015 if my table contains data starting of 2016.
in sql query- I wrote that:
select * from my_table where date > case when year({{date_filter}}) = 2015 then dateadd(DAY, -1, GETDATE()) else {{date_filter}} end
which means:
if I won't select any date in date_filter calender- date_filter year value will be 2015 (as I defined date_filter = 01.01.2015) so I will compare DATE against dateadd(DAY, -1, GETDATE()).
but if I WILL select value in date_filter calender, then year(date_filter) will be different than 2015 (as I said, I won't choose date before 2016 because I have no data before 2016 in my table) so I will compare DATE against {{date_filter}} value which I just selected.
SELECT *
FROM lighting
WHERE cast("time" as timestamp) BETWEEN '23:55:00'::timestamp
AND now();
But I get the error as follows:
ERROR: column "23:55:00::timestamp" does not exist LINE 3: WHERE cast("time" as timestamp) BETWEEN "23:55:00::timestam...
My "time" column is as follows in a text format
05:50:53
06:58:38
07:30:42
What am I doing wrong?
It seems your field "time" is having values as time which can not be converted into timestamp.
So try this way:
SELECT *
FROM lighting
WHERE cast("time" as time) BETWEEN '23:55:00'::time
AND current_time;
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 have the following mysqli query in my search.php file with ajax.
SELECT demand2.* FROM demand2
WHERE ddate >= '$dto' AND ddate >= '$dfrom' AND !(mach='--' OR mach='-' OR mach='' OR mach='----' OR mach='-----' OR mach='---');
field ddate is datatype of varchar2
as I entered Date From 01-01-2019 and Date To 30-01-2019, it shows the result with previous date like 29-12-2019.
So I can't find the solution to get result within specified range. Please Help.
Just as rypskar said, your type of field should be date.
If you don't want to change the datatype of the fiels, then you have to convert the string to date with STR_TO_DATE(ddate, '%d-%m-%Y'). But this is not recommended, as it will cause performance degradation on larger tables.
So your query would be like that (not tested!):
SELECT demand2.* FROM demand2
WHERE STR_TO_DATE(ddate, '%d-%m-%Y') >= STR_TO_DATE('$dto', '%d-%m-%Y') AND STR_TO_DATE(ddate, '%d-%m-%Y') >= STR_TO_DATE('$dfrom', '%d-%m-%Y') AND !(mach='--' OR mach='-' OR mach='' OR mach='----' OR mach='-----' OR mach='---');
If you want to keep the varchar type of field and not make any runtime conversions, then you have to change the format to Y-m-d, so the comparison with character set will work the same as a date comparison.
Right now, you are getting the result of 29-12-2019 because 29 is between 1 and 30.
I'm trying to convert value for DIM_DT_ID to MMddYY. I'm successful in doinf that. However, query fails because ultimately I'm comparing a character value to date here. Is there a way by which I can get value for DIM_DT_ID in MMddyy format and its data type still remains DATE ?
Here DIM_DT_ID
SELECT DIM_DT_ID
DIM_DT_ID >= FORMATDATE('MMddyy',ADDDAY(TO_date('yyyy-MM-dd','2016-12-21'), -25)); from abc;
Regards,
Ajay
In Denodo, to convert a string to a date field, use "to_date()" (which returns a date).
Then, don't convert back to a string, leave that field as a date (so don't use "Formatdate()", which returns a string).
So:
SELECT *
FROM MyTable
WHERE now() >= to_date('yyyy-MM-dd',myStringFieldThatLooksLikeADate)
In my example, "now()" is a date, and so is the output of the to_date function... so you can do a comparison.
If you try to convert the date back to a string using formatdate, it won't work:
#This doesn't work:
SELECT *
FROM MyTable
WHERE now() >= formatdate('MMddyy',to_date('yyyy-MM-dd',myStringFieldThatLooksLikeADate))
It doesn't work because we are comparing a date ("now()") to a string.