Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
i want to select record from database according to month-wise not by date wise means whatever date is there in database, i want to select data on basis of month and year which user will supply, here m using datepart() but it gives me result in two different column.

select DATEPART(MONTH ,GETDATE())  as [MonthNumber], DATEPART(YEAR,GETDATE()) as [Year]


what i want to do is select record from database where MONTH and YEAR is same

how i can do this .....

m using some of the separator /AND space FUNCTION ALSO but m not achieving my result

SQL
DECLARE @MONTHLYRECORD INT
select DATEPART(MONTH ,GETDATE())  as [MonthNumber], DATEPART(YEAR,GETDATE()) as [Year]
SELECT @MONTHLYRECORD


this query produce error
Posted

use this Query for get last month Records

SQL
select *From table where month(date)=04 and year(date)=2014


to get the Month and year

SQL
DECLARE @MONTHLYRECORD INT
declare @year int
select @MONTHLYRECORD= DATEPART(MONTH ,GETDATE())
select @year=DATEPART(YEAR,GETDATE())
SELECT @MONTHLYRECORD,@year
 
Share this answer
 
v2
Comments
Prasad Avunoori 8-May-14 3:05am    
What do you mean by "MONTH and YEAR is same"?<br>
What are the parameters your are passing to it?<br>
Make your question clear.
Maciej Los 8-May-14 3:12am    
Wrong place to post comment. Your comment corresponds to the first soultion ;)
King Fisher 8-May-14 3:16am    
i'm not ;)
Tarun Jaiswal 8-May-14 3:39am    
thanx for your reply but this is not the desired output result
eg. i have a column name DateOfRecord datatype Date.

this DateOfRecord have YYYY-MM-DD but i want to get result output for YYYY-MM how i can achieve this ?

----------------------
DateofRecord | someotherField|
2014-01-15 | data
2014-01-20 | data
2014-01-21 | data
2014-01-22 | data
2014-01-30 | data
2014-02-15 | data
2014-02-15 | data
2014-02-15 | data
2014-03-15 | data
2014-03-15 | data

how i can retrieve data only for 2014-01 [YYYY-MM]?




King Fisher 8-May-14 3:46am    
run this

create table ##table2(id bigint identity(1,1) ,value nvarchar(max) ,dates date)

insert into ##table2(value,dates) values('zzz','2014-02-15')
insert into ##table2(value,dates) values('zzz','2014-03-15')
insert into ##table2(value,dates) values('zzz','2014-02-15')
insert into ##table2(value,dates) values('zzz','2014-04-15')


select *From ##table2 where MONTH(dates)=02 and YEAR(dates)=2014
 
Share this answer
 
Comments
Tarun Jaiswal 8-May-14 3:46am    
thanx it works.
Peter Leow 8-May-14 4:08am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900