Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
Declare @year Varchar(100)=2018
Declare @month Varchar(100)=3

SELECT Shrimp_Name,Grades,Japan,EU,US  
 FROM (  
 SELECT MSN.Shrimp_Name,TMG.ID,TMG.Grades,TMR.Region_Name,TSP.Price from   
 Tbl_Shrimp_Price TSP  
 inner join Tbl_Mast_Shrimp_Region TMR On  TMR.ID=TSP.Region  
 inner join Tbl_Mast_Shrimp_Grades TMG On TMG.ID=TSP.Grade  
 inner join Tbl_Mast_Shrimp_Name MSN On MSN.ID=TSP.ShrimpType  
 WHERE TSP.ShrimpType=1 and TSP.PeriodType=@PeriodType AND 
 --PostMonth =cast('01-'+@year1 as Varchar(20))
 year(PostMonth) = @year AND  
 MONTH(PostMonth)=@month  
 ) as s  
 PIVOT  
 (  
 SUM(Price)  
 FOR [Region_Name] IN (Japan, EU, US)  
 )AS pvt  
 ORDER BY Shrimp_Name,Grades  

SQL
SELECT Shrimp_Name,Grades,Japan,EU,US
FROM (
SELECT MSN.Shrimp_Name,TMG.ID,TMG.Grades,TMR.Region_Name,TSP.Price from
Tbl_Shrimp_Price TSP
inner join Tbl_Mast_Shrimp_Region TMR On  TMR.ID=TSP.Region
inner join Tbl_Mast_Shrimp_Grades TMG On TMG.ID=TSP.Grade
inner join Tbl_Mast_Shrimp_Name MSN On MSN.ID=TSP.ShrimpType
WHERE TSP.ShrimpType=2  and TSP.PeriodType=@PeriodType
AND --PostMonth = cast('01-'+@year1 as Varchar(20))
year(PostMonth) = @year AND
MONTH(PostMonth)=@month
) as s
PIVOT
(
SUM(Price)
FOR [Region_Name] IN (Japan, EU, US)
)AS pvt
ORDER BY Shrimp_Name,Grades


What I have tried:

above is my Query i want to search Data from month and Year but when i search Data From this Query at the Location of @month i put Day means(@month=1) then this Query Select Data so how to Search Data From Month and Year Please Help Me-

PostMonth is my Column Name ans Save Data Format Below-
01-03-2018
Posted
Updated 2-Apr-18 4:05am
v3
Comments
MadMyche 2-Apr-18 9:44am    
When posting DB related questions, it is recommended to add in what type of RDBMS is being used as well as the table schema.
When dealing with dates and/or time, it is often easiest to work with the DATE datatype and then using INT for the portions (year, month, day). Using a text (VARCHAR) type and trying to do math within that often ends up doing something you didnt intend

1 solution

If I understand correctly the date is stored in a a varchar column and taken that this is SQL Server. If that is the case, one easy way is to convert it to date and then do the comparison. For example
SQL
...
AND DATEPART(year, CONVERT(date, PostMonth, 105)) = @year 
AND DATEPART(month, CONVERT(date, PostMonth, 105)) = @month
...

However, the actual correction would be to store the date inside a date column instead. This would make it a lot easier to do the comparison and wuold leave out all unnecessary conversions. The same applies to the variables since it seems that you have used varchar for them even though they are actually numbers.
 
Share this answer
 
Comments
Member 12183079 3-Apr-18 0:56am    
thanks
Wendelius 3-Apr-18 14:22pm    
You're welcome.

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



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