kevin.upshaw Posted May 5, 2012 Share Posted May 5, 2012 Hi. Wonder if anyone can help. Am trying to retrieve records from my order table from a date inputted by the user. I've done all the SQL on my site up to here - this one is really baffling me through. Query: SELECT * FROM [order] WHERE orderDateTime LIKE '04/05/2012%'; However, it doesn't return any values - see my data types in my table below: As you can see from the data view there is data applicable in there to my query: Am at a loss. Can anyone help? Thanks. Kevin Link to comment Share on other sites More sharing options...
Ingolme Posted May 6, 2012 Share Posted May 6, 2012 I believe Datetime fields are stored in the format YYYY-MM-DD HH:MM:SS Link to comment Share on other sites More sharing options...
kevin.upshaw Posted May 6, 2012 Author Share Posted May 6, 2012 Hi. I have tried the following query: SELECT * FROM [order] WHERE orderDateTime LIKE '%2012-05-04%' Unfortunately that doesn't return anything. However if I run this query: SELECT * FROM [order] WHERE orderDateTime LIKE '%2012%' It pulls back all my orders. Its as soon as I put a "-" or a "/" it just doesn't return any values. Kev Link to comment Share on other sites More sharing options...
kevin.upshaw Posted May 6, 2012 Author Share Posted May 6, 2012 I have also tried using the convert function to no joy (to return the date value): SELECT * FROM [order] WHERE CONVERT(NVARCHAR, [orderdatetime], 101) = CONVERT(NVARCHAR, '2012-05-05', 101) Link to comment Share on other sites More sharing options...
Err Posted May 6, 2012 Share Posted May 6, 2012 Try this: SELECT * FROM [order] WHERE orderDateTime LIKE '04_05_2012%' Link to comment Share on other sites More sharing options...
kevin.upshaw Posted May 6, 2012 Author Share Posted May 6, 2012 Hi. Thanks for your help. I managed to get around it using this code: var extractOrderInformation = "SELECT * FROM [order] WHERE CONVERT(NVARCHAR, orderdatetime, 103) BETWEEN CONVERT(NVARCHAR, @0, 103) AND CONVERT(NVARCHAR, @1, 103)"; Thanks again. Kev Link to comment Share on other sites More sharing options...
Guest LH91325 Posted May 12, 2012 Share Posted May 12, 2012 Another solution might be this: SELECT * FROM [order] WHERE orderDateTime LIKE '%2012%05%04%' Link to comment Share on other sites More sharing options...
Ingolme Posted May 14, 2012 Share Posted May 14, 2012 Another solution might be this: SELECT * FROM [order] WHERE orderDateTime LIKE '%2012%05%04%'Remember that you can use the underscore to match exactly one character rather than zero or more: %2012_05_04% Link to comment Share on other sites More sharing options...
Guest LH91325 Posted May 14, 2012 Share Posted May 14, 2012 Hey, I didn't know that! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now