For a project, I have to loop through the alphabet and run a search for each letter against some values in a database. The function would return the number of matches for each letter.
I would like to be able to do this in a SQL Stored Procedure, but I'm not certain how I could do a 'FOR letter = A to Z' loop in a SP. Does anyone know how this could be done?
it depends according to alphabet if you only need English characters you can do a loop from 65 (ascii for A) and 90 (Z) and use char letter = (char)i to get the letter.
If you also need non-English ones just set a web config setting "ABC......Z" and loop through it.
with ATable(c) as
(
select cast('A' as CHAR(1)) as c
union all
select CHAR(ASCII(c)+1) as C from ATable where C<'Z'
)
select * from ATable
SQLFiddle demo
Use a loop going from 65 (A) to 90 (Z), and use the T-SQL CHAR() function.
Of course, I'm assuming that you are using a SQL Server database. If not, please post the DB you're using.
You need to use "group by" and possibly include "count" in the query too. you can find further information here http://msdn.microsoft.com/en-us/library/ms177673.aspx
;WITH Alphabet AS
(
SELECT CHAR(65) AS Letter, 65 AS Code
UNION ALL
SELECT CHAR(Code + 1), Code + 1
FROM Alphabet
WHERE Code < 90
)
SELECT Letter
FROM Alphabet
WITH alpha AS
(
SELECT 65 AS c
UNION ALL
SELECT c + 1 FROM alpha
WHERE c < 90
)
SELECT CHAR(c) FROM alpha
Related
I have a column that is stored in ###0.0000000000 format. In a report I'm generating I need it to only show a few significant digits. Problem is the number needed changes based on the product with a default of 2. There's a column in another table that provides the required digits per each product.
I've tried a few things so far but it seems to not like it and throws a syntax error.
Cast(A.Price as Numeric(10,coalesce(B.Sig_Digits,2)))
That threw an error so I tried making the coalesce part a column and aliasing it in case the coalesce broke it, and that didn't work either. Round will take a column as an argument but I don't want it to round. Other than an ugly
case when Sig_digits = 1 then to_char(price,'###0.0') when Sig_digits = 2...
etc. what other options are there? This is a very large report, with 100+ columns and a few million rows so I'd prefer to not do the case when.
Use TO_CHAR with RPAD to add 0s to the end of the format model to the correct number of decimal places:
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE table_name ( value, sig ) AS
SELECT 123.456789, 2 FROM DUAL UNION ALL
SELECT 123456789.123456789, 7 FROM DUAL;
Query 1:
SELECT TO_CHAR( value, RPAD( 'FM999999999990.', sig + 15, '0' ) )
FROM table_name
Results:
| TO_CHAR(VALUE,RPAD('FM999999999990.',SIG+15,'0')) |
|---------------------------------------------------|
| 123.46 |
| 123456789.1234568 |
Am new to sqlite in my learning I come across the subString function so in my exercise, My table name is t1 and my column value is Partha000099 I want to increment by 1 eg., Partha000100 when i try with
SELECT SUBSTR(MAX(ID),6) FROM t1
am getting output as 000099 when I increment by 1 with the below query
SELECT SUBSTR(MAX(ID),6)+1 FROM t1
am getting output as 100, Now my question is how to construct it back as I expect
I tried with the below query,
SELECT 'Partha' || SUBSTR(MAX(ID),6)+1 FROM t1
am getting output as 1. Please some one help me.
While my solution will work, I would advice you against this type of key generation. "SELECT MAX(ID)+1" to generate the next key will be fraught with problems in more concurrent databases and you risk generating duplicate keys in a busy application/system.
It would be better to split the key into two columns, one with the group or name 'Partha', and the other column with an automatically incremented number.
However, having said that, here's how to generate the next key like your example.
You need to:
Split the key into two
Increment the numeric part
Convert it back to a string
Pad it to 6 digits
Here's the SQL that will do that:
SELECT SUBSTR(ID, 1, 6) || SUBSTR('000000' || (SUBSTR(MAX(ID), 7)+1), -6) FROM t1;
To pad it to 6 digits, I prepend 6 zeroes, then grab the last 6 digits from the resulting string with this type of expression
SUBSTR(x, -6)
The reason why you got 1 was that your expression was grouped like this:
SELECT .... + 1
And the .... part, your string concatenation, was then attempted converted to a number, which resulted in 0, thus 0+1 gives 1.
To get the unpadded result you could've just added some parenthesis:
SELECT 'Partha' || (SUBSTR(MAX(ID),6)+1) FROM t1
^ ^
This, however, would also be wrong as it would return Partha1, and that is because SUBSTR(..., 6) grabs the 6th character and onwards and the 6th character is the final a in Partha, so to get Partha100 you would need this:
SELECT 'Partha' || (SUBSTR(MAX(ID),7)+1) FROM t1
^
In sqlite3 table we have names like as below.
Monty Python
Luther Blissett
Rey maR EsteBaN
Monty Cantsin
Geoffrey Cohen
Karen Eliot
Poor Konrad
I need a query to fetch the character from the database.
Initially I need a query that should fetch first letter of every word in a string. In above example it should display M, P, L, B, R, E, C, G, K
Suppose if a user selects C after the above query, then it should fetch next possible characters of Cantsin and Cohen i.e A and O.
Please provide any inputs on how the solution can be arrived?
Solution below would work if only first name is taken to count.
Consider data:
sqlite> SELECT name FROM COMPANY;
Paul Allen
Mark
Monty Python
Luther Blissett
Monty Cantsin
For 1st query:
sqlite> SELECT distinct substr(name,1,1) FROM COMPANY;
P
M
L
And later for 2nd output try below algorithm:
if you have 1 character input then use substr (name, 2,1)
if you have 2 characters input then use substr (name, 3,1)
.. generally : if x is number of characters input and y is number of character output expected, try substr (name, x+1, y)
sqlite> SELECT distinct substr(name,2,1) FROM COMPANY where name like 'M%';
a
o
sqlite> SELECT distinct substr(name,3,1) FROM COMPANY where name like 'Ma%';
r
If others words also need to be considered for search, replace table name with sub queries as below.
SELECT distinct substr(name,2,1) FROM
(
select name from company where name like 'A%' -- counts for first word
UNION
select name from company where name like '% A%' -- counts for words that start with space
);
Please note that above queries will only return the characters of first word, though the match is for all words it would consider first name as source to fetch characters.
Hi I have a table with 2 columns with range, so for e.g If Range Start = ABC1/000/0/0000 and Range END = ABC1/000/0/1022 .
I have to get all the values between this range and then join this with another table. Can you let me know how can I get all the values in DUAL table. I am using Oracle 11g.
Basically I need to make a list with first value as ABC1/000/0/0000 second as ABC1/000/0/0001 till ABC1/000/0/1022.
I have no idea what you mean by "storing values temporarily in DUAL". DUAL is a singe column table with a single value!
However, something like this might be what you want. If its not, then perhaps you could elaborate on your problem a little further
select blah
from another_table
where somekey in
( select blah
from table
where col between <rangeStart> and <rangeEnd>
)
So, it seems you need a few things.
Separate the "last value" from a slash-separated string, such as
ABC1/000/0/0000. It is best to do this with standard substr() and
instr() functions, not with regular expressions (for faster
execution). In instr() we can use a negative argument for
occurrence, to indicate "counting from the end of the string".
Something like this:
select range_from, substr(range_from, instr(range_from, '/', -1) + 1
from ...
Actually, you will need to convert this to a number with to_number() for further processing, and you will also need to capture the substring up to the last slash (similar use of substr() and instr(). And you will need to do the same for range_to.
Generate all the numbers from the first value to the last value. This is easily done with a connect by level query (hierarchical query). Some care must be taken since we may need to do this for several input rows (input ranges) at once.
Then put everything back together and use the result in further processing.
I will assume that the range_from string contains at least one slash, that the substring between the last slash and the end of the string represents a non-negative integer in character format, and the range_to similarly contains at least one slash and the substring from the last slash to the end of the string represents a non-negative integer. It is your responsibility to guarantee that this integer is greater than or equal to the one from range_from. In the output I will use the same substring UP TO the last slash as I find in range_from; if the requirement is that range_to must have the same initial substring, it is your responsibility to guarantee that.
I will also assume that the width (number of characters) of the "number" part (the last token in the strings) is not known beforehand and must be calculated in the query.
with
test_data( id, range_from, range_to ) as (
select 1, 'ABC1/000/0/2033', 'ABC1/000/0/2035' from dual union all
select 2, 'xyz/33/200' , 'xyz/33/200' from dual union all
select 3, '300/LMN/000' , '300/LMN/003' from dual
)
-- end of test data; SQL query begins below this line
select id, stub || lpad(to_char(from_nbr + level - 1), len, '0') as val
from (
select id, stub, length(from_str) as len, to_number(from_str) as from_nbr,
to_number(to_str) as to_nbr
from (
select id, substr(range_from, 1, instr(range_from, '/', -1)) as stub,
substr(range_from, instr(range_from, '/', -1) + 1) as from_str,
substr(range_to , instr(range_to , '/', -1) + 1) as to_str
from test_data
)
)
connect by level <= 1 + to_nbr - from_nbr
and prior id = id
and prior sys_guid() is not null
order by id, level -- if needed
;
ID VAL
-- --------------------
1 ABC1/000/0/2033
1 ABC1/000/0/2034
1 ABC1/000/0/2035
2 xyz/33/200
3 300/LMN/000
3 300/LMN/001
3 300/LMN/002
3 300/LMN/003
I am trying to create a query which groups payments into ranges (e.g. 4-, 5 - 9, 10 - 49, 50 - 99, 100 - 149, 150+).
If I try to order these by the above range they appear in alphabetical order (as you would expect).
Is it possible for me to order these by a manual list (see above range)
What's your TD release?
TD14 supports regular expressions, simply extract the first string of digits and cast it to an integer:
ORDER BY CAST(REGEXP_SUBSTR(grp, '[0-9]+') AS INTEGER)
You can use OTRANSLATE, which basically allows you to specify characters and replace them with another.
EDIT : Thanks to JNeville for setting me straight on this being ranges. The same idea still applies though, if you take his suggestion to make the last entry a range as well.
So, assuming you only have numbers, +, and -, and white space:
select
otranslate(<your column>,'+- ','')
from
<your table>
Which should return just the numeric portion of those strings. Then you should be able to cast it as an integer, and sort it.
create volatile table vt as
(select cast ('-5' as varchar(10)) as theCol)
with data
on commit preserve rows;
INSERT into vt values ('10 - 49');
INSERT INTO vt values ('50 - 99');
insert into vt
values ('150-9999');
select
cast (otranslate(theCol,'+- ','') as integer) as theNum
from
vt
order by theNum
5
1049
5099
1509999