SQL Server datetime LIKE select?

后端 未结 11 1826
别那么骄傲
别那么骄傲 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:00

    You could use the DATEPART() function

    SELECT * FROM record 
    WHERE  (DATEPART(yy, register_date) = 2009
    AND    DATEPART(mm, register_date) = 10
    AND    DATEPART(dd, register_date) = 10)
    

    I find this way easy to read, as it ignores the time component, and you don't have to use the next day's date to restrict your selection. You can go to greater or lesser granularity by adding extra clauses, using the appropriate DatePart code, e.g.

    AND    DATEPART(hh, register_date) = 12)
    

    to get records made between 12 and 1.

    Consult the MSDN DATEPART docs for the full list of valid arguments.

提交回复
热议问题