How can I write a select statement to select only integers (and nothing more) from a char column in SQL Server. For example, my table name is POWDER with 2 columns, ID (int) and Name(char (5))
ID Name
-- ----------
1 AXF22
2 HYWWW
3 24680
4 8YUH8
5 96635
I want to be able to select only those rows that contain an integer and nothing more (ID 3 and ID 5 in this example)
If I try:
SELECT *
FROM POWDER
WHERE Name LIKE '[0-9]%'
...it will return:
ID Name
-- ----------
3 24680
4 8YUH8
5 96635
Any ideas how to get the rows containing just integers?
SELECT * FROM POWDER WHERE IsNumeric(Name) = 1
IsNumeric returns 1 for some other characters that are valid in numbers, such as + and - and $ but for your input you should be fine.
Try this:
SELECT * FROM Table WHERE Name LIKE '[0-9]%%'
To avoid issues with ISNUMERIC and all spaces, -, +, . etc, use the fact that the column is char(5)
SELECT *
FROM POWDER
WHERE Name LIKE '[0-9][0-9][0-9][0-9][0-9]'
Edit: for any number of characters. Double negative...
SELECT *
FROM POWDER
WHERE Name NOT LIKE '%[^0-9]%'
Use positive and negative checks to make sure we have an integer: It must contain a digit. Only digits and spaces are allowed. No spaces are allowed between digits.
SELECT *
FROM POWDER
WHERE Name LIKE '%[0-9]%'
AND Name NOT LIKE '%[^0-9 ]%'
AND Name NOT LIKE '%[0-9]% %[0-9]%'
Try:
SELECT *
FROM POWDER
WHERE Name patindex ('%[a-z]%',name) != 0
The last one is the best,kind of works really really well.
SELECT * FROM POWDER
WHERE Name LIKE '%[0-9]%'
AND Name NOT LIKE '%[^0-9 ]%'
AND Name NOT LIKE '%[0-9]% %[0-9]%'
Related
How can I select records where in the column Value the 5th character is letter A?
For example the following records:
ID Value
-------------------------
1 1234A5636A6363
2 1234A4343B6363
3 1234B5353A6363
if I run
select * from table
where Value like '%A%'
this will return all records
but all I want is the first 2 where the 5th character is A, regardless if there are more A characters in the text or not
select *
from your_table
where substring(Value, 5, 1) = 'A'
The LIKE operator, in addition to %, which matches any number of any character, can use _, which matches any one single character. You may try:
SELECT *
FROM yourTable
WHERE Value LIKE '____A%'; -- 4 underscores here
use like below by using _(underscore)
LIKE '____A%'
SQL Server
select *
from YourTableName
where CHARINDEX('A', ColumnName) = 5
Note:- This finds where string 'A' starts at position 5
AND specify Your ColumnName
I am trying to formulate a query that will allow me to find all records from a single column with 3 hyphens. An example of a record would be like XXXX-RP-XXXAS1-P.
I need to be able to sort through 1000s of records with either 2 or 3 hyphens.
You can REPLACE the hyphens in the string with an empty string and compute the difference of the length of original string and the replaced string to check for the number of hyphens.
select *
from yourtable
where len(column_name)-len(replace(column_name,'-',''))=3
and substring(column_name,9,1) not like '%[0-9]%'
If your records have 2 or 3 hyphens, then just do:
where col like '%-%-%-%'
This will get 3 or more hyphens. For exactly 3:
where col like '%-%-%-%' and col not like '%-%-%-%-%'
try this,
declare #t table(col1 varchar(50))
insert into #t values ('A-B'),('A-B-C-D-E'),('A-B-C-D')
select * from
(SELECT *
,(len(col1) - len(replace(col1, '-', ''))
/ len('-')) col2
FROM #T)t4
where col2=3
Is there a way to use Pattern Matching with SQL LIKE, to match a variable number of characters with an upper limit?
For example, I have one column which can have "correct values" of 2-10 numbers, anything more than 10 and less than 2 is incorrect.
If you really want to use like you can do:
where col like '__' or col_like '___' or . . .
or:
where col like '__%' and -- at least two characters
col not like '_________%' -- not 9 characters
The more typical method would be:
where len(col) between 2 and 10
If you want to ensure they are numbers:
where len(col) between 2 and 10 and
col not like '%[^0-9]%'
You could make a function to do this the long way of inspecting each character like below:
DECLARE #TempField varchar(max)= '1v3Abc567'
DECLARE #FieldLength int = LEN(#TempField)
DECLARE #CountNumeric int = 0
WHILE #FieldLength > 0
BEGIN
SELECT #CountNumeric = #CountNumeric + ISNUMERIC( SUBSTRING (#TempField,#FieldLength,1))
SET #FieldLength = #FieldLength - 1
END
SELECT #CountNumeric NumericCount
You can use Regex.
SELECT * FROM mytable WHERE mycol LIKE '%[0-9]{2,10}%';
should do it (that's off the top of my head, so double-check!
For example I have this table with a zipcode column(zipcode character varying(5)) but some of "bad" rows have invalid zipcode like "9000" or "2". and I want all the tuples whose zipcode values have exactly 5 characters.
How to translate
SELECT * FROM mytable WHERE zipcode "is size 5"
to SQL?
I am using postgresql btw.
SELECT * FROM mytable WHERE zipcode like "_____"
(Five underscores)
OR
SELECT * FROM mytable WHERE LENGTH(Trim(zipcode)) = 5
LENGTH function in postgresql
Imagine that you've data like below
zipcode
-------
5
5
900 5
900 2
900
90025
68656
so you need to use trim() function else no need to use trim(), just char_length(zipcode)=5; or length(zipcode)=5; is enough.
SELECT *
FROM zips
WHERE length(trim(zipcode)) =5
or
SELECT *
FROM zips
WHERE char_length(trim(zipcode))=5
trim(string):- Remove the longest string containing only the characters (a
space by default)
from the start/end/both ends of the string
length(string):- returns the length of the string measured in bytes.
char_length(string) or character_length(string):- returns the length of the string measured in
characters.
I have a table in which I want to get the strings which are like ab aabb aaabbb ...... a n times followed by b n times as shown below.
Eg TABLE:
value
----------
ab
aabb
aaabbb
aaaabbbb
1
1a
abababa
I want the result TABLE to be:
value
----------
ab
aabb
aaabbb
aaaabbbb
I've tried like this
select * from [NumTest] where value LIKE '[a]+[b]+'
but it's returning zero rows.
Can anybody help me how to use special characters in SQL Server's LIKE ?
Here is something that can work:
(EDIT - after O/P comment, commented parts not needed)
--WITH CTE_GoodValues AS
--(
SELECT value
FROM Table1
WHERE LEFT(VALUE,LEN(VALUE)/2) = REPLICATE('a',LEN(VALUE)/2)
AND RIGHT(VALUE,LEN(VALUE)/2) = REPLICATE('b',LEN(VALUE)/2)
AND LEN(VALUE)%2=0
--)
--SELECT REPLICATE(' ', (SELECT MAX(LEN(VALUE))/2 FROM CTE_GoodValues)- LEN(VALUE)/2) + VALUE
--FROM CTE_GoodValues
In the CTE - select values that have left half all a-s and right half all b-s. Then find MAX length and use it to replicate needed empty spaces in front
DEMO (after edit)