I'm trying to get the start of the week (Sundays, as a date) for a given date. This works except on Sundays since the day of the week origin begins on Monday:
SELECT DATE_SUB(FROM_UNIXTIME(UNIX_TIMESTAMP(CURRENT_DATE(), 'yyyy-MM-dd')), CAST(FROM_UNIXTIME(UNIX_TIMESTAMP(CURRENT_DATE(), 'yyyy-MM-dd'), 'u') AS INT))
The function above would return '2018-04-15' for a supplied date of '2018-04-22' whereas I want '2018-04-22'. Is the only recourse to write an case statement to offset for Sundays? I was hoping there was a nice parameter to FROM_UNIXTIME() that would have the weeks start on Sundays. I didn't find them in these docs:
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF
https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
You could get the result with a simple trick without case statements.
Calculate the modulus value of the weekday with 7 and you should get your result.
SELECT DATE_SUB(CURRENT_DATE(), CAST(DATE_FORMAT(CURRENT_DATE(),'u')%7 AS INT));
Related
How can I get a date using the week of the year in Oracle SQL?
I need to search for entries created after the beginning of the current week. Currently, I get the week of the year by doing select to_char(sysdate,'WW') from dual;, but then I can't do
select * from table where date > to_date(to_char(sysdate,'WW'), 'WW') because I get
ORA-01820: format code cannot appear in date input format
01820. 00000 - "format code cannot appear in date input format"
*Cause:
*Action:
You don't need to convert to a string and back, you can use truncate:
select * from table where date > trunc(sysdate,'WW')
Read more about the trunc() function and how the format model is applied.
Notice that WW gives you the same day as the first day of the year, so right now that would give 2020-09-02, which is a Wednesday - possibly not what you'd expect. It depends on your requirements of course, but you might want to work with IW which always starts from Monday, and would give 2020-09-07. If you have a different start day you can add or subtract a day, e.g. if your week starts on Sunday.
According to ORA-doc:
ORA-01820: format code cannot appear in date input format
Cause: A date specification contained an invalid format code. Only the following may > be specified when entering a date: year, month, day, hours, minutes, seconds, Julian day, > A.M./P.M. and B.C./A.D.
Action: Remove the invalid format code from the date specification.
You can't pass the weeknum to to_date() function. What you can do is e.g., the following
select * from table where date > (next_day(trunc(sysdate), 'SUNDAY') - 7)
Basically, next_day returns first date that meets specified weekday. Let's assume it's Monday 2020-09-07, next_day will return you the closest SUNDAY in the future, that is 2020-09-13, so you need to substract 7 to get date of the current week beginning. You can read more about it here
I would like to the current date minus a previous begin date with the result with the result being the number days there is a difference of the two?
I have attempted the following: date_sub(Begindt, INTERVAL current_date)
Also, will I have to cast things differently?
Below is for BigQuery Standard SQL
DATE_DIFF(CURRENT_DATE(), Begindt, DAY)
See more for DATE_DIFF()
Above assumes the Begindt field is of DATE type
If not, you should cast to DATE type via CAST or PARSE_DATE functions
Are you finding something like below
DATE_DIFF(Begindt, CURRENT_DATE, day)
I currently have a code that takes a date and returns a Sunday Start date. What I want is to get a Monday start date instead like the weekofyear() function.
Below is my current code where evt_time is my datetime variable:
date_sub(evt_time,pmod(datediff(to_date(evt_time),'1900-01-07'),7))
For instance, I would want 6/4/2018-6/10/2018 to be group into 6/4/2018.
Get the weekday with u argument and then use arithmetic to get the week start date as Monday.
select date_add(to_date(evt_time)
,1-cast(from_unixtime(unix_timestamp(to_date(evt_time),'yyyy-MM-dd'),'u') as int))
I want to be able to do something like
SELECT cast(my_date_col AS int) FROM my_table;
I would like to get the integer which MonetDB uses internally, i.e. the value you'd find if you looked into the BAT structure and got the appropriate element in code in MonetDB's GDK. Now, AFAICT, this internal value is the number of days since the Epoch, being Jan 1st on "Year 0" (so January 3rdt year 2 would be 366+365+2 = 732).
The best I could actually manage is
SELECT my_date_col AS int - cast('1-1-1' AS date) - 366 FROM my_table;
As MonetDB won't accept "Year zero" dates. This is rather an ugly hack, I'd like to do better. Help me?
If you're trying to get the number of days between "my_date_col" and 1970-01-01, in standard SQL you'd just subtract the one from the other. Your platform, monetdb, seems to support this syntax, but I don't have it installed. I wrote these examples in PostgreSQL.
select current_date - date '1970-01-01' as num_days;
num_days
--
16213
Check that result by adding 16213 days to the current date (2014-05-23).
select cast ((date '1970-01-01' + interval '16213' day) as date) as target_date
target_date
--
2014-05-23
The cast is necessary, because the result of this addition is a timestamp, not a date.
In your case, you want a column name instead of "current_date". So you're looking for something along these lines.
select my_date_col - date '1970-01-01' as num_days
from your-table-name;
In Oracle SQL Developer, how does one determine the calendar date of the next occurrence of Wednesday, based on the current date. I have searched and seen many examples for Ruby, PHP, MYSQL, but not for oracle sql. I read this article about DATETIME calculations, but these we have not gone over in class. We are working with date conversion, case, when, timestamps, and intervals but I'm not sure how to go about this problem.
Nex_day function will return the date of the first weekday, specified as the second parameter of the function, later than a date specified as the first parameter of the function. For example:
SQL> select next_day(sysdate, 'WEDNESDAY') next_day
2 from dual
3 ;
NEXT_DAY
-----------
05-DEC-2012
You can use DATE function called NEXT_DAY
Try this
SELECT NEXT_DAY(SYSDATE,'WEDNESDAY') "Next WED"
FROM DUAL ;