Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a table which contains Year and Month as columns like below:-
Id Year Month Value
1 2012 12 100
2 2013 1 200
3 2013 2 300
4 2013 12 200
5 2012 11 200
I want to create a query which gives me values with (year >= 2012 and month >= 12) and also (year =< 2013 and month =< 12) i.e. It should give me Id 1, 2, 3, 4.
EDIT
This is just an example to demonstrate the behavior.
The months and Year might vary. So please create an answer that suits the condition for any year and any month that are passed.
How can I create a query with the following condition?
Regards
Vishal
This should get you what you want
SELECT id
FROM YourTable
WHERE (Year = '2013' OR (Year = '2012' AND Month = '12'))
I hope I've understood your conditions now ("i want 2012 12 month abd 2013 1 to 12 month data"):
SELECT Id FROM TableName WHERE (Year = 2012 AND Month = 12) OR (Year = 2013) ORDER BY Id
This is assuming there can't be a month > 12 or < 1 ;-)
declare #YearFrom int, #YearTo int, #MonthFrom int, #MonthTo int
set #YearFrom = 2012
set #YearTo = 2013
set #MonthFrom = 12
set #MonthTo = 12
select ID, YEAR, MONTH, Value
from [yourtable]
WHERE (year = #yearfrom AND month >= #monthfrom or year > #yearfrom)
AND (year = #yearto AND month <= #monthto or year < #yearto)
I think the easiest way is to convert the columns to a bigger integer (or string). Assuming the values are integers:
select id
from table
where year*100+month between 201212 and 201312;
edit based on comment
For a given #yearIN
select * from table
where (year = #yearIN and month != 12) or (year = (#yearIN-1) and month = 12)
I think this is what you want
select * from table
where (year = 2013 and month != 12) or (year = 2012 and month = 12)
I think this makes the logic of what you are looking for clearer -- I expect you don't want the last month of 2011
Related
I have a query returns this result :
EmpNo
EmpName
Years
Months
Days
111
Ahmed
0
14
39
In this case, I have 14 months, so it should add one year in the years field and remains 2
months in months field, and I have 39 days, so it should add one month in the months field and remains 9 days in days field
how I could re-calculate the years, months, and days to get query result like this :
EmpNo
EmpName
Years
Months
Days
111
Ahmed
1
2
9
Try this, but it is more correct to use the date field:
UPDATE your_table
SET
days = mod(days,30),
months = mod(months,12) + trunc(days/30),
years = years + trunc(months/12);
COMMIT;
It seems strange to say that all months have 30 days. But if you are assuming that you need to recalculate all columns. In this case, revert to days and recalculate everything:
select t.*,
mod(x.total_days, 30) as days,
floor(mod(x.total_days, 360) / 30) as months,
floor(mod(x.total_days / 360)) as years
from t cross join lateral
(select t.days + t.months * 30 + t.years * 360 as total_days
from dual
) x;
Note that this handles the case where you have 0/11/39.
If you want this in an update, you should be able to use a subquery:
update (select t.*, t.days + t.months * 30 + t.years * 360 as total_days
from t
)
set days = total_days, 30),
months = floor(mod(total_days, 360) / 30),
years = floor(mod(x.total_days / 360));
Assuming that a month has 30 days and (hence) that a year has 360 days seems problematic, but that is another issue entirely.
Try this. However, I think it should be 1 year, 3 months and 9 days though, Kindly let me know otherwise.
UPDATE your_table
SET
days = days - 30,
months = months + 1
WHERE
days > 30;
COMMIT;
UPDATE your_table
SET
months = months - 12,
years = years + 1
WHERE
months > 12;
COMMIT;
Output:
Empno Empname Years Months Days
-------------------------------
111 Ahmed 1 3 9
friends, I know that my question is very basic but I am stuck with it as I am naive to SQL,So, apology for that.
I have table named TRANS_MONT and stores data like below
Year month amount
2017 10 500
2017 11 700
2017 12 400
2018 1 600
2018 2 450
2018 3 600
I am interested to return all those transaction from the year 2017 whose month value is greater than 10 and all those record from the year 2018 whose month value is less than 3
I tried below query, but it does not work
Select * from TRANS_MONT where year in (2017, 2018) and month between 10 and 3
It's quite straight forward:
SELECT *
FROM trans_mont
WHERE (year = 2017 AND month > 10)
OR (year = 2018 AND month < 3);
The simplest would be something like this:
select * from Trans_Mont
where (year=2017 and Month>10)
or (year=2018 and Month<3)
or
select * from Trans_Mont
where (year * 100 + Month) BETWEEN 201710 and 201803
SQL doesn't pick up on the modular arithmetic of months if it is only stored as an integer.
Given it's really two logical statements, separate it with an OR to capture both situations in both years. Even if the month range was to work, it could technically also pick up the 12th month in 2018 for example.
SELECT * FROM TRANS_MONT
WHERE (Year = 2017 AND month > 10) OR (Year = 2018 AND month < 3)
Try below
Select * from TRANS_MONT where (year=2017 and month>=10) OR (year=2018 and month<=3)
You didn't state your DBMS, but with standard SQL you can do the following
select *
from trans_mont
where (year, month) between (2017,11) and (2018,2);
The between operator is inclusive of the edges, that's why the month on the lower end is 11 (not 10) and the month on the upper end is 2
Online example: https://rextester.com/VZPQ64850
This question already has answers here:
How to get 3 letter abbreviation for month in SQL
(3 answers)
Closed 5 years ago.
I am currently dealing with a table that looks like this:
PersonID Month
1 Dec
2 Jan
3 Oct
4 Oct
5 Jan
6 Feb
I want to select rows where the month is equal to today's month. So for example for this current month i want all the rows that are equal to October but obviously this appears as 'Oct'. Is there anyway i can achieve this?
Desired output
PersonID Month
3 Oct
4 Oct
I thought maybe something along the lines of
SELECT *,
CASE WHEN Month = 'Oct'
THEN '01-10-17'
ELSE 'N/A' END AS [Date]
FROM Person
WHERE Month([Date]) = Month(getdate())
But im not sure this logic is that solid.
SQL - http://sqlfiddle.com/#!9/a3f33/1
You could use:
SELECT *
FROM Person
WHERE FORMAT(GETDATE(), 'MMM') = Month;
Rextester Demo
Remarks:
SQL Server 2012 and above
language for session should be English: SET LANGUAGE English;
Another option if not 2012+.
Select left(datename(MONTH,getdate()),3)
Returns
Oct
So for your actual query
Select *
From Person
Where Month = left(datename(MONTH,getdate()),3)
Here is one more way to do it with CONVERT:
SELECT *
FROM Person
WHERE Month = Convert(char(3), GetDate(), 0)
I am trying to write a SQL query to get data for last year. I don't have a date column in my table, but have Year and WeekNo. Suppose this is WeekNo 26 in Year 2017, I need to get the data from WeekNo 26 Year 2016 to WeekNo 26 Year 2017.
Columns in my table are:
Year WeekNo YearWeek(Eg 2016 26) DataX
Thanks for the help in advance!
If this is SQL Server. Give this a try. This will get you the Week #, Current Year and Previous Year to put into your query.
Select DatePart(Week,GetDate()) as WeekNo
, Year(GetDate()) as CurrentYear
, Year(DateAdd(Year,-1,GetDate())) as PreviousYear
Guessing this might be what you are looking for, assuming SQL Server:
Select T1.Year
,T1.WeekNo
,T1.DataX
From MyTable T1
Where (T1.Year = Year(GetDate())
AND T1.WeekNo <= DatePart(Week,GetDate())
)
OR (T1.Year = Year(GetDate()) -1
AND T1.WeekNo >= DatePart(Week,GetDate())
)
This is inclusive of the same week, last year, is your want this to be exclusive of that week, change the ">=" to ">"
I have a mysql table with year (YEAR(4)) and month (TINYINT) columns. What is the best way to select all records in a range, e.g. 2009-10 .. 2010-02 ?
Here is a generalized solution:
Select...
From ...
Where ( Year = <StartYear> And Month >= <StartMonth> )
Or ( Year > <StartYear> And Year < <EndYear> )
Or ( Year = <EndYear> And Month <= <EndMonth> )
Use:
WHERE (year = 2009 AND month >= 10)
OR (year = 2010 AND month <= 2)
...or, using UNION ALL:
SELECT t.*
FROM TABLE t
WHERE year = 2009 AND month >= 10
UNION ALL
SELECT t.*
FROM TABLE t
WHERE year = 2010 AND month <= 2
UNION ALL is faster than UNION, but won't remove duplicates.
You may want to consider creating another column called year_month which stores the date in YYYYMM format, such as 201002 for February 2010. Make sure to create an index on it, and you may also create two triggers to automatically update the column ON INSERT and ON UPDATE.
Then you would be able to use:
WHERE year_month BETWEEN 200710 AND 201002
This would use the index on year_month.
The accepted answer does not work for the case when StartYear and EndYear is the same. E.g. 2017-01 to 2017-08. A revised psuedocode that covers this case would be
((year = StartYear and month >= StartMonth) OR (year > StartYear)) AND ((year = EndYear and month <= EndMonth) OR (year < EndYear))