How select `Only String` and `Only Int` - sql

I have some nvachar data in a my database.
aaaa , bb1b, c+cc , 1234 , dser , 5896
I need to two select for Only String and Only Int. This is mean :
Result of first select be : aaaa , dser
Result of second select be : 1234 , 5896
How?

You can use LIKE to perform this comparison. By using a double-negative, you can perform the tests with a single expression each:
declare #t table (val varchar(10) not null)
insert into #t(val)
select 'aaaa' union all
select 'bb1b' union all
select 'c+cc' union all
select '1234' union all
select 'dser' union all
select '5896'
select * from #t where val not like '%[^A-Z]%'
select * from #t where val not like '%[^0-9]%'
The first select says "give me all strings that don't contain a non-alphabetic character". Similarly, the second says "give me all strings that don't contain a non-digit"

You need to check for a regular expression.
In your example the query would look like this:
SELECT * FROM example WHERE foo LIKE '%[0-9]%' AND foo NOT LIKE '%[A-Z]%'
and
SELECT * FROM example WHERE foo NOT LIKE '%[0-9]%' AND foo LIKE '%[A-Z]%'
Maybe you have to do UPPERCASE(foo) for case insensitive checks. And add other characters like +, - and so on to the NOT LIKE expression.
See: http://msdn.microsoft.com/en-us/library/ms187489(SQL.90).aspx

Related

How to use LIKE with ANY in BigQuery?

I would like to use the LIKE ANY operator to exclude rows based on an array of substrings, but BigQuery does not recognize it.
declare unlaunched_clistings array<string>;
set unlaunched_clistings = {unlaunched_clistings} ;
select * from {p}.simeon_logs.process_errors e
where not e.message like any(unlaunched_clistings)
Error : LIKE ANY is not supported at [8:32]
Is there any workaround for this?
LIKE ANY is not supported, however you can use following 2 ways:
Use LIKE with ORs between them
WITH table AS (
SELECT 'abc' as col union all
SELECT 'xzy' as col
)
SELECT col
FROM TABLE
WHERE (col like '%abc%'
OR col like '%cde%' OR col like '%something%')
User RegEx
WITH table AS (
SELECT 'abc' as col
UNION ALL
SELECT 'xzy' as col
)
SELECT col
FROM TABLE
WHERE REGEXP_CONTAINS(col
, 'abc|cde|something')
Above both will give you abc row.

sql repeat regex pattern unlimited times

I need to select where column contains numbers only and ends with a hyphen
I'm running SQL Server Management Studio v17.9.1
I have tried:
select * from [table] where [column] like '[0-9]*-'
select * from [table] where [column] like '[0-9]{1,}-'
select * from [table] where [column] like '[0-9]{1,2}-'
none of these work. The expression ([0-9]*-) works in any regex tester I've run it against, SQL just doesn't like it, nor the other variations I've found searching.
You can filter where any but the last character are not numbers and the last is a dash. DATALENGTH/2 assumes NVARCHAR type. If you're using VARCHAR, just use DATALENGTH
SELECT
*
FROM
[table]
WHERE
[column] like '%-'
AND
LEFT([column], (datalength([column])/2)-1) NOT LIKE '%[^0-9]%'
SQL Server does not support regular expressions -- just very limited extensions to like functionality.
One method is:
where column like '%-' and
column not like '%[^0-9]%-'
You can use left() and right() functions as below :
with [table]([column]) as
(
select '1234-' union all
select '{123]' union all
select '1234' union all
select '/1234-' union all
select 'test-' union all
select '1test-' union all
select '700-'
)
select *
from [table]
where left([column],len([column])-1) not like '%[^0-9]%'
and right([column],1)='-';
column
------
1234-
700-
Demo

Oracle SQL - group by char cast

