SQL Server datetime LIKE select?

后端 未结 11 1806
别那么骄傲
别那么骄傲 2020-11-28 02:44

in MySQL

select * from record where register_date like \'2009-10-10%\'

What is the syntax in SQL Server?

11条回答
  •  死守一世寂寞
    2020-11-28 03:03

    There is a very flaky coverage of the LIKE operator for dates in SQL Server. It only works using American date format. As an example you could try:

    ... WHERE register_date LIKE 'oct 10 2009%'
    

    I've tested this in SQL Server 2005 and it works, but you'll really need to try different combinations. Odd things I have noticed are:

    • You only seem to get all or nothing for different sub fields within the date, for instance, if you search for 'apr 2%' you only get anything in the 20th's - it omits 2nd's.

    • Using a single underscore '_' to represent a single (wildcard) character does not wholly work, for instance, WHERE mydate LIKE 'oct _ 2010%' will not return all dates before the 10th - it returns nothing at all, in fact!

    • The format is rigid American: 'mmm dd yyyy hh:mm'

    I have found it difficult to nail down a process for LIKEing seconds, so if anyone wants to take this a bit further, be my guest!

    Hope this helps.

提交回复
热议问题