Avatar of al4629740
al4629740
Flag for United States of America asked on

convert string to date format

Here is my query to get a list of all groups for all months.  I have also attached the output in a file.  As you can see from the data results, the Month column presents a challenge.  I would like to find all records between the months of December to February. How could I do so in SQL?  I imagine that I will have to convert the Month column to a date type somehow.

select P.Agency, M.Month, F.Description,F.GrossAmount,F.NetAmount,F.InKindAmount from tblOrgProfile P 
Join tblorgMonthlyReport M on M.AgencyID = P.AgencyID
Join tblOrgMonthlyFunds F on F.MonthlyID = M.MonthlyID
Where Agency <> 'Administrator'
order by Agency

Open in new window

output.xlsx
Microsoft SQL Server

Avatar of undefined
Last Comment
al4629740

8/22/2022 - Mon
HainKurt

maybe this:

DATEPART(month, some_Date) in (12,1,2)
Scott Pletcher

Honestly, I think it'd be more effort to force it to a date format than to just process it as is:

WHERE
    [Month] LIKE '%Dec%' OR
    [Month] LIKE '%Jan%' OR
    [Month] LIKE '%Feb%'
HainKurt

what values do you have? string like 'December', 'January', February'? Then:

month in ('December', 'January', February')
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
al4629740

ASKER
The problem is that from a vb6 form I am having a user choose from a date range and then I would retrieve it in SQL.

I would like to specify a date range which means the code would need to be more scalable for such a purpose.

Any other ideas?
al4629740

ASKER
The value would be a beginning month and an ending month.

For example

December to February
HainKurt

what are you passing to query, and what values do you have in your table?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
al4629740

ASKER
I would be passing only two variables: December, February

The attached file shows what I have in my table related to months.
al4629740

ASKER
Those are the typical variables
HainKurt

... where
DATEPART (month, M.Month) between GetMonth(@param1)+12 and GetMonth(@param2)+12

where GetMonth is a function like

create function GetMonth(@s varchar) returns int32 as 
begin
	return (case @s 
		when 'January' then 1
		...
		when 'December' then 12
	end
	);
end;

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
HainKurt

^^ is almost correct, I will find a way to check interval...
ASKER CERTIFIED SOLUTION
HainKurt

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
al4629740

ASKER
I think the parenthesis are off.  I corrected here:

select P.Agency, M.Month, F.Description,F.GrossAmount,F.NetAmount,F.InKindAmount from tblOrgProfile P 
Join tblorgMonthlyReport M on M.AgencyID = P.AgencyID
Join tblOrgMonthlyFunds F on F.MonthlyID = M.MonthlyID
Where Agency <> 'Administrator' And
((DATEPART (month, M.Month) >= 7) and (DATEPART (month, M.Month) <= 12)) or
((DATEPART (month, M.Month) <= 7) and (DATEPART (month, M.Month) > 12))
order by Agency

Open in new window


The problem is that I get an error:

Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
al4629740

ASKER
Nevermind, I think I found the problem
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.