I'd like to add 100 days to a field in a query:
SELECT DATE_ADD("date" + INTERVAL '100' DAY) FROM "history";
or whatever… but it doesn't work with the default HSQLDB frontend…
Any workaround ?
You don't need the date_add(), just add the interval to the column:
SELECT "date" + INTERVAL '100' DAY
FROM "history";
The default HSQLDB version in OpenOffic and LO is 1.8. It does not supports this function.
HSQLDB version 2.x supports several functions and expressions for date / time arithmetic.
If the column is named "date", this form is also supported. Note there is no underscore in the function name:
SELECT DATEADD('day',100, "date") FROM "history";
Related
I want to take out data for every date range in Data Studio without the need to change date range selectors in my BigQuery all the time. However, not sure if it is even possible to do so. The reasons I do this is to make sure that the queried data is only for 30 days, as later it do some kind of segmentation using that 30 days data.
Then I figured out that the Data Studio can use dynamic_date, however this way will never produce any datatable (datatable will be used to do other queries from it). Is it possible to do dynamic_date in BigQuery instead? like retrieving data from BigQuery using a date range not previously defined in the query.
From my point of view, code should be like :
SELECT
ID,
FROM `table`
WHERE DATE(Timestamp) between $DS_START_DATE and $DS_START_DATE + INTERVAL 30 DAY)
or
WHERE DATE(Timestamp) >= #DS_START_DATE
I believe in pure Bigquery you can use DECLARE clause for that purpose, defining variables of the specified type:
declare DS_START_DATE date default "2020-03-03";
declare DS_END_DATE date default "2020-03-04";
WITH sample AS (
SELECT '10001' AS id, cast('2020-03-01' AS timestamp) as date_id UNION ALL
SELECT '10002', cast('2020-03-02' AS timestamp) UNION ALL
SELECT '10003', cast('2020-03-03' AS timestamp) UNION ALL
SELECT '10004', cast('2020-03-04' AS timestamp) UNION ALL
SELECT '10005', cast('2020-03-05' AS timestamp) UNION ALL
SELECT '10006', cast('2020-03-06' AS timestamp)
)
select id, date_id from sample
where date(date_id) between DS_START_DATE and DS_END_DATE
Alternatively, you can take a look at parameterized queries, however as I mentioned in the comment, they are not supported in classic BigQuery web UI.
i created query!
SELECT install_time, Count(id)
FROM (SELECT DISTINCT install_time, id
FROM mytab
where event='run'
and install_time>='09.01.2017' and install_time<='09.05.2017')
GROUP BY install_time
but install_time has that format like 09-05-2017 5:34:23
but i need this format without hours,minutes and seconds
09-05-2017
How change this date format?
First, you don't need a subquery.
Second, date/time functions vary by database. Here is one method that assumes a date() function for removing the time component:
SELECT DATE(install_time), COUNT(DISTINCT id)
FROM mytab
WHERE event = 'run' AND
install_time >= '2017-09-01' AND install_time < '2017-09-06'
GROUP BY DATE(install_time)
ORDER BY DATE(install_time);
Note that I also changed the date format to a standard format -- this works in most databases. And, I changed the second comparison for install_time to take the time component into account.
Here are some equivalents to the date() function (which works in MySQL, SQLite, BigQuery, and DB2 at least):
SQL Server: cast(install_time as date)
Postgres: date_trunc('day', install_time)
Oracle: trunc(install_time)
Teradata: cast(install_time as format 'YYYYMMDD')
I'm writing a SQL query on a timesheet report. I need the report to return only the details for the week of the selected date.
E.g., if I pick 02/01/2012 (dd/MM/yyyy), then it should only return results between 02/01/2012 and 08/01/2012.
Thanks
SELECT
*
FROM
yourTable
WHERE
dateField >= #yourDate
AND dateField < #yourDate + 7
Some variations of SQL may have specific ways of adding 7 days to a datevalue. Such as...
- DateAdd(Day, 7, #date)
- DATE_ADD(#date, INTERVAL 7 DAYS)
- etc, etc
This option is both index friendly, and is resilient to database fields that have time parts as well as date parts.
You easiest is the equivalent of WEEK_OF_YEAR function in your SQL engine
But you can also use DATE_ADD
WHERE table.date BETWEEN target_date AND DATE_ADD(target_date,INTERVAL 7 DAY)
That depends on the database system you're using. MySQL has a function calles WEEK(), SQL Server can do something like this with the DATEPART() function:
MySQL:
SELECT
*
FROM table
WHERE WEEK(date_col) = WEEK('02/01/2012');
SQL SERVER:
SELECT
*
FROM table
WHERE DATEPART(WEEK, datecol) = DATEPART(WEEK,'02/01/2012');
SELECT * FROM table_name
WHERE date BETWEEN '1/02/2012' AND '1/08/2012';
you can replace the example date with your date and yourdate + 6.
Look here for example: http://www.roseindia.net/sql/sql-between-datetime.shtml
I need to get various parts of a TIMESTAMP field - specifically year, localized month name (in Russian), day of month and hours interval (like '11 - 12').
Currently i came up with this:
select
extract (year from prt.dtbegin) as f_year,
(
case extract (month from prt.dtbegin)
when 1 then 'Январь'
when 2 then 'Февраль'
/* ... */
when 12 then 'Декабрь'
end
) as f_month,
cast (lpad (extract (day from prt.dtbegin), 2, 0) as char(2)) as f_day,
(
cast (lpad (extract (hour from prt.dtbegin), 2, 0) as char(2))
|| ' - '
|| cast (lpad (extract (hour from prt.dtbegin) + 1, 2, 0) as char(2))
) as f_hour
from prt
It works fine (interval '23 - 24' is OK at the moment), but I don't like it, especially CASE sentence with each and every month.
So, i'd like to know, is there any common way of getting localized month names in Firebird? Also, can i format extracted parts of timestamp, instead of current cast-lpad-extract construct?
Thanks in advance.
What about a reference table with the localised strings in them with a key of the month number. I thought this was done in the Firebird system tables, but have not been able to find documentation or the location in the system tables, if indeed it is there.
EDIT:
I have checked the system tables, and the documentation, and the literals are not available within Firebird itself. Sorry :-)
Instead of CASE you can use DECODE builtin function: http://www.firebirdsql.org/refdocs/langrefupd21-intfunc-decode.html
I don't think there's a built-in feature for that. You should consider writing your own UDF for that. Once done, it's very simple to use. The following resources should point you in the right direction:
http://www.firebirdsql.org/index.php?op=useful&id=deatz_udf
http://www.firebirdfaq.org/faq83/
http://www.ibphoenix.com/downloads/writing_internal_udfs.pdf
I sure hope next major release (3.0) will support writing internal functions.
Update
Firebird 3.0 will support internal SQL Functions: http://tracker.firebirdsql.org/browse/CORE-2047
Suppose I have a date 2010-07-29. Now I would like to check the result of one day ahead. how to do that
For example,
SELECT *
from table
where date = date("2010-07-29")
How to do one day before without changing the string "2010-07-29"?
I searched and get some suggestion from web and I tried
SELECT *
from table
where date = (date("2010-07-29") - 1 Day)
but failed.
MySQL
SELECT *
FROM TABLE t
WHERE t.date BETWEEN DATE_SUB('2010-07-29', INTERVAL 1 DAY)
AND '2010-07-29'
Change DATE_SUB to DATE_ADD if you want to add a day (and reverse the BETWEEN parameters).
SQL Server
SELECT *
FROM TABLE t
WHERE t.date BETWEEN DATEADD(dd, -1, '2010-07-29')
AND '2010-07-29'
Oracle
SELECT *
FROM TABLE t
WHERE t.date BETWEEN TO_DATE('2010-07-29', 'YYYY-MM-DD') - 1
AND TO_DATE('2010-07-29', 'YYYY-MM-DD')
I used BETWEEN because the date column is likely DATETIME (on MySQL & SQL Server, vs DATE on Oracle), which includes the time portion so equals means the value has to equal exactly. These queries give you the span of a day.
If you're using Oracle, you can use the + and - operators to add a number of days to a date.
http://psoug.org/reference/date_func.html
Example:
SELECT SYSDATE + 1 FROM dual;
Will yield tomorrow's date.
If you're not using Oracle, please tell use what you ARE using so we can give better answers. This sort of thing depends on the database you are using. It will NOT be the same across different databases.
Depends of the DateTime Functions available on the RDBMS
For Mysql you can try:
mysql> SELECT DATE_ADD('1997-12-31',
-> INTERVAL 1 DAY);
mysql> SELECT DATE_SUB('1998-01-02', INTERVAL 31 DAY);
-> '1997-12-02'
If youre using MSSQL, you're looking for DateAdd() I'm a little fuzzy on the syntax, but its something like:
Select * //not really, call out your columns
From [table]
Where date = DateAdd(dd, -1, "2010-07-29",)
Edit: This syntax should be correct: it has been updated in response to a comment.
I may have the specific parameters in the wrong order, but that should get you there.
In PL SQL : select sysdate+1 from dual;