我如何把一个SQL日期的结果对英国的格式(DD / MM / YYYY) [英] How do I convert an SQL date result to British format (dd/mm/yyyy)

查看:129
本文介绍了我如何把一个SQL日期的结果对英国的格式(DD / MM / YYYY)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计的ASP / C#一个基本的搜索数据库中的记录,通过搜索和提供与这些领域,他们可以用它来搜索用户:词/短语,dateFrom,dateTill。所以在进入你好,世界!,2009年5月25日,2009年6月25日分别将搜索与内容的数据库记录你好,世界!这是给定的日期之间创建的。 pretty简单的东西。

I have designed a basic search in ASP/C# for searching through records in a database and have provided the user with these fields which they can use to search: word/phrase, dateFrom, dateTill. So entering "Hello, World!", 05/25/2009, 06/25/2009 respectively will search for a database record with the contents "Hello, World!" which was created between the given dates. Pretty simple stuff.

用户输入到字段中的文本直接注入SQL语句就像如下:

The text entered by the user into the fields is injected directly into an SQL statement like follows:

SELECT r.idRecord, r.recordText
FROM record r
WHERE LOWER(CAST(r.recordText AS VARCHAR)) LIKE LOWER('%" + word/phrase + "%')
  AND r.creationDate BETWEEN '" + dateFrom + "' AND '" + dateTill + "'"

所以还是pretty简单,当用户在美国的格式,MM / DD / YYYY日期进入它的工作原理就像一个魅力。然而,当在英国格式的日期被输入等,25/05/2009它失败。最后...

So still pretty simple and it works like a charm when the user enters dates in an American format, mm/dd/yyyy. However, it fails when a date in British format is entered like, 25/05/2009. Finally...

SELECT CONVERT(VARCHAR(10), GETDATE(), 103) AS [DD/MM/YYYY]

...并不特别帮助,因为我不想选择日期,可以在类似的语句的WHERE子句中使用?

...doesn't particularly help because I don't want to select the date, can a similar statement be used in the WHERE clause?

感谢您的帮助。

推荐答案

解析使用的的DateTime 数据类型值=HTTP ://msdn.microsoft.com/en-us/library/w2sa9yss.aspx相对=nofollow> DateTime.ParseExact方法。这种方法允许你指定的日期时间的格式,所以你可以使用 DD / MM / YYYY MM / DD / YYYY ,这取决于用户的preferences。然后,DateTime值作为参数传递到你的SQL语句。

Parse the value entered by the user into a DateTime data type using the DateTime.ParseExact method. This method allows you to specify the format of the datetime, so you can use dd/MM/yyyy or MM/dd/yyyy, depending on your user's preferences. Then pass the DateTime value as a parameter to your SQL statement.

下面是一篇博客文章,解释

Here is a blog post that explains


  • 如何使用参数化的SQL语句和

  • SQL为什么按字符串拼接是邪恶的:

编码恐怖:给我SQL参数化,毋宁死

这篇关于我如何把一个SQL日期的结果对英国的格式(DD / MM / YYYY)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