SQL查询以获取日期,无论它插入数据库的格式是什么? [英] SQL query to get date whatever the format it is inserted in database?

查看:55
本文介绍了SQL查询以获取日期,无论它插入数据库的格式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hii ,,

我正在使用自定义格式将日期存储在数据库中.日期应以MM/dd/yyyy格式存储.我必须根据日期检索结果.我的表格如下所示:

ID名称日期

1迪亚2012年10月10日

2奈提克2012年9月10日

现在要检索此数据(如果我编写查询以选择2012年9月10日的记录,则显示空结果.)我想要的是我指定的日期格式(2012年9月10日或2012年10月10日) )我应该能够检索结果.如何为此编写SQL查询?

谢谢,

Hii,,

I am using custom format to store the date in the database .It should be stored in this form MM/dd/yyyy. I have to retrieve the results based on the date.My table looks like this:

Id Name Date

1 Diya 09/10/2012

2 Naitik 09/10/2012

Now to retrieve this data if I write a query to select the records on 9/10/2012 then it is showing null results ..What I want is whatever the date format I give(9/10/2012 or 09/10/2012) I should be able to retrieve the results .How can I write the SQL query for this?

Thanks,,,

推荐答案

假设您正在使用SQL Server,则可以使用 ^ ]函数比较您的日期.
在您的where子句中,您可以将其写为
Assuming you are using SQL Server, you can use DATEDIFF[^] function to compare your dates.
In your where clause you can write it as
DATEDIFF(DAY, Date, @Date) = 0


其中@Date是您要与表中的date列进行比较的日期.

使用这种方法,您不必担心日期存储在数据库中的格式.


Where @Date is the date you are comparing with the date column in your table.

With this approach you don''t have to worry about the format in which the date is stored in database.


如果您具有自定义日期格式,并且查询看起来像:
If you have custom date format and your query looks like:
SELECT *
FROM Details
WHERE Date=@Date


使用设置日期格式 [


use SET DATEFORMAT[^] command before SELECT statement.

Example:

SET DATEFORMAT mdy;
SELECT *
FROM Details
WHERE Date=@Date


这篇关于SQL查询以获取日期,无论它插入数据库的格式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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