IS it possible to use the 'Where' clause in SQL to only show a field containing only letters & Numbers? - sql

I want to be able to select only the field where a certain field contains both letters and numbers. for example:
Select [field1], [field2]
from [db1].[table1]
where [field2] = *LETTERS AND NUMBERS*
Im using SQL Server 2005, also im sorry bu im not a hundred percent sure about the data type of the field because it is on a linked server and un-accessibleat the minute. Hope you can help
:)

LIKE will do it. This is a double negative
where [field2] NOT LIKE '%[^0-9a-z]%'
It says:
%[^0-9a-z]% means not (alphanumeric)
NOT LIKE '%[^0-9a-z]%' means not(not(alphanumeric)) -> alphanumeric
Edit:
For all numbers... "it works"
SELECT 'it works' WHERE '1234567' NOT LIKE '%[^0-9a-z]%'
All letters
SELECT 'it works' WHERE 'abcdefg' NOT LIKE '%[^0-9a-z]%'
Contains non-alphanumeric
SELECT 'it works' WHERE 'abc_123' NOT LIKE '%[^0-9a-z]%'
Edit 2:
This solution is for
only alphanumeric, any mixture of letters and numbers
Edit 3:
letters followed by numbers
where [field2] NOT LIKE '%[^0-9a-z]%' AND [field2] LIKE '[a-z]%[0-9]'
Edit:
Finally, 2 letters and upto 3 numbers
where
[field2] LIKE '[a-z][a-z][0-9]'
OR
[field2] LIKE '[a-z][a-z][0-9][0-9]'
OR
[field2] LIKE '[a-z][a-z][0-9][0-9][0-9]'

If you need it to contain both numerics and letters, and no other characters, I think you have to use 3 like clauses. One NOT LIKE, as #gbn said, then 2 LIKEs to ensure both character classes are represented:
select * from (select '123' union all select 'abc' union all select 'a2') t(Field)
where Field LIKE '%[0-9]%' and Field like '%[a-z]%'
AND Field NOT LIKE '%[^0-9a-z]%'
returns one row, with 'a2'.
If it should only be letters followed by numbers, I'm thinking you might be able to achieve this with a further not like, again inspired by #gbn:
NOT LIKE '%[0-9]%[a-z]%'
But it is starting to look like a regex in CLR might be the preferred route.

What you would want to do is SQL-based regexp matching. Check this out: http://msdn.microsoft.com/en-us/magazine/cc163473.aspx
Quotes:
"Although T-SQL is extremely powerful for most data processing, it provides little support for text analysis or manipulation. Attempting to perform any sophisticated text analysis using the built-in string functions results in massively large functions and stored procedures that are difficult to debug and maintain."
And:
"However there's SQLCLR, a CLR user-defined function (UDF) that lets you create an efficient and less error-prone set of functions using the Microsoft® .NET Framework."
Then you get code examples. Isn't Microsoft great? :D

I believe PATINDEX will do the trick for you. The query below checks for non 0-9 and non a-z characters, returning 0 if it doesn't find any (i.e., only #s and letters)
Select [field1], [field2]
from [db1].[table1]
where patindex('%[^0-9a-z]%', [field2]) = 0

Select [field1], [field2]
from [db1].[table1]
where [field2] REGEXP '^[0-9a-fA-F]*$'

Related

T-SQL - How to pattern match for a list of values?

