简体   繁体   English

使用VARCHAR列的一部分作为where子句中的日期

[英]Use part of VARCHAR column as date in where clause

I have a table where the varchar column CREATED_BY has the data in the format 我有一个表,其中varchar列CREATED_BY的格式为
USER - dd/MM/yyyy hh:mm . USER - dd/MM/yyyy hh:mm

I'm trying to do data migration and need to get records where the created date is greater than a certain date, but the format of the column makes this difficult, so 我正在尝试进行数据迁移,并且需要获取创建日期大于特定日期的记录,但是列的格式使此操作变得困难,因此

SELECT * FROM TABLE_NAME WHERE -- last part of CREATED_BY > SOMEDATE

Well, since the dd/MM/yyyy hh:mm format has a fixed length, you can use the RIGHT function to extract the data part from your string: 好吧,由于dd/MM/yyyy hh:mm格式的长度是固定的,因此您可以使用RIGHT函数从字符串中提取数据部分:

SELECT * 
FROM TABLE_NAME 
WHERE CONVERT(datetime, RIGHT(CREATED_BY, 16), 103) > somedate 

您需要从字符串中提取日期:

WHERE cast(reverse ( substring ( reverse ( @string  ) , 1 , 16 ) )  as datetime) > somedate

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

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