I want to create a function to get the last 4 days on data including today. Here's my function, what am I missing? When I run a test I got an empty table.
df.where(trunc(col("date"),"day") >= date_add(current_date(),-4))
Try to use date_trunc instead. trunc only supports month and year. Also note that date_trunc accepts arguments in the reverse order of trunc.
df.where(date_trunc("day",col("date")) >= date_add(current_date(),-4))
Related
I'm trying to generate a query that will display the date difference between a date stored in a column and the current date, but i'm getting the error:
"Cannot call method "getTime" of null."
What function can I use to calculate this date difference in Google Cloud SQL?
Current code:
SELECT date, DATEDIFF(date, CURRENT_DATE()) AS daysLeft
FROM table;
Are you using a mysql instance on Google Cloud. If so try the following
SELECT date, DATEDIFF(date, CURDATE()) AS daysLeft
FROM table;
All that changed is CURDATE() instead of CURRENT_DATE()
Turns out, the error was in another part of the code. The originally posted SQL query works perfectly fine, the problem was the model was casting the result as a date variable instead of an int.
The DATEDIFF function requires three parameters.
SELECT date, DATEDIFF(day, date, CURRENT_DATE()) AS daysLeft
FROM table;
This should return your result in days left.
I'm trying to remove the hours, minutes and seconds from a date using the following code :
TRUNC(column_date, 'YY')
But I get this : 01JAN2008:00:00:00, while I want this : 01JAN2008.
What should I do?
TRUNC() works as expected, and returns the original date truncated to year, as a date datatype. You want to format this date before displaying it (this actually means converting it to a string). For this, you can use the TO_CHAR() function.
You probably want:
TO_CHAR(TRUNC(column_date, 'YY'), 'ddmonyyyy')
Please note that this expression could be simplified to avoid the use of TRUNC(), as follows:
'01JAN' || TO_CHAR(column_date, 'yyyy')
I think you want to_char: https://docs.oracle.com/database/121/SQLRF/functions216.htm#SQLRF06129
Try using it in this way:
SELECT TO_CHAR(column_date, 'DD/MON/YYYY')
FROM x;
Where x is the table you are trying to query.
you only need to use
select to_char(column_date,'ddMONyyyy') FROM yourTable
Even you can invoke with
select to_char(column_date,'ddMONyyyy','nls_date_language=english') FROM yourTable
to guarantee to see the abbreviation JAN in the case your session/system date language value is different than english
to display as expected.
In Bigquery's legacy SQL, I can get the start of week for a date by using
SELECT DATE((UTC_USEC_TO_WEEK(TIMESTAMP_TO_USEC(TIMESTAMP('2017-04-13 20:58:06 UTC')), 0)))
which returns 2017-04-09.
Is there a way to do this in BigQuery's standard SQL? There doesn't seem to be any equivalents for UTC_USEC_TO_WEEK and UTC_USEC_TO_MONTH.
It looks like BigQuery has a function named TIMESTAMP_TRUNC which may do what you want. It is referenced as the replacement for UTC_USEC_TO_DAY(t) in LegacySQL when used with a Day datepart. It also accepts Week and Month as a parameter which may meet your requirements.
TIMESTAMP_TRUNC(TIMESTAMP '2008-12-25 15:30:00', WEEK, 'UTC')
Here is the page for migrating from Legacy to Standard sql
This is better option that works now:
select DATE_TRUNC(date( '2008-12-25 15:30:00'), month)
I have Oracle database columns with the number format YYYYMMDD. I have not been successful in using this format with datediff to get the difference between two dates. The documentation I've read online uses a different format:
DATEDIFF(day,'2008-06-05','2008-08-05')
What's the best way for me to get number of days between two dates given the format available to me in Oracle? Answers not involving datediff are acceptable as long as it gets the number of days between two dates with the format YYYYMMDD.
Simple subtraction in Oracle:
SELECT TO_DATE('20080805','YYYYMMDD') - TO_DATE('20080605','YYYYMMDD')
FROM DUAL;
Oracle doesn't have a DATEDIFF() function. Instead, you can use simple arithmetic with Oracle dates, where subtracting one date from another gives the number of days, and where you can add an subtract days from a given date. (You can also subtract fractions of days, but that might be outside the scope of this answer.)
To convert your NUMBER dates of the format YYYYMMDD to actual dates, just use the TO_DATE() function (I am pretty sure that Oracle will implicitly convert the NUMBER value to a VARCHAR2 before converting to a date; if not, use TO_CHAR() to do that explicitly).
TO_DATE(20150301, 'YYYYMMDD')
To get the difference between two dates, you can do the following:
SELECT TO_DATE(my_number_date1, 'YYYYMMDD') - TO_DATE(my_number_date2, 'YYYYMMDD')
FROM my_table;
Incidentally, if you want to get intervals instead of days, convert to timestamp (using TO_TIMESTAMP()) instead of converting to date.
i using MySQL Query for my task.
And I interested using Date and time function.
can i use DAY(), WEEK(), and YEAR() at one query?
SELECT Object
FROM table
WHERE DAY(date) BETWEEN 1 AND 7
GROUP BY WEEK(date, 1), YEAR(date)
i want do this bcoz i'm worry if sometimes my program have an error because of the date setting and not recognize some date.please give me an input.
Yes, you can use them all in a single query.
The only disadvantage I can think of is that using any of the DAY, WEEK or YEAR functions won't be able to use the index on the column the function is applied to, assuming one is present.
If you're having issues relating to date formatting, you should get familiar with:
DATE_FORMAT
STR_TO_DATE