简体   繁体   中英

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

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

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

( it is not clear from my code but here it on pseudo )

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

solution #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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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