Click here to Skip to main content
15,911,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone

I have table called MCALLERS on my database that has column which called CallDate, datatype is datetime stored data as 2016-02-01 17:01:00.000

on my web Form I have dropdownlist which is populating month and year as March-2016.

how to convert datetime into month and year when I search data.I want to search data from database using Month and year as (March-2016). I'm sing C#.

Thanks in advance

What I have tried:

SQL
select BusinessCallYN,CallID,NewNam,CallDate,CalledNo,CallExt,CallDuration,Amount,tmpNewNam from MCALLERS where UserName='"+txtEmail+"' and CallDate>='"+cmddropdownlist1+"' and CallDate<'"+cmddropdownlist1+"' and Amount>0 order by CallDate


Error message : Conversion failed when converting date and/or time from character string.
Posted
Updated 29-Mar-16 1:14am
v2
Comments
FARONO 29-Mar-16 6:58am    
You need to convert CallDate to "Month-Year" format to make it possible to compare. Example:(SELECT right(convert(varchar, getdate(), 106), 8);

You can use month and yesr to search on just those parts

SQL
where month(CallDate) = 3 and year(CallDate) = 2016


However you should use parameterised queries rather than string concatenation as your code is liable to sql injection attacks.
 
Share this answer
 
Comments
Member 12390137 30-Mar-16 8:18am    
Hi F-Es
Hi OriginalGriff
i'm using dropdownlist that populate month and year as (march-2016)

where month(CallDate) = '"+Ddlmonths.selectedValue.ToString()+"' and year(CallDate) = '"+Ddlmonths.selectedValue.ToString()+"'

the above query is giving Error how to search data into database using Dropdownlist value (March-2016) and populate data into Gridview. Please help
F-ES Sitecore 30-Mar-16 9:40am    
You don't need aprostrophes for integer values

where month(CallDate) = "+Ddlmonths.selectedValue.ToString()+" and year(CallDate) = "+Ddlmonths.selectedValue.ToString()+"

If that still throws an error post what the error is.
Hi,

You need to review SQL Server CONVERT() Function[^] to understand the DateTime conversion.

Hope above link will help you.
 
Share this answer
 
For starters, do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
Secondly, try:
SQL
SELECT * FROM MyTable WHERE DATEPART(year, DateColumn) = yearYouWant AND  DATEPART(month, DaetColumm) = monthYouWant

If you want to use your combobox directly, then you need to identify exactly what datatype the combobox contains, because string and DateTime will require different solutions.
 
Share this answer
 
Comments
Member 12390137 30-Mar-16 8:14am    
Hi OriginalGriff
on YearYouWant i'm using dropdownlist that populate month and year as (march-2016)

SELECT * FROM MyTable WHERE DATEPART(year, DateColumn) = '"+dllmonths.selectedValue,Tostring()+"' AND DATEPART(month, DaetColumm) = '"+dllmonths.selectedValue,Tostring()+"'

the above query is giving Error how to search data into database using Dropdownlist value as (March-2016). Please help
Another take would be to find dates between the first and last days in the month e.g.
C#
DateTime dt1;
if (!DateTime.TryParse("01-" + cmddropdownlist1.SelectedText, out dt1))
{
    //Report an error
    return;
}
var firstDayNextMonth = dt1.AddMonths(1);
var dt2 = firstDayOfNextMonth.AddDays(-1);

var cmd = new SqlCommand(
    "select BusinessCallYN,CallID,NewNam,CallDate,CalledNo,CallExt,CallDuration,Amount,tmpNewNam from MCALLERS where UserName=@email and CallDate between @dt11 and @dt2 and Amount>0 order by CallDate");
cmd.Parameters.AddWithValue("@dt1", dt1);
cmd.Parameters.AddWithValue("@dt2", dt2);

Note
- I've used command parameterization
- I've use the SQL BETWEEN comparison
- DateTime.TryParse to ensure the date is in the correct format
- I've assumed that column CallDate is a Date or DateTime type
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900