Can I Use Wildcards In Sql Query Datetime

Posted on  by  admin
Can I Use Wildcards In Sql Query Datetime Rating: 4,5/5 2266 votes

Using wildcard characters with datetime data. When you use like with datetime values, Adaptive Server converts the dates to the standard datetime format, then to varchar. Since the standard storage format does not include seconds or milliseconds, you cannot search for seconds or milliseconds with like and a pattern.

How do you perform a LIKE statement on a column of DateTime datatype in SQL Server?

If I run the following SQL it returns me all the dates with 2009 in.

However, if I want all the Oct, Nov & Dec dates I'd expect to be able to do the following:

But this returns me nothing!

I'm giving the user a filter option where they type in the date string and as they type I filter the data. So if they type '20', I'd like to return all the dates with '20' within the date (so this could be all 2012 dates or a date value like 06/20/1999)

Sql Wildcard

How to use like operator for date in mysql

Can anybody help?

I'm using SQL Server 2008.

Thanks in advance.

marc_s
596k135 gold badges1142 silver badges1280 bronze badges
SunSun
1,5979 gold badges38 silver badges73 bronze badges

3 Answers

Can

You can use the DATEPART function to extract portions of dates. It should also make your queries more explicit about what you're seeking to achieve:

Sql Query Wildcard Parameter

(There are also specifically named functions, such as MONTH and YEAR, but I prefer DATEPART for consistency since it can access all components of a datetime)

You should try to avoid thinking of datetimes as having any kind of string format. Treating them as strings is one of the largest sources of errors we encounter.

Damien_The_UnbelieverSql server where like datetime

Mysql Date Wildcard

Damien_The_Unbeliever
201k17 gold badges262 silver badges354 bronze badges

If you need to use Like operator (for some reason) you have to convert the DateTime column into a varchar.

MagnusMagnus
37.4k7 gold badges58 silver badges91 bronze badges
marc_s
596k135 gold badges1142 silver badges1280 bronze badges
LLAliveLLAlive

Not the answer you're looking for? Browse other questions tagged sqlsql-server-2008datetime or ask your own question.

Coments are closed
Scroll to top