I have a SQL 2008 database with a table having a field ‘Category’ of type nvarchar, which has values such as :
A1
A10
A2
B1/1
B1/2
B1/3
D1(1)
D1(2)
etc.
I know this is not good structure for the data, but we now need to somehow narrow down searches to this table such that the user will enter a start and end category (i.e. A1 to A15, or B1/1 to B/3) and we want to only return records with these category values.
Is there a way this can be done? The other thing is that we would also like to have the results sorted correctly (ie. A10 after A2).
c.
You can narrow down the return result using WHERE clauses. For example:
You can use this values (‘A1’ and ‘A15’) as input parameters of user defined function. Then it will be very easy to pass ‘B1’ and ‘B15’ or everything else instead them.
Also, if you want to display ‘A10’ after ‘A2’
For more information about user defined functions – User Defined functions
Check this information for strings compression in T-SQL. Also you should spend some time on collisions – are you comparing strings case sensitive or case insensitive.
As to “The other thing is that we would also like to have the results sorted correctly (ie. A10 after A2)” you can add some logic in your function like this: