简体   繁体   English

SQL:如何将日期时间与空白进行比较

[英]SQL: How to compare datetime with whitespace

I have a column date and it is type of datetime with format YYYY-MM-DD HH:MM:SS.000 我有一列date ,它是类型datetime与格式YYYY-MM-DD HH:MM:SS.000

my problem is for some dates there is a single space and others there are two spaces between the date and the time 我的问题是,对于某些日期,日期和时间之间存在一个空格,而对于其他日期,则存在两个空格

eg. 例如。

2009-08-20 13:44:12.753
2009-08-20  13:44:12.753

even though the times are the same when i try compare them they do not match due to the whitespace. 即使时间相同,当我尝试比较它们时,由于空格,它们也不匹配。 i have tried using the replace(date, ' ', '') function but this does not keep the datetime format correctly Aug2020091:44PM 我尝试使用replace(date, ' ', '')函数,但这不能正确保留日期时间格式Aug2020091:44PM

how would i compare these dates to ignore the whitespace but not affect the datetime itself? 我将如何比较这些日期以忽略空白但不影响日期时间本身?

why would'nt you use 你为什么不使用

replace(date, '  ', ' ') 

I mean - replace just 2 white spaces into one 我的意思是-仅将2个空格替换为1

( it is not clear from my code but here it on pseudo ) (根据我的代码尚不清楚,但此处为伪)

 replace(date, '[ ][ ]', '[ ]')  //where [ ] is whitespace.

solution #2: 解决方案2:

use this : 用这个 :

SELECT CAST('2009-08-20 13:44:12.753' AS DATETIME) --nevermind the spaces
SELECT CAST('2009-08-20       13:44:12.753' AS DATETIME)  --nevermind the spaces

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM