I have a table that captures monthly data using a dateYear and dateMonth to record date. I;m having trouble capturing data between two different years.
For example : between dateYear 2015 and dateMonth 09 through dateMonth 02 and dateYear 2016
In english , between September 2015 and Feb 2016
whats the best way to go about this.. Ive tried grouping through the where clause but the months overlap..
is there a date function or concatenation method i should use?>
SELECT
prodDays
,hours
FROM Table
WHERE 1=1
AND date_year BETWEEN 2015 AND 2016
Sounds like you are looking for this where clause:
where (date_year = 2015 and date_month >= 9) or (date_year = 2016 and date_month <= 2)
Related
I have a DateTime field that I want to summarise and format to YYYY mmm, then use as an SSRS parameter to filter my query.
Q1. When I use the format option the dates I get back are sorted as text e.g.
2022 Apr
2022 Feb
2022 Jan
2022 Mar
Q2. How do I sort as per the calendar e.g.
2022 Jan
2022 Feb
2022 Mar
2022 Apr
Q3. How do I use the values of this parameter in my WHERE clause to filter my dates? So if someone chooses "2022 Mar" I only get data where the DateTime field contains March 2022.
try_convert(date,...) will assume the 1st of the month if no day is given
Example
Declare #YourTable Table ([SomeCol] varchar(50))
Insert Into #YourTable Values
('2022 Apr')
,('2022 Feb')
,('2022 Jan')
,('2022 Mar')
Select *
From #YourTable
Order By try_convert(Date,SomeCol)
Results
SomeCol
2022 Jan
2022 Feb
2022 Mar
2022 Apr
You can sort it out by just using 'ORDER BY'
EX:
CREATE TABLE #TEMP_DATES
(Month_Year Date)
INSERT INTO #TEMP_DATES
SELECT
'2022 APR',
'2022 FEB',
'2022 JAN'
SELECT * FROM #TEMP_DATES
ORDER BY Month_Year
I have a table that contains some DateTime values stored as text, and I want to loop through them and get the DateTime object for each one.
Here are some examples of the data that I need to convert it.
26 Tuesday November 2019 - 09:00 AM
25 Monday November 2019 - 11:20 AM
06 Friday December 2019 - 10:00 AM
22 Sunday December 2019 - 01:05 PM
22 Sunday December 2019 - 04:00 PM
22 Sunday December 2019 - 02:30 PM
23 Monday December 2019 - 11:00 AM
I tried to use the SQL built-in functions (e.g. cast, convert,...) but didn't work.
Remove the name of the weekday and the hyphen and it should work:
WITH cte
AS
(
SELECT DATENAME(weekday,datefromparts(2020,1,dow.dow)) as WeekDay
FROM (values (1),(2),(3),(4),(5),(6),(7) ) dow(dow)
)
SELECT TRY_CAST(REPLACE(REPLACE(t.dateInTxt,'-',''),cte.WeekDay,'') as datetime),t.dateInTxt
FROM mytable t
JOIN cte
ON t.dateInTxt LIKE '%'+cte.WeekDay +'%'
I have Below Sample Table
month year budget_Amt Actual_Amt
feb 2017 25 30
mar 2016 10 5
apr 2016 50 15
I am executing following query
select month,year,budget_amt,Actual_Amt from Table where month in('Feb','Mar','apr')
I need following output
month year budget_Amt Actual_Amt
feb 2017 25 30
mar 2016 10 5
apr 2016 50 15
QuarterYTD 2016 85 50
4th record will generate automatically when I execute query
Based on the data given, you could UNION in grouping like the following. However, I assume that your data is more complex and some additional code would need to be added to the query.
select month,year,budget_amt,Actual_Amt from Table where month in('Feb','Mar','apr')
UNION ALL
select 'QuarterYTD', Year, sum(budget_amt), sum(Actual_Amt) from Table where month in ('Feb','Mar','apr') group by Year
I need date between minimum date and maximum date in the SQL Server in the table containing 3 rows.
ex:01 Jan 1992
02 Jan 1992
03 Jan 1992
i need that 02 Jan 1992
Please note that the query below is on a table with only 3 rows as your question depicts.
;WITH DateCTE AS
(
SELECT MAX(CreatedDate) AS MaxDate, MIN(CreatedDate) AS MinDate
FROM [metadata].[BuildVersion]
)
SELECT CreatedDate
FROM [metadata].[BuildVersion]
WHERE CreatedDate <> (SELECT MaxDate FROM DateCTE)
AND CreatedDate <> ( SELECT MinDate FROM DateCTE)
This should work just fine
select date from table_name where date between '01 Jan 1992' and '03 Jan 1992';
You can use between to extract a value between any 2 values. It will be better if you give the column names and table name, so that it will be easy for other who try to answer your question.
i have a table calculating the installments. in that table i'm saving all the data recording to that. For example if i'm calculating for 60 installments and saving all the data,so it is like 60 months. so now i need to sum up the value of one column for every 12 months. sometimes v start paying the installments from the middle of the year also.
my DB looks like this.the highlighted column must sum up for every 12 months. two images are one table only
suppose i have 30 installments from starting on jun 2012.suppose i started paying installment from jun 2012 then should sum up the installments from jun 2012 to may 2013. v can't use group by year. i must sum up like this ................................................................................
sum jun 2012 to may 2013
sum jun 2013 to may 2014
sum jun 2014 to nov 2014 ( only 6 months left)
You can use ROW_NUMBER to generate a group of 12 months:
WITH Cte AS(
SELECT *,
RN = (ROW_NUMBER() OVER(ORDER BY InstallmentMonth) - 1)/ 12
FROM your_table
)
SELECT
SUM(InteresetPerInstallment)
FROM Cte
GROUP BY RN