SQlite strftime Query Isuue - sql

SELECT *
FROM BillData
WHERE date BETWEEN strftime('%d-%m-%Y','2020-05-04') AND strftime('%d-%m-%Y','2020-07-08')
Query Result-it picks date Where the year is 2019
SN Date
1 07-07-2020
2 07-06-2020
3 07-01-2020
4 08-07-2020
5 08-07-2019 <------------(return this result 2019)need a solution
for this issue

Use a format that matches the date,
strftime('%Y-%m-%d','2020-05-04')

Related

Get the number of the weekday from each date in a date list

DB-Fiddle
CREATE TABLE dates (
date_list DATE
);
INSERT INTO dates
(date_list)
VALUES
('2020-01-29'),
('2020-01-30'),
('2020-01-31'),
('2020-02-01'),
('2020-02-02');
Expected Results:
Weekday
2
3
4
5
6
I want go get the number of the weekday for each date in the table dates.
Therefore, I tried to go with the solution from this question but could not make it work:
SELECT
EXTRACT(DOW FROM DATE d.date_list))
FROM dates d
How do I need to modify the query to get the expected result?
Get rid of the date keyword it is only needed to introduce a DATE constant. If you already have a DATE value (which your column is) it's not needed:
select extract(dow from d.date_list)
from dates d

Date format in sas concatenate with a proc sql under conditions

i have a macro_variable :%let date=201909
and table :
ID Sending-date item
1 15-jul-2019 A
2 23-sep-2019 B
3 12-sep-2019 A
4 1-jan-2019 B
5 5-feb-2019 B
What i'm wondering to do is to verify if there is an item sent in the month indicated in my date (09 september) and the two previous months (august and july) using a proc sql and without adding new variables.
the result_table expected is like this :
Month Year Number of items
9 2019 2
8 2019 0
7 2019 1
The biggest problem is how to convert the format of the date in the table like my macro_variable date.
Here's one method.
I used cutoff_date instead of date, because it helps differentiate the dates more easily.
Use INTNX() to do date calculations. In this case, I set the cutoff to be the end of cutoff_month and the start of two months prior. You may need to define that a bit more clearly to meet your needs but this works.
%let cutoff_date=201909;
proc sql;
create table want as
select month(sending_date) as Month, count(*) as num
from have
where sending_date between intnx('month', input("&cutoff_date.", yymmn6.), 0, 'e') and intnx('month', input("&cutoff_date.", yymmn6.), -2, 'b')
group by calculated Month;
quit;

Oracle Sql How to calculate if a birthdate is in 2 months from SYSDATE

I need help making a query that will return results from a table for where all people are currently 25 and whose birthdate is in 2 months (anytime of the month).
My current query is:
SELECT * from account_info
where to_char(SYSDATE,'YYYY')-to_char(birth_date,'YYYY') = 25
AND Months_between(TO_DATE(SYSDATE),TO_DATE(birth_date)) = 2
The format of birth_date in the database is 11-NOV-87
This isn't returning the proper months I need, because this is also calculating the years and it comes out to be 297 months. I was previously using:
AND abs(to_char(SYSDATE,'MM')-to_char(birth_date,'MM')) = 2`
which showed the proper result but I'm not sure if this will work if the current SYSDATE = December and the birth_date = February.
How do I return only the people that are currently 25 and where their birthdate is 2 months from the SYSDATE using the proper date calculations?
Try:
WHERE months_between(trunc(sysdate,'mm'),trunc(birth_date,'mm'))=310
EDIT
Corrected month count from 298 to 310 (it was calculating 24 turning 25 rather than 25 turning 26).
SOLUTION:
SELECT * from account_info
WHERE to_char(SYSDATE,'YYYY')-to_char(birth_date,'YYYY') = 25
AND to_char(add_months(TRUNC(SYSDATE), 2),'MM') = to_char(birth_date,'MM');
The add_months will add 2 to the current sysdate month and compare that value to the birth_date.

Get monthly report in sql

I got in my data base colums logs, data, type
How to get logs from year from now with month distinction
f.ex:
row
logs = 'error...'
data = 2012-11-05 11:24:08
type = 1
....
And I want get them in that view
month logs-count type
January 100 1
January 100 2
February 160 1
February 120 2
....
For mysql:
select monthname(data) as month, count(*) as logs-count, type
from table
group by month, type
You need to group by both date and type to get multiple rows per month.
try this one:
select datename(month, data) as month count(logs)as logscount, type from table
try this,If these columns are in same table
select monthname(data) as month,count(logs)as logs-count,type from table
group by month

Oracle week calculation issue

I am using Oracle's to_char() function to convert a date to a week number (1-53):
select pat_id,
pat_enc_csn_id,
contact_date,
to_char(contact_date,'ww') week,
...
the 'ww' switch gives me these values for dates in January of this year:
Date Week
1-Jan-10 1
2-Jan-10 1
3-Jan-10 1
4-Jan-10 1
5-Jan-10 1
6-Jan-10 1
7-Jan-10 1
8-Jan-10 2
9-Jan-10 2
10-Jan-10 2
11-Jan-10 2
12-Jan-10 2
a quick look at the calendar indicates that these values should be:
Date Week
1-Jan-10 1
2-Jan-10 1
3-Jan-10 2
4-Jan-10 2
5-Jan-10 2
6-Jan-10 2
7-Jan-10 2
8-Jan-10 2
9-Jan-10 2
10-Jan-10 3
11-Jan-10 3
12-Jan-10 3
if I use the 'iw' switch instead of 'ww', the outcome is less desirable:
Date Week
1-Jan-10 53
2-Jan-10 53
3-Jan-10 53
4-Jan-10 1
5-Jan-10 1
6-Jan-10 1
7-Jan-10 1
8-Jan-10 1
9-Jan-10 1
10-Jan-10 1
11-Jan-10 2
12-Jan-10 2
Is there another Oracle function that will calculate weeks as I would expect or do I need to write my own?
EDIT
I'm trying to match the logic used by Crystal Reports. Each full week starts on a Sunday; the first week of the year starts on whichever day is represented by January 1st (e.g. in 2010, January 1st is a Friday).
When using IW, Oracle follows the ISO 8601 standard regarding week numbers (see http://en.wikipedia.org/wiki/ISO_8601). That is the same standard than the one we generally use in Europe here.
Your problem is also mentioned on the Oracle forum: http://forums.oracle.com/forums/thread.jspa?threadID=947291 and http://forums.oracle.com/forums/message.jspa?messageID=3318715#3318715. Maybe you can find a solution there.
I know this is old, but still a common question.
This should give you the correct results in the smallest amount of effort:
select pat_id,
pat_enc_csn_id,
contact_date,
to_char(contact_date + 1,'IW') week,
...
Since it looks like you are using your own special definition of the week number you'll need to write your own function.
It might be helpful that NLS_TERRITORY affects the day with which a week starts as used by the D Format Model
see also:
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/sql_elements004.htm#SQLRF00210
and
http://www.adp-gmbh.ch/ora/sql/to_char.html
Based on this question, How do I calculate the week number given a date?, I wrote the following Oracle logic:
CASE
--if [date field]'s day-of-week (e.g. Monday) is earlier than 1/1/YYYY's day-of-week
WHEN to_char(to_date('01/01/' || to_char([date field],'YYYY'),'mm/dd/yyyy'), 'D') - to_char([date field], 'D') > 1 THEN
--adjust the week
trunc(to_char([date field], 'DDD') / 7) + 1 + 1 --'+ 1 + 1' used for clarity
ELSE trunc(to_char([date field], 'DDD') / 7) + 1
END calendar_week