如何获取日期在varchar中的两个日期之间的结果 [英] how to get the result for between two date where date is in varchar

查看:74
本文介绍了如何获取日期在varchar中的两个日期之间的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,其中记录了每日销售。

现在我想让客户在两个日期之间保持优秀。

我的date_列在varchar。

im没有得到所需的结果。通过这个查询和我在查询之间使用它显示错误的数据。那么解决方案是什么?请帮助

Quote:

SELECT

clientid AS [ID]

,SUM(转换(浮动,总计))AS [SUM]

FROM

buffalo_milk_sale

WHERE

clientid'WD001'''HD099'

AND

CONVERT(DateTime,date_,103)> = CONVERT(DateTime,'01 / 09/2014 ',103)

和CONVERT(DateTime,date_,103)< = C>

错误

Quote:

将varchar数据类型转换为日期时间数据类型会导致超出范围的值。

解决方案

您确定Date列的所有日期都是DD / MM / YYYY格式吗?

使用这两个代码并查看第二个错误

  SELECT   CONVERT  DateTime '  13/12/2014' 103 



  SELECT  < span class =code-keyword> CONVERT ( DateTime '  2014年12月13日' 103 



然后你会明白问题是什么


在比较之前,不应该将当前日期转换为varchar格式,特别是当你使用>,<运算符,而不是在比较之前将数据(此处为sales-date)转换为来自varchar的Date格式。



检查数据库服务器的默认日期格式是否相同作为用于以varchar格式存储数据的日期格式。



如果日期格式不同,则需要更改数据库服务器的默认格式(如果可能) )或需要以默认格式重新构建日期。您可以使用SUBSTRING()函数提取日期的不同部分(以varchar格式)并连接它们以构建所需格式的日期。



请注意,这可能影响你的查询效率。



看看这里:MSDN博客:安装SQL服务器后如何更改日期格式[^]



或者,你可以使用:MSDN:SET DATEFORMAT(Transact-SQL)[^]


你可以从这个例子中得到建议



 DECLARE @TempTable作为表(Id INT,Dd VARCHAR(30))
INSERT INTO @TempTable(Id,Dd)值(1,'17 / 08/2014')
INSERT INTO @TempTable(Id,Dd)值(2,'18 / 08/2014')
INSERT INTO @TempTable(Id,Dd)值(3,'19 / 08/2014')
INSERT INTO @TempTable(Id,Dd)值(4,'20 / 08/2014')
INSERT INTO @TempTable(Id,Dd)值(5,'21 / 08/2014')

SELECT * FROM @TempTable WHERE CONVERT(DATE,Dd,103)CONWERT(DATE,'19 / 08/2014',103)和CO之间NVERT(DATE,'21 / 08 / 2014’ ,103)


i have a database where daily sale is recorded.
now i want to get the client outstanding between two dates.
my date_ column is in varchar.
i m not getting the required result.through this query and when i used between query its showing wrong data.so what is the solution? kindly help

Quote:

SELECT
clientid AS [ID]
,SUM(convert(float, total)) AS [SUM]
FROM
buffalo_milk_sale
WHERE
clientid BETWEEN 'HD001' AND 'HD099'
AND
CONVERT(DateTime, date_, 103) >= CONVERT(DateTime, '01/09/2014', 103)
and CONVERT(DateTime, date_, 103) <= C>
error

Quote:

The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

解决方案

Are you sure that your all dates of Date column is having DD/MM/YYYY format ?
Use these two codes and see the error of second one

SELECT CONVERT(DateTime, '13/12/2014', 103) 


SELECT CONVERT(DateTime, '12/13/2014', 103) 


Then you will understand what the problem is


You should not convert current date to varchar format before comparing, specially when you using >, < operator, rather you should convert your data(here sales-date) to Date format from varchar before comparing.

Check if the default date format of your DB Server is same as the date format you using to store the data in varchar format.

If the date format is different, you need to either change the DB Server default format(if possible) or need to re-construct the date in default format. You could use SUBSTRING() function to extract different part of the date(in varchar format) and concatenate them to build the date in desired format.

Note that this could impact your query efficiency.

Have a look here: MSDN Blogs: How to change Date Format after installing SQL server[^]

Alternatively, you can use: MSDN: SET DATEFORMAT (Transact-SQL)[^]


You may get idea from this example

DECLARE @TempTable As TABLE (Id INT,Dd VARCHAR(30))
INSERT INTO @TempTable (Id,Dd)values (1,'17/08/2014')
INSERT INTO @TempTable (Id,Dd)values (2,'18/08/2014')
INSERT INTO @TempTable (Id,Dd)values (3,'19/08/2014')
INSERT INTO @TempTable (Id,Dd)values (4,'20/08/2014')
INSERT INTO @TempTable (Id,Dd)values (5,'21/08/2014')

SELECT * FROM @TempTable WHERE CONVERT(DATE,Dd,103) BETWEEN  CONVERT(DATE,'19/08/2014',103) AND  CONVERT(DATE,'21/08/2014',103)


这篇关于如何获取日期在varchar中的两个日期之间的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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