|
Update : i've pasted the code without the ISNULL which seems to be faster. Update : i've come up with two different approaches for doing this i will edit my original code. Please let me know what is a better approach and if both are correct etc... Hi all, I've been tasked with creating a customer search stored proc that has the following fields. FirstName LastName City Postcode Country Name DateOfBirth Obviously for the user i would like them to search by ANY or ALL fields and have the wildcards bring back similar records i.e. search LastName Smith would bring back Smiths, Smithy, etc. No fields are mandatory. I'm struggling a bit with the DOB. The DOB field is datetime in the database how would i search for a partial DOB like 12/ or 2012/ for example? This is what i've come up with so far... Please help! I'm doing this to fix the current search which isn't working properly so is fairly urgent! Thanks.
(comments are locked)
|
|
@jhowe if you are looking to cope with ANY or ALL fields, then your procedure is not catering this scenario with all the ANDs in there? As far as date search is concerned, if you are sure that the datepart would be dd-mm-yyyy, then there should also be assurance that the current stored format of the searched DOB column be dd-mm-yyyy as well. One option could be to search like Another option could be (Does not seems efficient though but search for more records) And same for the year part. But its all guesswork. You need to sit down with the stake holders and should come up with what could be the scenarios and how they need to be dealt with. EDIT: Assuming if any field is not meant to be searched, then the respective parameter would be NULL EDIT1: Is it what you want You're right USMAN. As the DOB is in a fixed format i have updated my question with the correct format the app needs. It is 110 style. You're also correct about AND not working. Replacing AND with OR doesn't work either. I need some sort of combination of the both? The app also has validation on it so that the full date in that format MUST be entered.
Jun 28 '12 at 01:38 PM
jhowe
@jhowe I have edited my answer for AND/OR thing.
Jun 28 '12 at 02:09 PM
Usman Butt
Hi Usman, I've posted two different queries can you let me know which one is best?
Jun 28 '12 at 04:00 PM
jhowe
@jhowe That you have to test as it would depend. But I do not like COALESCE unless it is inevitable. Use ISNULL instead, that could be much faster. For e.g. Hope it helps.
Jun 28 '12 at 04:11 PM
Usman Butt
(comments are locked)
|
|
TBH I wouldn't try and do all this in one stored procedure. Can you push back on the application development team and see if they 'know' at search time what field is being passed through - that way you can tailor a sp for each 'type' of search. (This will also alleviate any bad plan issues cause by parameter sniffing) Then for DoB searches, the UI can give a date picker (with wildcards) instead of a string input. You would then have a segmented date coming through and can easily search using Hi Kev, Unfortunately the app is outsourced and changes have all been released etc. so i just have to make it work. The database is only a few gig, couple million records so it doesn't have to be massively efficient/complicated...
Jun 28 '12 at 11:47 AM
jhowe
I've just found out that the DOB search input field has to be in format dd-mm-yyyy and it has a calendar picker... so that might make it a bit easier... They also have the option to manually enter a date.
Jun 28 '12 at 11:51 AM
jhowe
if the format is dd-mm-yyyy and comes from a picker then there should be no option to search for a partial date then?
Jun 28 '12 at 11:52 AM
Kev Riley ♦♦
and sorry - just reread the question - being able to cope with ANY or ALL fields - scrub the suggestion about a tailored sp!
Jun 28 '12 at 12:01 PM
Kev Riley ♦♦
@Kev Riley Excellent advice. +1.
Jun 28 '12 at 12:19 PM
Usman Butt
(comments are locked)
|

