Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have following query
C#
string query1 = "select sum(total) from dbo.total_salary where user_id=@userid and paydate>=convert(datetime,'" + DateFrom + "') and paydate<=convert(datetime,'" + DateTo + "')";


where datefrom and dateto are parameters..
On debugging I get value in datefrom and dateto parameter as

C#
select sum(total) as total from dbo.total_salary where user_id='greencity' and paydate>(convert(datetime,'2/2/2014 12:00:00 AM')) and paydate<=(convert(datetime, '2/28/2014 12:00:00 AM'))


But it returns null..
when in sql I execute query as
SQL
select sum(total) as total from dbo.total_salary where user_id='greencity' and paydate>(convert(datetime,'2/2/2014 12:00:00')) and paydate<=(convert(datetime, '2/28/2014 12:00:00'))

It returns value..
So plz help me how to remove am/pm part in datetime
Posted
Updated 26-Feb-14 23:25pm
v2

In my opinion, SQL Server should convert it implicity, without using CONVERT and CAST[^] function (explicity).

Try this:
SQL
DECLARE @sdate VARCHAR(30)

SET @sdate = '2/28/2014 12:00:00 AM'

SELECT MyDate
FROM (
    SELECT CONVERT(DATETIME,'2/27/2014 12:00:00 PM') AS MyDate UNION ALL
    SELECT CONVERT(DATETIME,'2/28/2014 12:00:00 AM') AS MyDate UNION ALL
    SELECT CONVERT(DATETIME,'1/01/2014 12:00:00 AM') AS MyDate UNION ALL
    SELECT CONVERT(DATETIME,'12/31/2014 12:00:00 AM') AS MyDate UNION ALL
    SELECT CONVERT(DATETIME,'2/22/2014 12:00:00 AM') AS MyDate
) AS T
WHERE MyDate <= @sdate



On the other hand, i would suggest you to use stored procedure[^] instead construct sql command in code, because of SQL Injection[^].

For further information, please see: How to: Execute a Stored Procedure that Returns Rows[^]
How To: Protect From SQL Injection in ASP.NET[^]
Stop SQL Injection Attacks Before They Stop You[^]

Use SearchBox at the top-right corner of this site to find more information about that.
 
Share this answer
 
Comments
Peter Leow 27-Feb-14 10:21am    
Best! +5.
Maciej Los 27-Feb-14 10:54am    
Thank you, Peter ;)
Hi u need to avoid time also,then it better to get result,try to search date time convert,u are try to get employee salary date,so no need time,if it is employee or student login or logout then u use time.

http://www.w3schools.com/sql/func_convert.asp

SQL Server Functions that helps to convert date and time values to and from string literals and other date and time formats.[^]
 
Share this answer
 
v2
Comments
heta.dave 28-Feb-14 1:50am    
Thanks..
this links helps me a lot..
You could either use a Parameterized query (strongly recommended), or format your date in SQL friendly format:
C#
string query1 = "SELECT SUM(total) FROM dbo.total_salary WHERE user_id=@userid AND paydate BETWEEN '" + DateFrom.ToString("yyyy-MM-dd") + "' AND '" + DateTo.ToString("yyyy-MM-dd") + "'";
 
Share this answer
 
Comments
heta.dave 27-Feb-14 5:35am    
it not works..
OriginalGriff 27-Feb-14 6:01am    
"it not works..." is a pointless thing to say.
If your car breaks down in the middle of nowhere, do you ring the garage, say "it no works.." and put the phone down? And if you did, how long are you going to be waiting until they arrive with exactly the parts to fix it?

Tell us what it does that you didn't expect, or doesn't do that you did!
Give us error messages, data, information!

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