- Ensure the date format matches: Verify that the date format in your database matches the format you’re using in the LIKE clause.
- Check for leading/trailing spaces: Ensure there are no leading or trailing spaces in the date strings in your database.
- Verify the column name: Make sure the column name date is correct and matches the column name in your database.
Using 'LIKE' in database explorer to select variables corresponding to substring.
7 次查看(过去 30 天)
显示 更早的评论
Right so I have a SQL database that I'm analysing using the database toolbox and I want to select all variables corresponding to a particular date. Problem is that the dates currently include the time that the variable was measured, e.g. '2014-01-01 00:00:00'. I looked online and thought the following should work...
WHERE data.date LIKE '2014-01-01%'
to select variables where data.date starts with '2014-01-01'. When I implement this I don't get an error but nothing seems to be happening. Any ideas? Thanks in advance.
0 个评论
回答(1 个)
Piyush Kumar
2024-11-26
Hi,
The query you have shared should work as expected, selecting all records where the date column starts with ‘2014-01-01’. If you’re not seeing any results, here are a few things to check:
If you still encounter issues, you might want to try using the CONVERT function to ensure the date format is consistent:
SELECT * FROM data WHERE CONVERT(datetime, date, 120) LIKE '2014-01-01%';
This converts the date column to a string in the format ‘yyyy-mm-dd hh:mi:ss’ before applying the LIKE operator.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Database Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!