且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从sql server中的日期时间中删除am / pm

更新时间:2023-11-05 12:56:46

在我看来,SQL Server应该转换它的隐含性,而不使用CONVERT和CAST [ ^ ]功能(明确)。



试试这个:

  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







另一方面,我建议你使用存储过程 [ ^ ]代替在代码中构造sql命令,因为 SQL注入 [ ^ ]。



详情请见:如何:执行返回行的存储过程 [ ^ ]

如何:保护ASP.NET中的SQL注入 [ ^ ]

停止SQL注入在他们阻止你之前 [ ^ ]



使用本网站右上角的SearchBox查找更多相关信息。


Hi
u也需要避免时间,然后更好地获得结果,尝试搜索日期时间转换,你试图获得员工工资日期,所以不需要时间,如果是员工或学生登录或注销那么你使用时间。



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



SQL Server有助于将日期和时间值转换为字符串文字和其他日期和时间格式。 [ ^ ]


你可能会她使用参数化查询(强烈推荐),或以SQL友好格式格式化日期:

  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)+  '; 


I have following query

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

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

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

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

Try this:
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.


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.[^]


You could either use a Parameterized query (strongly recommended), or format your date in SQL friendly format:
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") + "'";