SQL Server datetime LIKE select?

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

    You can also use convert to make the date searchable using LIKE. For example,

    select convert(VARCHAR(40),create_date,121) , * from sys.objects where     convert(VARCHAR(40),create_date,121) LIKE '%17:34%'
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-28 03:09

    I am a little late to this thread but in fact there is direct support for the like operator in MS SQL server.

    As documented in LIKE help if the datatype is not a string it is attempted to convert it to a string. And as documented in cast\convert documentation:

    default datetime conversion to string is type 0 (,100) which is mon dd yyyy hh:miAM (or PM).

    If you have a date like this in the DB:

    2015-06-01 11:52:59.057
    

    and you do queries like this:

    select * from wws_invoice where invdate like 'Jun%'
    select * from wws_invoice where invdate like 'Jun 1%'
    select * from wws_invoice where invdate like 'Jun 1 %'
    select * from wws_invoice where invdate like 'Jun 1 2015:%'
    select * from wws_invoice where invdate like 'Jun ? 2015%'
    ...
    select * from wws_invoice where invdate like 'Jun 1 2015 11:52AM'
    

    you get that row.

    However, this date format suggests that it is a DateTime2, then documentation says:

    21 or 121 -- ODBC canonical (with milliseconds) default for time, date, datetime2, and datetimeoffset. -- yyyy-mm-dd hh:mi:ss.mmm(24h)

    That makes it easier and you can use:

    select * from wws_invoice where invdate like '2015-06-01%'
    

    and get the invoice record. Here is a demo code:

    DECLARE @myDates TABLE (myDate DATETIME2);
    INSERT INTO @myDates (myDate)
    VALUES
    ('2015-06-01 11:52:59.057'),
    ('2015-06-01 11:52:59.054'),
    ('2015-06-01 13:52:59.057'),
    ('2015-06-01 14:52:59.057');
    
    SELECT * FROM @myDates WHERE myDate LIKE '2015-06-01%';
    SELECT * FROM @myDates WHERE myDate LIKE '2015-06-01 11%';
    SELECT * FROM @myDates WHERE myDate LIKE '2015-06-01 11:52:59%';
    SELECT * FROM @myDates WHERE myDate LIKE '2015-06-01 11:52:59.054%';
    

    Doing datetime searches in SQL server without any conversion to string has always been problematic. Getting each date part is an overkill (which unlikely would use an index). Probably a better way when you don't use string conversion would be to use range checks. ie:

    select * from record 
    where register_date >= '20091010' and register_date < '20091011';
    
    0 讨论(0)
  • 2020-11-28 03:10

    I solved my problem that way. Thank you for suggestions for improvements. Example in C#.

    string dd, mm, aa, trc, data;
    dd = nData.Text.Substring(0, 2);
    mm = nData.Text.Substring(3, 2);
    aa = nData.Text.Substring(6, 4);
    trc = "-";
    data = aa + trc + mm + trc + dd;
    
    "Select * From bdPedidos Where Data Like '%" + data + "%'";
    
    0 讨论(0)
  • 2020-11-28 03:14

    There's no direct support for LIKE operator against DATETIME variables, but you can always cast the DATETIME to a VARCHAR:

    SELECT (list of fields) FROM YourTable
    WHERE CONVERT(VARCHAR(25), register_date, 126) LIKE '2009-10-10%'
    

    Check the MSDN docs for a complete list of available "styles" in the CONVERT function.

    Marc

    0 讨论(0)
提交回复
热议问题