I'm trying to find the most efficient way to do some pattern validation in T-SQL and struggling with how to check against a list of values. This example works:
SELECT *
FROM SomeTable
WHERE Code LIKE '[0-9]JAN[0-9][0-9]'
OR Code LIKE '[0-9]FEB[0-9][0-9]'
OR Code LIKE '[0-9]MAR[0-9][0-9]'
OR Code LIKE '[0-9]APRIL[0-9][0-9]
but I am stuck on wondering if there is a syntax that will support a list of possible values within the single like statement, something like this (which does not work)
SELECT *
FROM SomeTable
WHERE Code LIKE '[0-9][JAN, FEB, MAR, APRIL][0-9][0-9]'
I know I can leverage charindex, patindex, etc., just wondering if there is a simpler supported syntax for a list of possible values or some way to nest an IN statement within the LIKE. thanks!
I think the closest you'll be able to get is with a table value constructor, like this:
SELECT *
FROM SomeTable st
INNER JOIN (VALUES
('[0-9]JAN[0-9][0-9]'),
('[0-9]FEB[0-9][0-9]'),
('[0-9]MAR[0-9][0-9]'),
('[0-9]APRIL[0-9][0-9]')) As p(Pattern) ON st.Code LIKE p.Pattern
This is still less typing and slightly more efficient than the OR option, if not as brief as we hoped for. If you knew the month was always three characters we could do a little better:
Code LIKE '[0-9]___[0-9][0-9]'
Unfortunately, I'm not aware of SQL Server pattern character for "0 or 1" characters. But maybe if you want ALL months we can use this much to reduce our match:
SELECT *
FROM SomeTable
WHERE (Code LIKE '[0-9]___[0-9][0-9]'
OR Code LIKE '[0-9]____[0-9][0-9]'
OR Code LIKE '[0-9]_____[0-9][0-9]')
You'll want to test this to check if the data might contain false positive matches, and of course the table-value constructor could use this strategy, too. Also, I really hope you're not storing dates in a varchar column, which is a broken schema design.
One final option you might have is building the pattern on the fly. Something like this:
Code LIKE '[0-9]' + 'JAN' + '[0-9][0-9]'
But how you find that middle portion is up to you.
The native TSQL string functions don't support anything like that.
But you can use a workaround (dbfiddle) such as
WHERE CASE WHEN Code LIKE '[0-9]%[^ ][0-9][0-9]' THEN SUBSTRING(Code, 2, LEN(Code) - 3) END
IN
( 'JAN', 'FEB', 'MAR', 'APRIL' )
So first of all check that the string starts with a digit and ends in a non-space character followed by two digits and then check the remainder of the string (not matched by the digit check) is one of the values you want.
The reason for including the SUBSTRING inside the CASE is so that is only evaluated on strings that pass the LIKE check to avoid possible "Invalid length parameter passed to the LEFT or SUBSTRING function." errors if it was to be evaluated on a shorter string.

How to search using similar characters in random positions in Microsoft SQL?

I'm looking for a query that would allow the user to use a variation of characters while searching for a result. The character positions are completely random. We use special characters È,Š,Ć,Č,Ž and Đ so all of the variations have to match, because most of users do not know how to spell correctly.
Example:
MISIC
MISIĆ
MISIČ
MIŠIC
MIŠIĆ
MIŠIČ
You can search it by using COLLATE
SELECT *
FROM TableNAme
WHERE
columnName COLLATE Like '%MISIC%' COLLATE Latin1_general_CI_AI
latin1 makes the server treat strings using charset latin 1,
basically ascii.
CI specifies case-insensitive, so "ABC" equals to "abc".
AI specifies accent-insensitive,so 'ü' equals to 'u'.
for more information collation go through the
Collete
refereance : #JINO SHAJI
as per #Adephx comment this is working as expected with few modification
SELECT * FROM [TABLE] WHERE [COLUMN] LIKE '%NAME%' COLLATE Latin1_general_CI_AI
Applying COLLATION is a great practice, especially if we want to get rid of all Accent-marks, however, if we need more granular control over individual accent-characters (È,Š,Ć,Č,Ž), we can do something like below to selectively compare individual accent-characters.
Most DBMSs provide string-comparison functionality based on how the words sound (pronounced). SQL Server provides two built-in functions for this: SOUNDEX() and DIFFERENCE(). In this scenario we can do this:
IF (DIFFERENCE('MISIC', 'MISIĆ')>=4)
AND (DIFFERENCE('MISIC', 'MISIČ')>=4)
AND (DIFFERENCE('MISIC', 'MIŠIC')>=4)
AND (DIFFERENCE('MISIC', 'MIŠIĆ')>=4)
AND (DIFFERENCE('MISIC', 'MIŠIČ')>=4)
PRINT 'Same word'
ELSE
PRINT 'Different word.'
Actually, in many languages 'Š' sounds quite different than 'S', therefore SQL Server considers them as less-compatible, but here is a workaround to impose equivalence:
WITH words AS (SELECT value FROM STRING_SPLIT(N'MISIĆ,MISIČ,MIŠIC,MIŠIĆ,MIŠIČ', ','))
SELECT
value,
CASE WHEN (DIFFERENCE('MISIC', replace(value,'Š','S'))>=4)
THEN 'Same word'
ELSE 'Not same'
END AS 'Comparison'
FROM words
Output:
value comparison
----- ----------
MISIĆ Same word
MISIČ Same word
MIŠIC Same word
MIŠIĆ Same word
MIŠIČ Same word
Above example will work in "Microsoft SQL Server 2016" or above, note that the STRING_SPLIT() function is only used to iterate over the array of words/strings, this function is not available in SQL Server 2014 or below.
Hope this helps.

