A query to get me specific alphabetic order for sybase - sybase

I have a table called telephone_contacts that contain two columns:
telephone_contacts (
Name varchar(100)
Numbers number(20)
)
the column name contains about 20,000 rows.
I want to filter the name by alphabetic , example:
I want a query that get me only the first 6 alphabetic (A , B, C , D ,E ,F G)
Then, a query that get me the last 6 alphabetic (U,V,W,X,Y,Z)
example: the column name contains the following data:
Abe, car, night, range, chicken, zoo, whatsapp,facebook, viber Adu , aramt, Bike, Male, dog,egg
I want a query that get me only (A , B, C , D ,E ,F G) so the results will be
abe ,care ,chicken facebook,adu,aramt,bike, dog, egg
the rest are ignored
In oracle I can do it like this, how do I do it for sybase?
SELECT * FROM user_tab_cols WHERE SUBSTR(UPPER(table_name),1) BETWEEN 'A' and 'Q'
SELECT * FROM user_tab_cols WHERE SUBSTR(UPPER(table_name),1) BETWEEN 'P' and 'Z'

In Sybase you can use the regex (regular expression) to sepecify character ranges [A-G] Assuming your server is set to case insensitive you can do the following:
SELECT * FROM telephone_contacts WHERE name LIKE "[A-G]%"
SELECT * FROM telephone_contacts WHERE name LIKE "[U-Z]%"
or
SELECT * FROM telephone_contacts WHERE name BETWEEN "A%" and "G%"
SELECT * FROM telephone_contacts WHERE name BETWEEN "U%" and "Z%"
If you find that your server is case sensitive, then you can do what was suggested in another answer, and use upper(name)

It's even simpler:
select * from yourtable where upper(name) like "[A-Q]%"
select * from yourtable where upper(name) like "[P-Z]%"

Related

Count the number of multiple values in a single column in SQL

I have a long list of string values that I would like to count, in one particular column of a table. I know this works for counting all unique values.
SELECT
code_id ,
COUNT(*) AS num
FROM
mydb
GROUP BY
code_id
ORDER BY
code_id
I only have a certain selection of values to count, therefore do now want all. My list is long, but for example, if I just wanted to count the numbers of strings 'ax1', 'c39', and 'x1a' in my code_id column? I've seen examples with multiple lines of code, one for each value which will be huge for counting many values. I'm hoping for something like :
SELECT
code_id ,
COUNT(* = ('ax1, 'c39', 'x1a')) AS num
FROM
mydb
GROUP BY
code_id
ORDER BY
code_id
Desired output would be
code_id count
ax1 39
c39 42
x1a 0
Is there an easy way, rather than a line of code for each value to be counted?
Create a CTE that returns all the string values and a LEFT join to your table to aggregate:
WITH cte AS (SELECT code_id FROM (VALUES ('ax1'), ('c39'), ('x1a')) c(code_id))
SELECT c.code_id,
COUNT(t.code_id) AS num
FROM cte c LEFT JOIN tablename t
ON t.code_id = c.code_id
GROUP BY c.code_id;
See the demo.
I think this should work.
SELECT
code_id ,
sum(1) AS num
FROM Mydb
WHERE code_id in ('ax1', 'c39', 'x1a')
GROUP BY code_id
ORDER BY code_id

Table with record occurrences count for each column in PostgreSQL

I'm looking to generate a table of columns containing count of the occurences of each unique record using PostgreSQL. My current approach is following:
with a as (SELECT count(*) as count_each_record_a
FROM (SELECT column1 as word from table_name) t
group by word),
b as (SELECT count(*) as count_each_record_b
FROM (SELECT column2 as word from table_name) t
group by word)
select * from a, b;
When I run part of the query, as in:
SELECT count(*) as count_each_record_b
FROM (SELECT column2 as word from table_name) t
group by word
then I get proper results.
When I run the whole query, I get the proper results for count_each_record_a, but column count_each_record_b is populated only with 1. Column count_each_record_b should be the same as when running the query by itself.
What am I doing wrong here?
I've actually found an answer, even if it's not that straightforward:
with a as (SELECT ARRAY(SELECT count(*)
FROM (SELECT a as word from table) t
group by word) as a),
b as (SELECT ARRAY (SELECT count(*)
FROM (SELECT b as word from table) t
group by word) as b)
select * from a, b
I get for each column one row with an array containing the occurences. Since I'm using this data further, it does serve my needs, but I would't call this a proper solution for the concept problem at hand.

Snowflake Ordering on Text seems Incorrect

I am new to snowflake and have noticed the ordering on text columns does not behave as expected.
Take this simple example:
select *
from ( values ('ab'), ('aBc'), ('acd') ) t(col1)
order by col1
Expected order: ab, aBc, acd
Actual order: aBc, ab, acd
Am I missing something?
Thank you.
You could use COLLATE specification directly in the order by clause.
The collate lets you specify following configuration settings to be used when comparing values:
Locale
Case-sensitivity
Accent-sensitivity
Punctuation-sensitivity
First-letter preference
Case-conversion
Space-trimming
Following example uses English Locale(en) and Case Insensitive(ci) collation:
select *
from ( values ('ab'), ('aBc'), ('acd'), ('Z') ) t(col1)
order by collate(col1, 'en-ci');
Result returned:
ab
aBc
acd
Z
According to documentation:
All data is sorted according to the numeric byte value of each character in the ASCII table. UTF-8 encoding is supported.
In the ASCII table, B comes before b.
It's weird the order by didn't consider the string length in the ordering.
This will also sort "Z" before "a" because it's first in ASCII / Unicode order. You can order by with an upper function:
select *
from ( values ('ab'), ('aBc'), ('acd'), ('Z') ) t(col1)
order by col1
To sort without case sensitivity, you can use the upper or lower function.
select *
from ( values ('ab'), ('aBc'), ('acd'), ('Z') ) t(col1)
order by upper(col1)

SELECT row if concatenated value contains text

select dbo.strconcat(r.Name) as Names, r.Category
from Reference r
group by r.Category
For this query I need get categories which have at least one name (r.Name) LIKE any text.
Example: condition (LIKE '%123%')
123,2,4 - correct Names
Is this what you are looking for:
;WITH A
AS
(SELECT dbo.strconcat
(r.Name
) AS Names, r.Category
FROM Reference r
GROUP BY r.Category
)
SELECT *
FROM A
WHERE Names LIKE '%123%';

SQL Server Select Where value LIKE(temporary table value)

I have a function in SQL server 2008 that takes a string: 'A,B,C,D' and splits it and creates a table of the values.
Values
------
A
B
C
D
I now want to search a table (Users) Where a column value is LIKE one of the rows (surname) in the above table.
This is what I would like to do:
SELECT * FROM Users WHERE vLastName LIKE 'A%'
SELECT * FROM Users WHERE vLastName LIKE 'B%'
SELECT * FROM Users WHERE vLastName LIKE 'C%'
SELECT * FROM Users WHERE vLastName LIKE 'D%'
If the above is not possible, how else would you do it? Some kind of loop?
I'm using SQL Server 2008
SELECT * FROM Users,NewTable WHERE vLastName LIKE Values + '%'
SELECT * from Users u
JOIN StringSplitterResult r on r.Values = SUBSTRING( u.vLastName, 1,1)

Resources