Display current month values based upon abbreviation (Oct) SQL Server [duplicate] - sql

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)

Related

How to Return records between two values in SQL

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

Using Least with DatePart?

I'm running a query during the month of august, but I want to convert it to a date_part function.
Here's the original query.
SELECT avg(CASE WHEN date = LEAST(current_date-1,'8/31/14') THEN bo ELSE NULL END) end_bo
From <table>
What I'm trying to do, is plug LEAST into the one below.
SELECT avg(CASE WHEN date_part('month', date) = 8 and date_part('year', date) = 2014 THEN bo ELSE NULL END) end_bo
From <table>
The problem is, I don't see where I can plug it in.
The first one looks to see which of those dates are earlier, currentdate or Aug 31 2014, if it is equal to date it returns bo.
While the second one tests to see if the current month is aug 2014 and returns bo.
To apply the same logic to the second query, it might look like this:
SELECT avg(CASE
WHEN least(date_part('month', currentdate), 8) = date_part('month', date)
and date_part('year', date) = 2014
THEN bo
ELSE NULL
END) end_bo
This would look at the current month, find if it was lower than 8, compare it with date month, if equal return bo.
LEAST simply returns the lowest number in a list. So least(9,8) (sept, aug) would return 8.

SQL Month Year datatypes

Its really frustrating whenever I have to work with date/ datetime datatypes and SQL doesn't provide a good and easy way to work with it.
Currently I have this table RecordsDaily with a column Date with datatype date. I want to convert the Date column to month and take only distinct month values AND then sort according to month. Below is the query.
select distinct(CAST(DATEPART(year,Date) as varchar(10)) + ' ' + datename(MONTH,Date)) Month
from P98.dbo.RecordsDaily
where Date >= '2013/12/1'
order by Month
Obviously since I have made it varchar it doesn't consider it as date datatype and sorts it alphabetically as below.
Month
2013 December
2014 April
2014 February
2014 January
2014 March
2014 May
Any help to make it sort as per calendar.
UPDATE
Please note the output should be with Month and Year
Please try:
select Month from(
select distinct(CAST(DATEPART(year,Date) as varchar(10)) + ' ' + datename(MONTH,Date)) Month
,year(Date) yr, month(Date) mn
from P98.dbo.RecordsDaily
where Date >= '2013/12/1'
)x
order by yr, mn
try this !
select * from table order by cast(('01'+id) as datetime)asc
-- id is your column_name

Query related to month and year [closed]

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

In SQL how to return records matching date and month only (ignore year)

Using SQL, I want to return all records where the date is between 1st March and 31st June (for example), but the records should cover all years. Is there a simple way I can achieve this?
Here is what you would do if you are using PL/SQL or oracle SQL+
SELECT * FROM table
WHERE TO_CHAR(MONTH_COLUMN,'MM/DD') = '06/21'
this will give you all the rows that have a date of June 21 regardless of the year.
For SQL Server use:
select *
from table
where month(dtgCol) between 3 and 6
Use the date functions to get the Month and the Day of Month from the date field and use in the where clause.
Depending on your DB, the function names may vary. But it will be in general like
SELECT * FROM table
WHERE Month(dateField) = 6
AND (DayOfMonth(dateField) >= 1 AND DayOfMonth(dateField) <= 30)
in SQL Server:
SELECT * FROM table
WHERE Month(dateField) = 6
AND (Day(dateField) >= 1 AND Day(dateField) <= 30)
Try this, definitely work
SELECT *
FROM Table
WHERE Month(DateColumn) IN (3, 4, 5, 6)
For SQL Server I'll use following.
eg:between 1st March and 31st June
select * from (
select *,DATEFROMPARTS(2011,MONTH(CreateDate),DAY(CreateDate)) as dt from tblAction
) as x
where x.dt between
DATEFROMPARTS(2011,3,1) and
DATEFROMPARTS(2011,6,31)
See if it helps..:)