Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 123
  • Last Modified:

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
0
al4629740
Asked:
al4629740
  • 6
  • 6
1 Solution
 
HainKurtSr. System AnalystCommented:
maybe this:

DATEPART(month, some_Date) in (12,1,2)
0
 
Scott PletcherSenior DBACommented:
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%'
0
 
HainKurtSr. System AnalystCommented:
what values do you have? string like 'December', 'January', February'? Then:

month in ('December', 'January', February')
0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
al4629740Author Commented:
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?
0
 
al4629740Author Commented:
The value would be a beginning month and an ending month.

For example

December to February
0
 
HainKurtSr. System AnalystCommented:
what are you passing to query, and what values do you have in your table?
0
 
al4629740Author Commented:
I would be passing only two variables: December, February

The attached file shows what I have in my table related to months.
0
 
al4629740Author Commented:
Those are the typical variables
0
 
HainKurtSr. System AnalystCommented:
... 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

0
 
HainKurtSr. System AnalystCommented:
^^ is almost correct, I will find a way to check interval...
0
 
HainKurtSr. System AnalystCommented:
maybe this (fix for above query where I used +12):

... where
(
(DATEPART (month, M.Month) >= GetMonth(@param1) and DATEPART (month, M.Month)<=GetMonth(@param2)) or
(DATEPART (month, M.Month) <= GetMonth(@param1) and DATEPART (month, M.Month)>=GetMonth(@param2))
)
0
 
al4629740Author Commented:
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.
0
 
al4629740Author Commented:
Nevermind, I think I found the problem
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now