Creating daterange in SQL - sql

Given the date range:
'20180504' and '20180425'
I want to write a query that would return the following dates
'20180504'
'20180503'
'20180502'
'20180501'
'20180430'
'20180429'
'20180428'
'20180427'
'20180426'
'20180425'
Could anyone suggest what would be the best way to generate dates like these? The date format should be same as above, because I would use it to extract data from another table. Thanks!

You can use a hierarchical query:
SQL Fiddle
Query 1:
SELECT TO_CHAR( DATE '2018-04-25' + LEVEL - 1, 'YYYYMMDD' ) AS value
FROM DUAL
CONNECT BY DATE '2018-04-25' + LEVEL - 1 <= DATE '2018-05-04'
Results:
| VALUE |
|----------|
| 20180425 |
| 20180426 |
| 20180427 |
| 20180428 |
| 20180429 |
| 20180430 |
| 20180501 |
| 20180502 |
| 20180503 |
| 20180504 |

You seem to want a string output, so you can generate the dates and then convert to strings:
with dates as (
select date '2018-04-25' + level - 1 as dte
from dual
connect by date '2018-04-25' + level - 1 <= date '2018-05-04'
)
select to_char(dte, 'YYYYMMDD')
from dates;
Here is a rextester.

Related

GBQ - Merge cells of a column across rows

I have a data table that looks like this
start_date | end_date | string
date x | date y | apple
date x | date y | orange
date z | date y | grape
I want to merge the string column if the start_date and end_date are the same across rows. So out put would look like this
start_date | end_date | string
date x | date y | apple/orange
date z | date y | grape
I am using Google big query SQL. Any help would be greatly appreciated.
Thank you.
Below is for BigQuery Standard SQL
#standardSQL
SELECT start_date, end_date, STRING_AGG(str, '/') str
FROM `project.dataset.table`
GROUP BY 1, 2
You want GROUP_CONCAT :
select start_date, end_date, GROUP_CONCAT(string) as string
from table t
group by start_date, end_date;

SQL statement to convert dd/mm/yy nvarchar(255) to Date format dd/mm/yyyy

I am struggling with a SQL statement to convert imported data that is in a format dd/mm/yy and of type nvarchar(255) into a date type 'dd/mm/yyyy' format.
The sample data is as follows:
Date:
'23/10/17'
'24/10/17'
'25/10/17'
'26/10/17'
'27/10/17'
To Date Format:
'23/10/2017'
'24/10/2017'
'25/10/2017'
'26/10/2017'
'27/10/2017'
I am using SQL Server 2008 R2
Here you go:
WITH CTE AS (
SELECT '23/10/17' AS MyDate
UNION
SELECT '24/10/17'
UNION
SELECT '25/10/17'
UNION
SELECT '26/10/17'
UNION
SELECT '27/10/17'
)
SELECT * ,
CONVERT(DATE, MyDate,3) AS MyDate,
CONVERT(VARCHAR, CONVERT(DATE, MyDate,3), 103) AS ExactFormat
FROM CTE;
Results:
+----------+---------------------+-------------+
| MyDate | MyDate | ExactFormat |
+----------+---------------------+-------------+
| 23/10/17 | 23.10.2017 00:00:00 | 23/10/2017 |
| 24/10/17 | 24.10.2017 00:00:00 | 24/10/2017 |
| 25/10/17 | 25.10.2017 00:00:00 | 25/10/2017 |
| 26/10/17 | 26.10.2017 00:00:00 | 26/10/2017 |
| 27/10/17 | 27.10.2017 00:00:00 | 27/10/2017 |
+----------+---------------------+-------------+
Demo
Try using CONVERT.
SELECT convert(datetime, datecolumn, 103);
Refer these links for more detail
http://www.sqlusa.com/bestpractices/datetimeconversion/
https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql
Hope this helps.
USE REPLACE
CONVERT(DATE,REPLACE('23/10/17', '/17', '/2017'))

Use Date difference in a where clause with SQL

From a table named 'Subscriptions' I want to list all item that expire between 0 and 3 days from the current day.
$Today = date('Y-m-d');
|--------|----------|------------|
| SUB_Id | SUB_Name | SUB_End |
|--------|----------|------------|
| 1 | Banana | 2017-12-01 |
| 2 | Apple | 2017-11-03 |
| 3 | Pear | 2017-11-03 |
|--------|----------|------------|
I should have the last two rows as the SUB_End - $Today is <= 3 days.
What I try:
select * from Subscriptions
where DATEDIFF(SUB_End , $today) <= 3;
I would do this entirely in SQL:
select s.*
from Subscriptions s
where sub_end >= curdate() and
sub_end <= curdate() + interval 3 day;
You can use the following query, this is only for sql server :
select * from Subscriptions
where DATEDIFF(day, SUB_End , getdate()) <= 3;
You can try this.
Edited: Oracle based solution.
select s.*
from Subscriptions s
where s.sub_end BETWEEN TRUNC(SYSDATE - 3) AND TRUNC(SYSDATE)

Converting date into integer (1 to 365)

I have no idea if there is a function in postgres to do that, but how can I convert a date (yyyy-mm-dd) into the numeric correspondent in SQL?
E.g. table input
id | date
------+-------------
1 | 2013-01-01
2 | 2013-01-02
3 | 2013-02-01
Output
id | date
------+-------------
1 | 1
2 | 2
3 | 32
You are looking for the extract() function with the doy("Day Of Year") argument, not day ("day of the week"):
select id, extract(doy from "date")
from the_table;
Acording to this documentation an other option is to do something like:
SELECT id, DATE_PART('day', date - date_trunc('year', date)) + 1 as date
from table_name;
Here you can see a sql-fiddle.

Having issues joing a table with a recursive function in Sqlite

I'm building a complex query but I have a problem...
Pratically, I retrieve a dates range from recursive function in sqlite:
WITH RECURSIVE dates(d)
AS (VALUES('2014-05-01')
UNION ALL
SELECT date(d, '+1 day')
FROM dates
WHERE d < '2014-05-5')
SELECT d AS date FROM dates
This is the result:
2014-05-01
2014-05-02
2014-05-03
2014-05-04
2014-05-05
I would join this query on other query, about this:
select date_column, column1, column2 from table
This is the result:
2014-05-03 column_value1 column_value2
Finally, I would like to see a similar result in output (join first query and date_column of second query):
2014-05-01 | | |
2014-05-02 | | |
2014-05-03 | column_value1 | column_value2 |
2014-05-04 | | |
2014-05-05 | | |
How can I obtain this result?
Thanks!!!
Why don't you simply do something like this ?
WITH RECURSIVE dates(d)
AS (VALUES('2014-05-01')
UNION ALL
SELECT date(d, '+1 day')
FROM dates
WHERE d < '2014-05-5')
SELECT
dates.d AS date
,table.column1
,table.column2
FROM dates
left join table
ON strftime("%Y-%m-%d", table.date_column) = dates.date
Perhaps you will need to convert your date...