Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在SQL Server中筛选VARCHAR(50)date列_Sql_Sql Server - Fatal编程技术网

如何在SQL Server中筛选VARCHAR(50)date列

如何在SQL Server中筛选VARCHAR(50)date列,sql,sql-server,Sql,Sql Server,我使用CSV文件中的批量插入创建了一个表。我试着和date约会,但没能通过。它给了我一个错误,所以尝试使用TradeDate varchar50,一切都正常,并将“11012012”保存在TradeDate列中 但是,当我尝试使用select query检索数据时: SELECT * FROM FundTranTest WHERE CONVERT(DATETIME, RIGHT(TradeDate,4) + Left(TradeDate, 2) + SUBSTRING(TradeDate

我使用CSV文件中的批量插入创建了一个表。我试着和date约会,但没能通过。它给了我一个错误,所以尝试使用TradeDate varchar50,一切都正常,并将“11012012”保存在TradeDate列中

但是,当我尝试使用select query检索数据时:

SELECT * 
FROM FundTranTest 
WHERE CONVERT(DATETIME, RIGHT(TradeDate,4)  + Left(TradeDate, 2)  + SUBSTRING(TradeDate, 3, 2))
      < '2012-12-29' 
但它不起作用……

正如错误和评论员告诉您的那样,您导入的数据不是有效的日期时间格式。这也是更新失败的原因,数据本身不正确,这是问题的根本原因,您需要在继续之前修复数据

您需要执行以下操作之一:

删除导入的数据,通过查看tradedate的值错误的位置来更正CSV文件中的数据,更正它们,然后将数据重新导入到表中,但这次导入到正确的数据类型列中,因此不是varchar50,而是datetime。 确定当前表中哪些数据错误,将其修复为正确格式,然后将当前tradedate列从varchar50转换为datetime列。 无论您选择哪种方法,请留意@marc_的文字和链接:

您应该始终使用最合适的数据类型——毕竟,这就是它们的用途

如果您选择了第二种方法,这里有一些关于如何验证列中信息的提示,这些提示并不包括您的数据可能存在的所有错误,但它应该让您了解您可以检查哪些其他方法进行验证:

select tradedate,
       *
  from FundTranTest
 where -- Look for TradeDate's that are not even 8 chars long
       len(tradedate) <> 8
       -- Shows any years that are not starting with a 2
    or right(TradeDate, 4) not like '2%'
       -- Check that your months are correct
    or left(tradedate, 2) not in ('01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12')
       -- check that dates are correct
    or substring(tradedate, 3, 2) not in ('01', '02', '03', '04', '05', '06', '07', '08', '09', '10',
                                          '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', 
                                          '21', '22', '23', '24', '25', '16', '17', '18', '19', '30',
                                          '31')

-您应该始终使用最合适的数据类型——毕竟,这就是它们的用途!全球化设置?例如,如果您的CSV是英国格式,SQL是否设置为美国日期格式?如果您将日期保存为一个长日期字符串,那么SQL可以更轻松地计算出来。例如“2012年1月11日”,请在将来努力格式化和校对您的帖子。另请参阅以获取一些提示。如果必须处理这样的数据,最好先将这些数据转换为正确的数据类型,并在执行任何类型的查询之前将其存储到另一个表中。听起来您正在提取的某些日期行的格式不相同,或者为空,等等。不管怎样,在WHERE子句中进行这种比较,即使它确实起作用,也会破坏数据库。引擎无法为此使用任何类型的索引或缓存计划。它将不得不计算每一行。嗨,伯恩德,谢谢你的帮助。你解释了一下,它就解决了。没问题,但以后尽量少用大写字母请:
select tradedate,
       *
  from FundTranTest
 where -- Look for TradeDate's that are not even 8 chars long
       len(tradedate) <> 8
       -- Shows any years that are not starting with a 2
    or right(TradeDate, 4) not like '2%'
       -- Check that your months are correct
    or left(tradedate, 2) not in ('01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12')
       -- check that dates are correct
    or substring(tradedate, 3, 2) not in ('01', '02', '03', '04', '05', '06', '07', '08', '09', '10',
                                          '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', 
                                          '21', '22', '23', '24', '25', '16', '17', '18', '19', '30',
                                          '31')