Define Range in SQL Query using Regular Expression - sql-server

I have the following values
ABCD_AB_1234
ABCD_ABC_2345
ABCD_ABCD_5678
and a regular expression to match them
ABCD_[A-Z]{2-4}_[0-9]{4}
Now I am looking to convert that regular expression to a SQL query so I can get those records back from the database.
Right now I have following where clause
where [columnName] like 'ABCD_[A-Z][A-Z]%[_][0-9][0-9][0-9][0-9]%'
The problem is that I cannot define a range in the SQL query as I did in the regular expression, like {2-4}, what I am doing now is to set the minimum range only.
Is there any solution?

Assuming you are explaining the full picture the easiest way is probably to create 3 conditions to cover your scenarios e.g.
where [columnName] like 'ABCD_[A-Z][A-Z][_][0-9][0-9][0-9][0-9]%'
or [columnName] like 'ABCD_[A-Z][A-Z][A-Z][_][0-9][0-9][0-9][0-9]%'
or [columnName] like 'ABCD_[A-Z][A-Z][A-Z][A-Z][_][0-9][0-9][0-9][0-9]%'
Its not optimal but SQL Server doesn't have any regex support so if you have to do it in SQL this is one way.

Related

What are the rules for using an ADO Recorset filter with a SQL Server uniqueidentifier column?