Using the a char-cast in a group-by clause results something unexpected:
select cast(col as char(2)) from (
select 'Abc' as col from dual
union all
select 'Abc' as col from dual
) group by cast(col as char(10));
The result is 'Abc ' (10 characters long).
Intuitively, I would have expected Oracle to return one of the following:
An error: 'not a group-by expression', as the group-by clause is another than the selection clause
A result of length 2 'Ab'.
Replacing cast(col as char(2)) with cast(col as char(3)), Oracle returns an error 'not a group-by expression'. This, again is a very strange behavior.
How can this be explained? What's the reason behind it?
I'm using Oracle SQL 11g.
As was mentioned above, I think there is a misunderstanding going on. o.O
I can't explain why it's doing this, but here's the pattern for the type of query you have:
If you generalize it a bit like this, where [A] and [B] are integers, and [STRING] is whatever text you want:
select cast(col as char([A])) from (
select '[STRING]' as col from dual
union all
select '[STRING]' as col from dual
) group by cast(col as char([B]));
it looks like this always fails if one of the two conditions below is true (there may be others):
( LENGTH([STRING]) < [B] OR LENGTH([STRING] > [B]) and [A] = LENGTH([STRING])
( LENGTH([STRING]) = [B] AND [A] <> LENGTH([STRING]) )
Otherwise, it'll return a row.
But if you take your example that runs and use it in a CREATE TABLE statement, it's going to fail as it sets up the column width to be the 2 and can't fit the 3 character string coming in.
To add to the oddity, if you append something at the start and the end of the string like this:
select '\*'||cast(col as char([A]))||'\*' from (
select '[STRING]' as col from dual
union all
select '[STRING]' as col from dual
) group by cast(col as char([B]));
This will only work if [A] >= [B], otherwise it fails on ORA-01489: result of string concatenation is too long.
Curious...

Like Operator for checking multiple words

I'm struggling for a like operator which works for below example
Words could be
MS004 -- GTER
MS006 -- ATLT
MS009 -- STRR
MS014 -- GTEE
MS015 -- ATLT
What would be the like operator in Sql Server for pulling data which will contain words like ms004 and ATLT or any other combination like above.
I tried using multiple like for example
where column like '%ms004 | atl%'
but it didn't work.
EDIT
Result should be combination of both words only.
Seems you are looking for this.
`where column like '%ms004%' or column like '%atl%'`
or this
`where column like '%ms004%atl%'
;WITH LikeCond1 as (
SELECT 'MS004' as L1 UNION
SELECT 'MS006' UNION
SELECT 'MS009' UNION
SELECT 'MS014' UNION
SELECT 'MS015')
, LikeCond2 as (
SELECT 'GTER' as L2 UNION
SELECT 'ATLT' UNION
SELECT 'STRR' UNION
SELECT 'GTEE' UNION
SELECT 'ATLT'
)
SELECT TableName.*
FROM LikeCond1
CROSS JOIN LikeCond2
INNER JOIN TableName ON TableName.Column like '%' + LikeCond1.L1 + '%'
AND TableName.Column like '%' + LikeCond2.L2 + '%'
Try like this
select .....from table where columnname like '%ms004%' or columnname like '%atl%'

How to select a number thats in a string format

I Have a table that has a column that can have values like
Id FileNumber
------------------
1 0025
2 CDE01
3 0035
4 25
5 45
6 250
7 XYZ1
8 2500
Now if I want to select the row that has the FileNumber 25, how can I get that. I do not want the row that has 250 or 2500. The search should only get 0025 and 25. This column is of type varchar. However, I should be able to search by any value like XYZ1 OR 0025 OR 250
I tried using the Like operator eg: WHERE FileNumber Like '%25%' also tried WHERE FileNumber Like '25%' does not give me the desired results
Any help will be greatly appreciated
Based on the sample data, I'll assume the numbers you need to retrieve are integers. So you could cast them to integers if they are numeric using something like this:
CASE WHEN ISNUMERIC(s.col) = 1 THEN CAST(s.col AS INT) END
Example:
WITH sample AS (
SELECT '0025' AS col
UNION ALL
SELECT 'CDE01'
UNION ALL
SELECT '0035'
UNION ALL
SELECT '25'
UNION ALL
SELECT '45'
UNION ALL
SELECT '250'
UNION ALL
SELECT 'XYZ1'
UNION ALL
SELECT '2500')
SELECT CASE WHEN ISNUMERIC(s.col) = 1 THEN CAST(s.col AS INT) END
FROM sample s
You can use that in a derived table/inline view to compare against. It's possible you could add a computed column using the logic to the table.
You want anything that
matches exactly (25)
ends in with only leading zeroes) (0025)
This code does that with some LIKE matching
WITH sample AS (
SELECT '0025' AS col
UNION ALL SELECT 'CDE01'
UNION ALL SELECT '0035'
UNION ALL SELECT '25'
UNION ALL SELECT '45'
UNION ALL SELECT '250'
UNION ALL SELECT 'XYZ1'
UNION ALL SELECT 'XYZ125'
UNION ALL SELECT '125'
UNION ALL SELECT '2500')
SELECT
s.col
FROM
sample s
WHERE
col = '25'
OR
(REPLACE(col, '25', '00') NOT LIKE '%[^0]%' AND col LIKE '%25')
How about this?
SELECT * FROM [TABLE NAME]
WHERE FILENUMBER LIKE '%25'
Please use this to get all the rows from the table having the values that exactly matches the comma seprated values.
SELECT * FROM TABNAME WHERE FILENUMBER in ('25','0025','xyz')
It will select all the rows who contains the filenumber as 25 or 0025 or xyz.
If it's safe to fix the data...do that.
Try something like this
update
set filename = cast(filename as int)
where filename like '%[1-9]%' --has a number in it
and filename like '%[^a-z]%' -- doesn't have a letter in it