SQL SERVER get datetime from a custom formatted string - sql-server

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 +'%'

Related

MonthName-Year as SSRS parameter

I have dimTime table below:
TheYear Fiscalyear TheMonthName TheMonth
2022 2022 - 2023 November 11
2022 2022 - 2023 October 10
2022 2022 - 2023 September 9
2023 2022 - 2023 February 2
2023 2022 - 2023 January 1
How to implement the MonthName and Year as SSRS parameter and have this in the main dataset? Like below:
Select Month-Year: January-2023
Dataset: Select
from Table
where date = selected MonthYear from the parameter
This will also goes to same report but the selection is Fiscal year:
*Select Fiscal Year: 2022-2023
Dataset: Select
from Table
where date = selected FiscalYear from the parameter
Im trying to convert it to date but dont know where to start.

Format Date, sort as calendar and use as SSRS parameter

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

How to generate automatic Rows

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

Capture date range of different dateYear , DateMonth columns

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)

SQL Server : pivot date to month quarter and year subtotal format

I have a table that contains datefield and product. I want the below pivot query to show the month total, quarter total and year total.
Year Month A B C
-----------------
2014 Jan
2014 Feb
2014 Mar
2014 Q1
2014 Apr
2014 May
2014 Jun
2014 Q2
2014 Jul
2014 Aug
2014 Sep
2014 Q3
2014 Oct
2014 Nov
2014 Dec
2014 Q4
2014 Total
2015 Jan
2015 Feb
2015 Mar
2015 Q1
Refer this Simple-Way-To-Use-Pivot-In-SQL-Query from CodeProject, the code given in the example is more similar to your requirement.
I think this is what you are talking about.
SELECT 'Count' AS Month,
[Jan], [Feb]
FROM
(SELECT year, month
FROM Product) AS SourceTable
PIVOT
(
count(year)
FOR Month IN ([Jan],[Feb])
) AS PivotTable;
Out Put
Month Jan Feb
Count 2 2
This is what you are expecting.
Count Month quarter Year
Count 24 8 2
But We cannot categorise month, year unless they are designated as months, quarter etc. If there is any flag which denotes that Jan and Feb are months then we can categorise them based on Months.

Resources