SQLITE strftime() function issue - sql

SELECT strftime('%W', 'Week'), sum(income) FROM tableOne GROUP BY Week;
Format for date is a simple date: YYYY-MM-DD
PROBLEM: When run no value for the Week column is provided. Any suggestions?
There is data in the table and when the query is run the income is summarized by the date in the week column. Thing is, this column contains a date that may be any day of the week and often multiple different days of the same week. I need to summarize the income by week.

In SQL, 'Week' is a string containing four characters.
To reference the value in a column named "Week", remove the quotes: Week.

Related

Big query sql: month to date query

I have daily data and the code im using creates a report between some dates. I want to replace the date interval with month to date. So every time it will generate a month to date report based on which month we are in. Is there a simple way to do it? thanks :)
An example using BigQuery Standard SQL
SELECT
*
FROM
your_table
WHERE
your_date_field BETWEEN
DATE_TRUNC(CURRENT_DATE(), month) --to get start date of current month
AND
CURRENT_DATE()
You should be able to use that in a WHERE clause and

SQL query between two dates using regex

I'm doing a sql query to extract orders from date to date.
I think I need to use LIKE because dates are strings and has days, but I have to consider only year and month, for example:
order date -> 2019-01-01
regex -> LIKE '%2019-01-%' (all orders in done in january)
and then I have to to the same thing for another date (let's say '%2019-03-%'). After that I use the two dates to extract time with BETWEEN.
The problem is that I don't know how to put these two things together.
You can use the substring and then do comparison as follows:
select * from orders
where substring(date, 1, 7) between '2019-01' and '2019-03'
Or you can convert your date column into date and then compare it with dates.

Combining year column and month column to date

In my database, I have a report_year column (char(4)) and a report_month column (varchar(2)). I am making an ssrs report that would use a stored procedure and would pull data from this table and my parameters are the date and year. I am succesful at doing this by casting both of the columns and concatenating them, also adding a "/" in between. So in SSRS report, the parameter that users need to put is the month and date (ex. 09/2016).
Users want a drop down to get the dates. Since my parameter is a varchar, it would ask literally for the month and the year formatted above. Is there anyway to cast this to date without the day itself, only just the month and the year? I tried datediff and dateadd functions but I am not having any luck.
Select BegMonth = cast(Replace('09/2016','/','/01/') as date)
,EndMonth = EOMonth(cast(Replace('09/2016','/','/01/') as date))
Returns
BegMonth EndMonth
2016-09-01 2016-09-30

Get the month and year now then count the number of rows that are older then 12 months in SQL/Classic ASP

I know this one is pretty easy but I've always had a nightmare when it comes to comparing dates in SQL please can someone help me out with this, thanks.
I need to get the month and year of now then compare it to a date stored in a DB.
Time Format in the DB:
2015-08-17 11:10:14.000
I need to compare the month and year with now and if its > 12 months old I will increment a count. I just need the number of rows where this argument is true.
I assume you have a datetime field.
You can use the DATEDIFF function, which takes the kind of "crossed boundaries", the start date and the end date.
Your boundary is the month because you are only interested in year and month, not days, so you can use the month macro.
Your start time is the value stored in the table's row.
Your end time is now. You can get system time selecting SYSDATETIME function.
So, assuming your table is called mtable and the datetime object is stored in its date field, you simply have to query:
SELECT COUNT(*) FROM mtable where DATEDIFF(month, mtable.date, (SELECT SYSDATETIME())) > 12

Sql select statement that will bring back my data in weeks, using derived tables with JasperReports

I'm currently running the following sql statement in JasperReports Server to bring back my data using derived tables.
Select count(createddate) as ModulesCreatedDuringPastWeek,
count(updateddate) as ModulesUpdatedDuringPastWeek,
createddate,
updateddate
from merchendisingmodule
group by merchendisingmodule.createddate, merchendisingmodule.updateddate
However when grouping my data, I am only able to do it in Year, quarter, month and day. However for my report I'm needing the data to be group weeks, and so I was wondering what I will need to add to my code to do this.
DATEADD(D,-DATEPART(weekday,createddate)+1,createddate)
I use this method to prevent issues around the year transitions (week 53 in first days of januari and also in the last days of december, will group days together that are 360 days apart).
I use the first day of the week, instead of week numbers. I can use these dates to group by.
Also this will ensure that every week is 7 days long, instead of the last week of the year being only 3 or 4 days long.
Btw, in this example the first day of the week is sunday.
If your dates include time, use:
CAST(FLOOR(CAST(createddate AS FLOAT)) AS DATETIME)
instead of createddate in the above SYNTAX