Compare between two weeks of the year sqlite - sql

I want to filter values from a table, between two weeks, like this:
select * from SalesWeekly
where SalesWeek BETWEEN '50' and '02'
Problem is, i have no idea how to specify week 50 is from year 2019,
and week 02 is from year 2020.

Assuming that you are storing the year in your table, say in table SalesYear, you could concatenate it with the week number and do string comparisons:
select *
from SalesWeekly
where SalesYear || '-' || SalesWeek BETWEEN '2019-50' and '2020-02'
For this to work, SalesWeek must be a 2-characters long string, left padded with 0 (so the 1st week should be '01', not '1').

Related

Combine with month and year column kept in different columns on Oracle

I keep the month and year information in different columns as numbers.
I want to go back 12 months on sysdate using these columns.
The table I used below as an example ,
Since we are in the 5th month now, I want to get the data up to the 6th month of last year.
versiyon table :
So as a result of the query ,
the following result should return.
First of all, I want to query by combining the year and month columns and going back one year from the current month as a date.
Convert the values to strings and concatenate and then use ADD_MONTHS(SYSDATE, -12) to get last year's date (which will get the correct date regardless of whether it is a leap year or not):
SELECT *
FROM versiyon
WHERE TO_CHAR(year, 'fm0000') || TO_CHAR(month, 'fm00')
>= TO_CHAR(ADD_MONTHS(SYSDATE, -12), 'YYYYMM')
db<>fiddle here
select *
from versiyon
where lpad(year, 4, '0') || lpad(month, 2, '0') >= to_number(to_char(sysdate - 365, 'yyyymm' ))
Here is a demo
As MT0 say it will not work for leap years... so you can check if it is leap year or not with case when then end clause and using mod(year, 4) to check. In this demo I have created a situation as it is 2020, to be exact like it is 29th of February 2020. where you can see what I am suggesting in action:
DEMO2
You can convert your year and month into an integer form of YYYYMM and compare:
SELECT *
FROM versiyon_table
WHERE (versiyon_table.year * 100) + versiyon_table.month > (EXTRACT(YEAR FROM SYSDATE) * 100) + EXTRACT(MONTH FROM SYSDATE)

calculate sum of two values on the base of field name

Add values between two fields on the base of month names I have fields in the table from January to December and I want to calculate only the sum between May to September.
code I have tried but it's not working for me
SELECT DATENAME(month,GETDATE()) 'Month Name'
(
select top 1 Column_name='May'
from Information_schema.columns
where Table_name like 'session2021'
)
I have fields in the table from January to December and I want to calculate only the sum between May to September.
I would expect:
select May + June + July + August + September as my_sum
from the_table;
You should fix your data model so you have one row per month, rather than putting the months in separate columns.

Oracle concatenate two int columns, make them date and select a date from them

I have column REF_YEAR and column REF_MONTH. Both are int columns.
I want to select a range of dates between these two ranges.
for example
select *
from table
where date between '05/2020' and '12/2021'
Can i do something like that but with similar results for int columns?
If I understood you correctly, you'd
select *
from your_table
where to_date(ref_year || lpad(ref_month, 2, '0'), 'yyyymm')
between date '2020-05-01' and date '2021-12-01'
i.e. you'd have to "convert" (to_date) ref_year concatenated with ref_month (left-padded with zero up to 2 characters in length because May 2021 isn't 20215 but 202105), using appropriate format mask (yyyymm) and compare it to valid date values, e.g. date literals I used.
You should be using a single proper date column here, rather than separate columns for the month and year. That being said, integer month and year columns is still workable. Using your example:
SELECT *
FROM yourTable
WHERE REF_YEAR = 2020 AND REF_MONTH >= 5 OR REF_YEAR = 2021;
Note that the above logic would include the entire 2021 calendar year, including the entire month of December. This is what I imagine you want; if you instead want to exclude December, we would have to change the logic slightly. That being said, it would be much better to maintain a single date column REF_DATE:
SELECT *
FROM yourTable
WHERE REF_DATE >= date '2020-05-01' AND REF_DATE < date '2022-01-01''

Combine and convert nvarchar month field + float year field to a single date field

I have a table in SQL Server 2012 with a month column stored as nvarchar(255):
"January", "February", "March"
And another column in this table with year stored separately as float
"2012","2013,"2014".
I do not have a day column so I want to create a combined month date column with the day starting as 1.
So for month and year fields January 2012. I want to show '2012-01-01'
How can I do such and add that into my current table?
I want to find the maximum row for a record in my table for each employee.
so for an [employee #], [month],[year]. what is latest record so for example below:
1. 102, Jan, 2019
2. 102, feb, 2019
I want to only see the second record which is the latest.
SQL Server has pretty flexible conversion to date. So, just convert the columns to a date:
select convert(date, month + ' ' + year)
You can get the maximum as:
select empid, max(convert(date, month + ' ' + year))
from t
group by empid;
If you really like, you can change the format for output purposes. I would advise you to stick with a date, though.
Note: This assumes that your internationalization settings are set to English -- which seems reasonable if you are storing month names in English.
Fix your design! The way you store data makes it really inefficient to interpret it. Here, I think the simplest option is datefromparts() and a 12-branches case expression.
Assuming that the (float) year is stored in column col_year and the (string) month is in col_month:
select t.*,
datefromparts(
cast(col_year as int),
case col_month
when 'January' then 1
when 'February' then 2
...
when 'December' then 12
end,
1
) as date_col
from mytable t

How to get year, month and day from seconds in PostgreSql?

I'm trying to create three columns based on date in seconds format.
My user.updated_at = 1521533490
I would like to get year, month and day separately and put these formatted values to columns for example:
year -> 2018, month -> 11, day -> 23
Does someone know how can I do that in pgSQL?
I would like to get year, month and day separately and put these formated values to columns
Don't do that.
Use a single column of type date or timestamp, depending on your application. Not every combination of your three columns will be a valid date. But every value in a single column of type date will be a valid date.
If you need the parts of a date separately, use PostgreSQL's date/time functions.
Try this approche to get differents arguments, then you can do whatever you want:
SELECT to_timestamp(1521533490); //2018-03-20T08:11:30.000Z
SELECT to_char(to_timestamp(1521533490), 'HH'); // 08 Hour
SELECT to_char(to_timestamp(1521533490), 'MI'); // 11 Minutes
SELECT to_char(to_timestamp(1521533490), 'SS'); // 30 Seconds
SELECT to_char(to_timestamp(1521533490), 'DD'); // 20 Day
SELECT to_char(to_timestamp(1521533490), 'Mon'); // MAR Month
SELECT to_char(to_timestamp(1521533490), 'YYYY'); // 2018 Year
Use the EXTRACT function.
SELECT to_timestamp(updated_at) "Date",
EXTRACT(YEAR FROM (to_timestamp(updated_at))) "Year",
EXTRACT(MONTH FROM (to_timestamp(updated_at))) "Month",
EXTRACT(DAY FROM (to_timestamp(updated_at))) "Day"
FROM users
Output
Date Year Month Day
2018-03-20T08:11:30Z 2018 3 20
SQL Fiddle: http://sqlfiddle.com/#!15/afe0e/15/0
More information on the EXTRACT function.