I'm trying to use the ADO Filter property to filter a Recordset with a uniqueidentifier column. I'm connecting to SQL Server 2008 using ADO on Windows 7, using the SQLOLEDB.1 provider.
I create a Recordset and use it to send the SQL query to the server. Then I set the ADO Recordset.Filter property. I get errors no matter what I've tried for the Filter.
If I try
[column name] = '5D9C83FB-E758-0D4B-B1C7-E751D951B67C'
I get
Filter cannot be opened.
If I try
[column name] like '%5D9C83FB-E758-0D4B-B1C7-E751D951B67C%'
I get
Invalid class string.
(Note: I've left out any required double-quotes for clarity. I'm able to search other types of string columns so the quoting isn't the problem.)
I am using Delphi XE2, but I think that's probably not relevant as I'm creating my ADO object directly rather than use any of their wrapper code. I have no problem with the Filter property on any of the various string type columns, just uniqueidentifier.
My basic question is if you can make this work, how do you do it (short of converting the column to a string during querying)? The more general question is what are the rules around using uniqueidentifier columns with the Filter property. I couldn't find anything relevant.
In both of those cases you're treating them like strings. I suggest trying wrapping the GUID value in curly braces:
[column name] = '{5D9C83FB-E758-0D4B-B1C7-E751D951B67C}'
In Delphi (and most other frameworks), you cannot filter GUID locally in a RecordSet with syntax similar to LIKE with strings. To the frameworks, GUIDs are just a bunch of bytes, not a string.
You have to do the filtering on the server side using the LIKE syntax that SQL Server itself supports (an example of the syntax is in How to use SQL's LIKE on a guid in Entity Framework?).

TSQL - Get maximum length of data in every column in every table without Dynamic SQL

Is there a way to get maximum length of data stored in every column in the database? I have seen some solutions which used Dynamic SQL, but I was wondering if it can be done with a regular query.
Yes, Just query the INFORMATION_SCHEMA.COLUMNS view for the database, you can get the information out from all columns of all tables in the database if you desire, see the following for more details:
Information_Schema - COLUMNS
If you are talking about the length of particular data in and not the declared length of a column, I am afraid that is not achievable without dynamic SQL.
The reason is that there is only way to retrieve data, and that is the SELECT statement. This statement however requires an explicit column, which is part of the statement itself. There is nothing like
-- This does not work
select col.Data
from Table
where Table.col.Name='ColumnName'
So the answer is: No.

Get SQL Server 2005 sort varchar columns just as Java would do

My problem is very simple: I have an array of String. Sorting it in Java gives one order , and in Sql Server 2005 slightly different order . An example of difference I spotted was in the case of two strings: "jyl ; pagespro" , "jyl" , which Java sorts in this order, and Sql in the inverse order .
I tried to make Sql Server order by ascii(myColumn) but still with no effect. How can I solve this ??.....
To extend on DaveE's answer, if you have determined the collation you want to use for sorting, you can use its name in the ORDER BY clause like this:
ORDER BY colA COLLATE SQL_Latin1_General_Cp437_CS_AS_KI_WI.
Please be aware that this would not use an index for sorting that might be defined on colA. You could get around that by using calculated columns.
In general, I would suggest to do all sorting in one place: either in Java, or in the database, but not sometimes here and sometimes there. This just leads to confusion and complexity.
I'd suspect it has to do with the SQL Server collation in effect. That affects not only the available character set but also how the characters sort against one another. Even in the fairly generic SQL_Latin1_General family, there are dozens of specific collations available.

Using an IN clause with LINQ-to-SQL's ExecuteQuery

LINQ to SQL did a horrible job translating one of my queries, so I rewrote it by hand. The problem is that the rewrite necessarily involves an IN clause, and I cannot for the life of me figure out how to pass a collection to ExecuteQuery for that purpose. The only thing I can come up with, which I've seen suggested on here, is to use string.Format on the entire query string to kluge around it—but that will prevent the query from ever ending up in the query cache.
What's the right way to do this?
NOTE: Please note that I am using raw SQL passed to ExecuteQuery. I said that in the very first sentence. Telling me to use Contains is not helpful, unless you know a way to mix Contains with raw SQL.
Table-Valued Parameters
On Cheezburger.com, we often need to pass a list of AssetIDs or UserIDs into a stored procedure or database query.
The bad way: Dynamic SQL
One way to pass this list in was to use dynamic SQL.
IEnumerable<long> assetIDs = GetAssetIDs();
var myQuery = "SELECT Name FROM Asset WHERE AssetID IN (" + assetIDs.Join(",") + ")";
return Config.GetDatabase().ExecEnumerableSql(dr=>dr.GetString("Name"), myQuery);
This is a very bad thing to do:
Dynamic SQL gives attackers a weakness by making SQL injection attacks easier.
Since we are usually just concatenating numbers together, this is highly unlikely, but
if you start concatenating strings together, all it takes is one user to type ';DROP TABLE Asset;SELECT '
and our site is dead.
Stored procedures can't have dynamic SQL, so the query had to be stored in code instead of in the DB schema.
Every time we run this query, the query plan must be recalculated. This can be very expensive for complicated queries.
However, it does have the advantage that no additional decoding is necessary on the DB side, since the AssetIDs are found by the query parser.
The good way: Table-Valued Parameters
SQL Server 2008 adds a new ability: users can define a table-valued database type.
Most other types are scalar (they only return one value), but table-valued types can hold multiple values, as long as the values are tabular.
We've defined three types: varchar_array, int_array, and bigint_array.
CREATE TYPE bigint_array AS TABLE (Id bigint NOT NULL PRIMARY KEY)
Both stored procedures and programmatically defined SQL queries can use these table-valued types.
IEnumerable<long> assetIDs = GetAssetIDs();
return Config.GetDatabase().ExecEnumerableSql(dr=>dr.GetString("Name"),
"SELECT Name FROM Asset WHERE AssetID IN (SELECT Id FROM #AssetIDs)",
new Parameter("#AssetIDs", assetIDs));
Advantages
Can be used in both stored procedures and programmatic SQL without much effort
Not vulnerable to SQL injection
Cacheable, stable queries
Does not lock the schema table
Not limited to 8k of data
Less work done by both DB server and the Mine apps, since there is no concatenation or decoding of CSV strings.
"typical use" statistics can be derived by the query analyzer, which can lead to even better performance.
Disadvantages
Only works on SQL Server 2008 and above.
Rumors that TVP are prebuffered in their entirety before execution of the query, which means phenomenally large TVPs may be rejected by the server.
Further investigation of this rumor is ongoing.
Further reading
This article is a great resource to learn more about TVP.
If you can't use table-valued parameters, this option is a little faster than the xml option while still allowing you to stay away from dynamic sql: pass the joined list of values as a string parameter, and parse the delimited string back to values in your query. please see this article for instructions on how to do the parsing efficiently.
I have a sneaking suspicion that you're on SQL Server 2005. Table-valued parameters weren't added until 2008, but you can still use the XML data type to pass sets between the client and the server.
This works for SQL Server 2005 (and later):
create procedure IGetAListOfValues
#Ids xml -- This will recevie a List of values
as
begin
-- You can load then in a temp table or use it as a subquery:
create table #Ids (Id int);
INSERT INTO #Ids
SELECT DISTINCT params.p.value('.','int')
FROM #Ids.nodes('/params/p') as params(p);
...
end
You have to invoke this procedure with a parameter like this:
exec IGetAListOfValues
#Ids = '<params> <p>1</p> <p>2</p> </params>' -- xml parameter
The nodes function uses an xPath expression. In this case, it's /params/p and that's way the XML uses <params> as root, and <p> as element.
The value function cast the text inside each p element to int, but you can use it with other data types easily. In this sample there is a DISTINCT to avoid repeated values, but, of course, you can remove it depending on what you want to achieve.
I have an auxiliary (extension) method that converts an IEnumerable<T> in a string that looks like the one shown in the execute example. It's easy to create one, and have it do the work for you whenever you need it. (You have to test the data type of T and convert to an adequate string that can be parsed on SQL Server side). This way your C# code is cleaner and your SPs follow the same pattern to receive the parameters (you can pass in as many lists as needed).
One advantage is that you don't need to make anything special in your database for it to work.
Of course, you don't need to create a temp table as it's done in my example, but you can use the query directly as a subquery inside an IN predicate
WHERE MyTableId IN (SELECT DISTINCT params.p.value('.','int')
FROM #Ids.nodes('/params/p') as params(p) )
I am not 100% sure that I understand correctly the problem, but LinqToSql's ExecuteQuery has an overload for parameters, and the query is supposed to use a format similar to string.Format.
Using this overload is safe against SQL injection, and behind the scenes LinqToSql transalets it to use sp_executesql with parameters.
Here is an example:
string sql = "SELECT * FROM city WHERE city LIKE {0}";
db.ExecuteQuery(sql, "Lon%"); //Note that we don't need the single quotes
This way one can use the benefit of parameterized queries, even while using dynamic sql.
However when it comes to using IN with a dynamic number of parameters, there are two options:
Construct the string dynamically, and then pass the values as an array, as in:
string sql = "SELECT * FROM city WHERE zip IN (";
List<string> placeholders = new List<string>();
for(int i = 0; i < zips.Length;i++)
{
placeholders.Add("{"+i.ToString()+"}");
}
sql += string.Join(",",placeholders.ToArray());
sql += ")";
db.ExecuteQuery(sql, zips.ToArray());
We can use a more compact approach by using the Linq extension methods, as in
string sql = "SELECT * FROM city WHERE zip IN ("+
string.Join("," , zips.Select(z => "{" + zips.IndexOf(f).ToString() + "}"))
+")";
db.ExecuteQuery(sql, zips.ToArray());

SQL server search

I'm going to perform a search in my SQL server DB (ASP.NET, VS2010,C#), user types a phrase and I should search this phrase in several fields, how is it possible? do we have functions such as CONTAINS() in SQL server? can I perform my search using normal queries or I should work in my queries using C# functions?
for instance I have 3 fields in my table which can contain user search phrase, is it OK to write following sql command? (for instance user search phrase is GAME)
select * from myTable where columnA='GAME' or columnB='GAME' or columnC='GAME
I have used AND between different conditions, but can I use OR? how can I search inside my table fields? if one of my fields contains the phrase GAME, how can I find it? columnA='GAME' finds only those fields that are exactly 'GAME', is it right?
I'm a bit confused about my search approach, please help me, thanks guys
OR works fine if you want at least one of the conditions to be true.
If you want to search inside your text strings you can use LIKE
select * from myTable where columnA like '%GAME%' or columnB like '%GAME%' or columnC like '%GAME%'
Note that % is the wildcard.
If you want to find everything that begins with 'GAME' you type LIKE 'GAME%', if you allow 'GAME' to be in the middle you need % in both ends.
You can use LIKE instead of equals and then it can contain wildcard characters, so your example could be:
select * from myTable where columnA LIKE '%GAME%' or columnB LIKE '%GAME%' or columnC LIKE '%GAME%'
Further information may be found in MSDN
This is going to do some pretty heavy lifting in terms of what the database has to do though - I would suggest you consider something like full text search as I think it would more likely be suited to your scenario and provide faster results (of course, if you never have many records to search LIKE would probably do fine). Information on this is also in MSDN
Don't use LIKE, as suggested by other answers. It won't work with indexes, and therefore will be slow to return and expensive to run. Instead, you have two options:
Option 1: Full-Text Indexes
do we have functions such as CONTAINS() in SQL server?
Yes! You can use the CONTAINS() function in sql server. You just have to set up a full-text index for each of the columns you need to search on.
Option 2: Lucene.Net
Lucene.Net is a popular client-side library for searching text data that integrates closely with Sql Server. You can use it to make implementing your search a little easier.

Resources