sorry if this is a duplicate, I wasn't able to find what I was looking for in the answered questions.
I'm looking to query for only records with a field formatted like this numbers (0-9), hyphen (-), number (0-9), hyphen (-), numbers (0-9). This is what I have tried:
SELECT *
FROM TABLE_1
WHERE LTRIM(RTRIM(LOC_NAME)) LIKE '[0-9]-[0-9]-[0-9]'
The result set I'm looking for would be 123456-123-1234.
I thought at first they may have spaces so I trimmed the field but still no results are showing with the ABOVE query. The BELOW query returns them, but with other results:
SELECT *
FROM TABLE_1
WHERE LOC_NAME LIKE '%[0-9]-[0-9]%'
But I would get results like 1-2-3 Place...
I think this does what you want:
SELECT *
FROM TABLE_1
WHERE LTRIM(RTRIM(LOC_NAME)) NOT LIKE '%[^-0-9]%'
This checks that the field has no non-hyphens or non-digits.
If you specifically want two hyphens, separated by digits, then:
WHERE LTRIM(RTRIM(LOC_NAME)) NOT LIKE '%[^-0-9]%' AND
LTRIM(RTRIM(LOC_NAME)) LIKE '[0-9]%-[0-9]%-[0-9]%' AND
LTRIM(RTRIM(LOC_NAME)) NOT LIKE '%-%-%-%'
The second pattern requires at least two hyphens and a digit in all three parts of the name. The third forbids three hyphens.
I would do this way
select *
from table_1
where isnumeric(replace(LOC_NAME, '-','')) = 1;
Update (2018-Jun-12)
After reading the comments of #EzLo, I realized that the OP may just need two hyphens (no more, no less), so I am updating my answer with the following demo code
create table #t (LOC_NAME varchar(100));
go
insert into #t (loc_name)
values ('a12-b12-123'), ('123456-123-11'), ('123-123-123-123')
go
select *
from #t --table_1
where isnumeric(replace(LOC_NAME, '-','')) = 1
and len(loc_name)-len(replace(LOC_NAME, '-',''))=2
The result is:
Related
Here are my queries:
(Won't return a value)
select * from T_VoucherHeaderEntry
where Vhe_VoucherNo = 'APV-1808-00160'
(Will return a value)
Select * from T_VoucherHeaderEntry where Vhe_VoucherNo like 'APV-1808-00160%'
I tried trimming my first query but it doesn't work.
You appear to have other control characters in your stored data, specifically carriage-return and line-feed. This highlights the issue and the final query finds all rows currently affected by this1:
;declare #t table (Val1 varchar(20))
insert into #t(Val1) values ('abc
'),('def')
select * from #t where Val1 = 'abc'
select * from #t where Val1 like 'abc%'
select * from #t where Val1 like '%
%'
So, fix those rows however you choose to do so. Next, add a CHECK constraint on this column:
ALTER TABLE T_VoucherHeaderEntry
ADD CONSTRAINT CK_T_VoucherHeaderEntry_NoExoticChars
CHECK (Vhe_VoucherNo not like '%[^-A-Za-z0-9]%')
(It's expressed as a double negative to say we want to disallow any character in the provided range. We have to put - as the first character so that it's interpreted literally and not as a range separator)
And finally update your applications to not attempt to insert such bogus data in the first place.
1The third query identifies those specifically affected by CR/LF issue. For a more general approach, once you've decided on the appropriate character range to specify in your check constraint, a variant of that same approach will find rows that won't satisfy the check constraint for you to fix.
if your Vde_VoucherNo column contain this 'APV-1808-00160' value then definitely below should work and return data
select * from T_VoucherHeaderEntry
where Vhe_VoucherNo = 'APV-1808-00160'
in case of white-space in your column value, you can use trim function
select * from T_VoucherHeaderEntry
where trim(Vhe_VoucherNo) = 'APV-1808-00160'
But if your column contain pattern of this values APV-1808-00160 then like will work which is your 2nd query
Select * from T_VoucherHeaderEntry
where Vhe_VoucherNo like 'APV-1808-00160%'
BTW noticed the two query is from two different table , so that may be also reason
I am using query as follows to get any records that begins with any character, has bunch of 0s and ends with number (1 in this case).
where column like '_%[0]1'
But the issue is it's even returning me d0101 etc. which I don't want. I just want d0001, or r0001. Can I use it to exactly match pattern, not partially using like?
Any other options in ms-sql?
SQL-Server does not really do proper regular expressions but you can generate the search clause you want like this:
where column like '_%1' and column not like '_%[^0]%1'
The second condition will exclude all cases where you have a character other than 0 in the middle of the string.
It will allow strings of all possible lengths, provided they start with an arbitrary character, then have any number of 0s and finish with a 1. All other strings will not satisfy the where clause.
create table tst(t varchar(10));
insert into tst values('d0101');
insert into tst values('d0001');
insert into tst values('r0001');
select * from tst where PATINDEX('%00%1', t)>0
or
select * from tst where t like '%00%1'
You use the _ to say that you don't care what char is there (single char) and then use the rest of the string you want:
DECLARE # TABLE (val VARCHAR(100))
INSERT INTO #
VALUES
('d0001'),
('f0001'),
('e0005'),
('e0001')
SELECT *
FROM #
WHERE val LIKE '_0001'
This code only really handles your two simple examples. If it is more complex, add it to your post.
I want to put a condition in my query where I have a column that should contain second position as an alphabet.
How to achieve this?
I've tried with _[A-Z]% in where clause but is not working. I've also tried [A-Z]%.
Any inputs please?
I think you want mysql query. like this
SELECT * FROM table WHERE column REGEXP '^.[A-Za-z]+$'
or sql server
select * from table where column like '_[a-zA-Z]%'
You can use regular expression matching in your query. For example:
SELECT * FROM `test` WHERE `name` REGEXP '^.[a-zA-Z].*';
That would match the name column from the test table against a regex that verifies if the second character is either a lowercase or uppercase alphabet letter.
Also see this SQL Fiddle for an example of data it does and doesn't match.
agree with #Gordon Linoff, your ('_[A-Z]%') should work.
if not work, kindly add some sample data with your question.
Declare #Table Table
(
TextCol Varchar(20)
)
Insert Into #Table(TextCol) Values
('23423cvxc43f')
,('2eD97S9')
,('sAgsdsf')
,('3Ss08008')
Select *
From #Table As t
Where t.TextCol Like '_[A-Z]%'
The use of '%[A-Z]%' suggests that you are using SQL Server. If so, you can do this using LIKE:
where col like '_[A-Z]%'
For LIKE patterns, _ represents any character. If the first character needs to be a digit:
where col like '[0-9][A-Z]%'
EDIT:
The above doesn't work in DB2. Instead:
where substr(col, 2, 1) between 'A' and 'Z'
Suppose in my like clause for my query, i would like to match all records that begins with the characters 'Harris' in my specified column. But their can also be names such as 'Harrison' that I would also like to match. How would I go about finding this
select *
from MyTable
where somecol LIKE 'Harris%'
I have a column that has comma seperated values and I need to select all rows that have 13 commas. They seperate numbers so I don't need to worry about any strings that contain commas. How would I do this?
alternative to like (I do not like the like, and the above will fail if contains 14 commas or more)
select * from table
where length(replace(your_column, ',', ''))=length(your_column)-13;
for better utilize the index, you should seek to normalize your table
If you're using PostgreSQL, you could also use regular expressions.
However, a better question might be why you have a single column with comma-separated values instead of multiple columns.
If you count a string with 14 commas as having 13 commas, then this will work:
SELECT * FROM table WHERE column LIKE '%,%,%,%,%,%,%,%,%,%,%,%,%,%'
% matches any string (including zero length).
In PostgreSQL you can do:
select col from table where length(regexp_replace(col, '[^,]', '', 'g')) = 13;