sql server中的日期列和查询 [英] date column in sql server and querying

查看:94
本文介绍了sql server中的日期列和查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从UI生成报告,其中涉及执行查询,例如date1和date2之间的currrentdate。



该应用程序正在为一家印度公司托管。默认情况下,数据库以mm / dd / yy存储。



但客户在UI中输入日期时更喜欢看dd / mm / yy格式并不关心sql server中的语言环境。



默认设置为US,这很好..但如何使用它来准确查询? 。

有人可以帮我在这里帮助设置具有适当语言环境的数据库列,并指导我完成SQL查询。



我是询问sql date列是默认的 - mm / dd / yy。



查询的where子句应该是 -



dd-mm-yy格式?

在'1-1-2013'和'31 -12-2014'之间的当前日期

OR

mm / dd / yy格式?

在'1/1/2013'和'12 / 31/2014之间的当前日期'

解决方案

试试这个。它不会使用任何分离器。这也有助于避免mm / dd和dd / mm错误。



112格式是yyyymmdd。



WHERE转换(currentDate,112)在'20130101'和'20141231'之间


这是您的解决方案



 其中  convert  varchar  20 ),currentDate, 103 之间 '  2013年1月1日'  AND  '  2014年12月31日' 





您必须将日期转换为dd / mm / yyyy格式,103格式为dd / mm / yyyy格式



更多datetime format命令你可以参考在sql中转换函数 [ ^ ]


使用此解决方案



  WHERE  GETDATE() BETWEEN   CONVERT  DATE  '  01/01/2014' AND   CONVERT  DATE '  12 /二千〇一十四分之三十一' 


I am generating a report from the UI which involves performing a query like where currrentdate between date1 and date2.

The application is being hosted for an indian company. The database by default stores it in mm/dd/yy.

But the customer prefers to see the format dd/mm/yy when he enters the dates in the UI and doesnt care about the locale in the sql server.

By default its set to US, which is fine..but how to do work with it to query accurately?.
Can someone help me here to help set up the database column with proper locale and guide me through the sql query.

I am asking the sql date column is by default- mm/dd/yy.

The where clause of the query should be like-

dd-mm-yy format?
WHERE currentDate between '1-1-2013' AND '31-12-2014'
OR
mm/dd/yy format?.
WHERE currentDate between '1/1/2013' AND '12/31/2014'
OR

解决方案

Try this. It doesenot use any seperator. This also helps in avoiding mm/dd and dd/mm erros.

112 format is yyyymmdd.

WHERE convert(currentDate,112) between '20130101' AND '20141231'


Here is your solution

where convert(varchar(20),currentDate ,103) between '1/1/2013' AND '12/31/2014'



You have to convert your date to dd/mm/yyyy format, 103 gives dd/mm/yyyy format

for more datetime format command you can refer Convert function in sql[^]


use this solution

WHERE GETDATE() BETWEEN CONVERT(DATE,'01/01/2014') AND CONVERT(DATE,'12/31/2014')


这篇关于sql server中的日期列和查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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