SQL fetch results by concatenating words in column

I have column store_name (varchar). In that column I have entries like prime sport, best buy... with a space. But when user typed concatenated string like primesport without space I need to show result prime sport. how can I achieve this? Please help me
SELECT *
FROM TABLE
WHERE replace(store_name, ' ', '') LIKE '%'+#SEARCH+'%' OR STORE_NAME LIKE '%'+#SEARCH +'%'
Well, I don't have much idea, and even I am searching for it. But may be what I know works for you, You can achieve this by performing different type of string operations:
Mike can be Myke or Myce or Mikke or so on.
Cat an be Kat or katt or catt or so on.
For this you should write a function to generate number of possible strings and then form a SQL Query using all these, and query the database.
A similar kind of search in known as Soundex Search from Oracle and Soundex Search from Microsoft. Have a look of it. this may work.
And overall make use of functions like upper and lower.
Have you tried using replace()
You can replace the white space in the query then use like
SELECT * FROM table WHERE replace(store_name, ' ', '') LIKE '%primesport%'
It will work for entries like 'prime soft' querying with 'primesoft'
Or you can use regex.

SQL: insert space before numbers in string

I have a nvarchar field in my table, which contains all sorts of strings.
In case there are strings which contain a number following a non-number sign, I want to insert a space before that number.
That is - if a certain entry in that field is abc123, it should be turned into abc 123, or ab12.34 should become ab 12. 34.I want this to be done throughout the entire table.
What's the best way to achieve it?
You can try something like that:
select left(col,PATINDEX('%[0-9]%',col)-1 )+space(1)+
case
when PATINDEX('%[.]%',col)<>0
then substring(col,PATINDEX('%[0-9]%',col),len(col)+1-PATINDEX('%[.]%',col))
+space(1)+
substring(col,PATINDEX('%[.]%',col)+1,len(col)+1-PATINDEX('%[.]%',col))
else substring(col,PATINDEX('%[0-9]%',col),len(col)+1-PATINDEX('%[0-9]%',col))
end
from tab
It's not simply, but I hope it will help you.
SQL Fiddle
I used functions (link to MSDN):
LEFT, PATINDEX, SPACE, SUBSTRING, LEN
and regular expression.

I have a problem in SQL Server 2000 when searching for a term in farsi

I have a problem in SQL Server 2000 with farsi search.
I have a table with nvarchar fields with unicode (farsi) values and need to search content of that with unicode (farsi) text.
I am using
select * from table1
where fieldname like '%[farsi word]%'
My farsi word is exist but return 0 row.
What can I do?
thanks all.
If you're using NVARCHAR fields, you should also use Unicode when searching! You do this by prepending a N before your search term:
select * from table1
where fieldname like N'%[farsi word]%'
Also: be aware the if your search term begins with a % wildcard, you've basically disabled all use of any indices there might be to speed up your search. Using LIKE %...% for searching will always result in a pretty slow table scan....