Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server datetime LIKE select?

in MySQL

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

What is the syntax in SQL Server?

like image 776
Paisal Avatar asked Oct 27 '09 06:10

Paisal


People also ask

Can we use like with datetime in SQL?

Like operator will not work with DateTime.

How do I select a date from a timestamp in SQL?

In MySQL, use the DATE() function to retrieve the date from a datetime or timestamp value. This function takes only one argument – either an expression which returns a date/datetime/ timestamp value or the name of a timestamp/datetime column. (In our example, we use a column of the timestamp data type.)


1 Answers

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.

like image 132
Ralph Lavelle Avatar answered Sep 23 '22 16:09

Ralph Lavelle