Solved

convert string to date format

Posted on 2014-11-24
13
112 Views
Last Modified: 2014-11-25
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
Comment
Question by:al4629740
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
13 Comments
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 40462750
maybe this:

DATEPART(month, some_Date) in (12,1,2)
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40462790
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
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 40462821
what values do you have? string like 'December', 'January', February'? Then:

month in ('December', 'January', February')
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:al4629740
ID: 40462845
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
 

Author Comment

by:al4629740
ID: 40462859
The value would be a beginning month and an ending month.

For example

December to February
0
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 40462878
what are you passing to query, and what values do you have in your table?
0
 

Author Comment

by:al4629740
ID: 40462891
I would be passing only two variables: December, February

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

Author Comment

by:al4629740
ID: 40462892
Those are the typical variables
0
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 40462919
... 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
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 40462925
^^ is almost correct, I will find a way to check interval...
0
 
LVL 53

Accepted Solution

by:
Huseyin KAHRAMAN earned 500 total points
ID: 40462941
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
 

Author Comment

by:al4629740
ID: 40463199
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
 

Author Comment

by:al4629740
ID: 40463203
Nevermind, I think I found the problem
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Query 9 29
What does "Between" mean? 6 48
T-SQL: need to reset a declared variable 4 33
Sql group by orderdate and ordertotal 3 12
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question