I'm working on a really basic list to retrieve records by date based on current day of the week.
I can't figure out how to get the expression to work correctly.
What I'm trying to accomplish is
Schedule Mon - Friday.
If day_of_week(current_date) = Monday Then get [record date] for prior Friday (Monday pulls Fridays records)
Else If day_of_week(current_date) = Tuesday Then get [record date] for days between Saturday - Monday (Tuesdays pull Sat - Mon records)
Else get [record date] = current_date - 1 (All other business days pull prior days records)
I've tried creating a filter on [record date] based on a query expression [data item1] to get day of week.
day_of_week(current_date,1)
But no luck. Appreciate any help possible. Thanks!
Start with the data in the database, then compute the date you want the data presented.
Here is a sample filter expression using a brute force method to accomplish this:
current_date =
case
when _day_of_week ([Sales (query)].[Time].[Date], 1) in (6,7)
then _add_days ([Sales (query)].[Time].[Date], 9 - _day_of_week ([Sales (query)].[Time].[Date], 1))
when _day_of_week ([Sales (query)].[Time].[Date], 1) in (5)
then _add_days ([Sales (query)].[Time].[Date], 3)
else _add_days([Sales (query)].[Time].[Date], 1)
end
You could absolutely use IF-THEN-ELSE for this. I usually use CASE-WHEN-THEN because I'm usually dealing with SQL databases and it's just easier to do it the same way.
Related
Let's say I have a column with values in the format of a datetime '2021-07-01 00:00:00.000', how do I get the last date of the week (Sunday) for any datetime such as this in sqlite3? The day of the last date of the week is a Sunday.
So the answer for this date would want something like 2021-07-04 or 2021/07/04 (removing the time part) which is the date of the Sunday for the week the date 2021-07-01 is belonging to.
I know how this can be done in sqlserver but I do not know how to do it in sqlite.
Could someone please assist me on this? Let me know if you need more clarification.
Here is my attempt on sqlserver, but i need to get the same result in sqlite
SELECT
somedate,
CONVERT(VARCHAR(10),DATEADD(DAY, 7 - DATEPART(WEEKDAY, [somedate]), [somedate]),111) AS Last_Date_Of_Week
FROM table
apparently i can't do this in sqlite because sqlite does not have an official datetime type.
Sqlite date and time functions consider Sunday to be the first day of the week, and have a weekday modifier to advance a given timestamp to the given day of the week (Using 0-based indexing from Sunday). So:
sqlite> SELECT date('2021-07-01 00:00:00.000', 'weekday 0');
date('2021-07-01 00:00:00.000', 'weekday 0')
--------------------------------------------
2021-07-04
I have current_date in Teradata which 18 DEC 2019
I have to calculate the previous quarter start date and end date from the above current_date.
Input = '2019-12-18'
Output Start Date = '2019-07-01'
Output End Date = '2019-09-30'
You should be able to do this using the TRUNC function, something like:
SELECT
TRUNC(ADD_MONTHS(CURRENT_DATE, -3), 'Q') AS Start_Quarter, -- Previous quarter start
TRUNC(CURRENT_DATE, 'Q') - 1 AS End_Quarter -- Current quarter start date - 1 day
Give it a try and let me know. This assumes the mistake in the manual is still considered a "mistake".
Also, depending on what TD version you're using, you may be able to use built-in functions:
SELECT
TD_QUARTER_BEGIN(CURRENT_DATE) AS Start_Quarter,
TD_QUARTER_END(CURRENT_DATE) AS End_Quarter
Reference
TD Manual
Built-in functions
I have a simple SQL for calculating week number in my reports on SQLite
SELECT STRFTIME('%W', 'date_column')
It was correct for 2009-2012. In 2013 I got always the wrong week number.
For example
SELECT STRFTIME('%W', '2012-02-28')
return '09' and this is correct.
SELECT STRFTIME('%W', '2013-02-28')
return '08' and this is wrong. We have the 9th week.
Is there something in SQLite date time functions that I don't understand? Or is it a bug of SQLite?
CL's answer works fine for OP's definition of "right", which is not quite the same as ISO definition. ISO week numbers are always in the range 1-53 (no week 0), and the last 3 days of a year may fall into Week 1 of the following year, just like the first 3 days may fall into Week 52 or 53 of the preceding year. To take these corner cases into account, you need to do something like:
SELECT
(strftime('%j', date(MyDate, '-3 days', 'weekday 4')) - 1) / 7 + 1 AS ISOWeekNumber
FROM MyTable;
As a side note, SQLite's Date and Time documentation does link to the POSIX strftime man page, which defines %W modifier as:
"week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0."
To convert from SQLite's undocumented week definition (first week is the week with the year's first Monday in it, or the week with 7 January in it) to the ISO week definition (first week is the week with the year's first Tuesday in it, or the week with 4 January in it), we let SQLite compute the week of the year's 4 January. If that is not one, we have to increase the week number:
SELECT strftime('%W', MyDate)
+ (1 - strftime('%W', strftime('%Y', MyDate) || '-01-04'))
FROM MyTable
I'm trying to obtain the current week for date comparison in SQLite.
I have no problem for last month, last year, today, yesterday... but don't find the solution to have the current week.
I tried lot of things like:
SELECT tastings.* FROM tastings
WHERE (DATE(tastings.date) > DATE('now','weekday 1','+ 7 days'))
Can you help me ? Thanks.
This code gives you the week number where the first day of week is monday. It also works well for last and first weeks of the year.
strftime('%W', 'now', 'localtime', 'weekday 0', '-6 days')
I guess you want compare 2 date, Assume you have a table named _testTbl and have 3 column _id INTEGER, _name TEXT, _recordDate TEXT
you want name that record this week
you can use below code:
SELECT * FROM _testTbl
WHERE _recordDate > datetime('now', 'start of day', 'weekday 6', '-7 day')
note that this week start by saturday (sunday 0, monday 1, ..., saturday 7)
this t-sql means:
datetime is a sqlite date and time function.
first parameter is given time: 'now' means the current time.
second parameter take the time to start of day.
third parameter take time to the next weekday number (in this case, saturday).
fourth parameter take time to start of week
What is stored inside the tastings.date column? Note that SQLite does not have “timestamp” type affinity, so probably you store Text (some representation of the date) or integer (julian day, epoch…)
All time and date functions expect a valid time string and convert that time string to another string format. If tastings.date contains a week number then use:
AND cast(tastings.date AS TEXT) = strftime('%W','now')
This helps me to compare the 2 dates using the week of the year.
AND ( strftime('%W', tastings.date) = strftime('%W', 'now') )
Thanks you.
This has gotten me a little paranoid, but I'm retrieving a set of records that fall within a period of time, say, the period from the january 1, 2011 (starting at midnight) to march 31, 2011 (all records up to 11:59:59 PM)
I'm using the condition
t.logtime between to_date('2011-01-01', 'yyyy-mm-dd') and to_date('2011-03-31')
Note that logtime is a datetime field.
Does this reflect what I want? Or am I actually missing 24 hours less a second?
I could specify the time as well, but I was hoping I this could be done without it.
Yes, you are missing nearly all of the last day. There are various solutions; probablt the simplest is:
t.logtime >= to_date('2011-01-01', 'yyyy-mm-dd')
and t.logtime < to_date('2011-04-01', 'yyyy-mm-dd')
I'd use the ANSI date literal syntax too:
t.logtime >= date '2011-01-01'
and t.logtime < date '2011-04-01'
Another way is:
trunc(t.logtime) between date '2011-01-01' and date '2011-03-31'
but note that that can no longer use an index on logtime (though it can use an index on trunc(logtime)).