current_date usage - sql

I would like to subtract the current date from a given date in SQL or in JDBC. I would like to have the result in hours. Not sure how to treat the date in that case. Can some one give me a basic example Please.

You don't mention which database server you use - here's a sample in MySQL.
select hour(TIMEDIFF('2011-03-15 19:59:59.000001', now()))
Note: the "hour" function doesn't deal with rounding, so if you need that, you need to do some further arithmetic.
Date functions are pretty vendor-specific, so your mileage may vary....

In standard SQL
select (date '2011-03-16' - CURRENT_DATE) as days_different,
(date '2011-03-16' - CURRENT_DATE) * 24 as hours_different
days_different hours_different
--
1 24
As I write this, CURRENT_DATE = '2011-03-15'.

Related

Presto - how is there an alternative to to_char like postgresql?

I need to build a query in presto that could look back the trailing 70 days, the table I am working with is storing the dates in the format of 'YYYYMMDD'.
in postgresql, I can just simply write the where clause as
where date >= to_char(current_date - 70, 'YYYYMMDD')
and it'll pull in the date 70 days ago in a YYYYMMDD format.
However, in PrestoSQL, that function doesn't seem to exist, is there an alternative to this?
You can do this with date_format():
where date >= date_format(current_date - interval '70' day, '%Y%m%d')
Note that storing dates as strings is not a good practice at all - you should be using the proper date-like datatype - and then you won't need to do conversions at all.
You would just use date arithmetic:
where date >= current_date - interval '70' day
I'm not sure why you would want to involve strings in comparisons that are strictly date related.

Bigquery standardSQL: Current date minus a previous date with a result in number of days?

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)

Using 'IN' to compare Date - Oracle

I have a table that I need to filter based on date. For my problem, I have to filter the record which are relevant only for the current date without the time portion of the date.
For that I have used the following approach. Query seems to be working fine but I would like to know whether there are any pitfalls in this approach.Part of the WHERE clause related to the query is as follows.
AND TO_CHAR(EOD_DATE,'YYYY-MM-DD') IN TO_CHAR(sysdate,'YYYY-MM-DD')
EOD_DATE is the field in the table I take into consideration.I want records that EOD_DATE value is in the current date regardless of the time portion of EOD_DATE.
Thanks in advance :)
DATE datatype is up to a second precise. Therefore:
SQL> select trunc(sysdate) d_from, trunc(sysdate + 1) - 1/(24*60*60) d_to from dual;
D_FROM D_TO
------------------- -------------------
21.06.2018 00:00:00 21.06.2018 23:59:59
says that - if you want to be able to use index on the EOD_DATE column - you should consider
where eod_date between trunc(sysdate)
and trunc(sysdate + 1) - 1/(24*60*60)
Your solution works, but you are using a double conversion to compare strings instead of comparing dates. Besides, you don't need IN but an =
This could be a better, more readable way:
... and trunc(sysdate) = trunc(EOD_DATE)
Simply use TRUNC to remove the time component (or rather set it to 00:00:00) and then compare the dates:
WHERE TRUNC(EOD_DATE) = TRUNC(SYSDATE)

In MonetDB, how can I get the date as an integer?

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;

Date not valid for month specified

I have a problem when running this Oracle SQL statement:
SELECT *
FROM tbl_content
WHERE last_updated >= (systimestamp - INTERVAL '1' month(1))
ORDER BY last_updated desc
And this error:
java.sql.SQLException: ORA-01839: date not valid for month specified
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:111)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:330)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:287)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:742)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:212)
at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:951)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1053)
at oracle.jdbc.driver.T4CPreparedStatement.executeMaybeDescribe(T4CPreparedStatement.java:835)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1123)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3284)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3328)
at com.vtdd.sms.content.xskt.XsktService.getKQXSFollowArea(XsktService.java:4044)
at com.vtdd.sms.content.xskt.XsktService.getMessages(XsktService.java:421)
at com.vht.sms.content.ContentAbstract.getSubmitMessages(ContentAbstract.java:47)
at com.vht.sms.content.ContentFactory.getSubmitMessages(ContentFactory.java:335)
at com.vht.sms.content.ContentFactory.run(ContentFactory.java:62)
at java.lang.Thread.run(Thread.java:662)
Could you tell me what is wrong?
Firstly, why are you using systimestamp? If you want this to the month then surely sysdate is exact enough? Secondly, I like - i.e. it's personal preference - to make it extremely clear what's happening. Oracle has an add_months function, which will do what you want. So your query could easily be:
SELECT *
FROM tbl_content
WHERE last_updated >= add_months(sysdate, -1)
ORDER BY last_updated desc
What is actually wrong is that interval arithmetic doesn't adjust days - see the 6th bullet in the link:
When interval calculations return a datetime value, the result must be an actual datetime value or the database returns an error.
ADD_MONTHS does; as that link says:
If date is the last day of the month or if the resulting month has fewer days than the day component of date, then the result is the last day of the resulting month.
So, ADD_MONTHS(DATE '2011-12-31', -1) gives you 2011-11-30, while DATE '2011-12-31' - INTERVAL '1' MONTH tries to give you 2011-11-31, which as the message says, isn't a valid date.
(It's debatable if this behaviour is actually wrong; it's unexpected, but I believe it's conforming to ANSI. There may be times you want it to work this way, though I can't think of any...)