SQL "Select" stripping needed dash character from value - sql

I have a basic SELECT statement that looks like this:
Select Distinct currency.denomination From currency
Some values in my Currency table contain ranges, such as 10-50 but instead in these particular instances the query returns 1050 instead of the stored value range of 10-50.
This is just one example, but all of the values that store the dash are being returned as one big number instead of the range.
What can I do to ensure the Select statement returns the dash which is stored, and needed for my use case?

Related

PostgreSQL: return row where any column value like variable

I am trying to have the user search for a value in a SQL table, and the user is returned with any row that contains that value. At the moment, I can make it work such that the code is:
SELECT * FROM table WHERE lower('foo') in (lower('col1'),lower('col2'),etc)
However, I would like it to be able to search every column and return any row LIKE 'foo'. For instance,
SELECT * FROM table WHERE (lower('col1'), lower('col2'), etc) like lower('%foo%')
But that doesn't work.
Any suggestions?
I believe you need to use multiple WHERE clauses instead of grouping them all into one statement. Try this:
SELECT * FROM table
WHERE lower(col1) like lower('%foo%')
OR lower(col2) like lower('%foo%')
OR etc like lower('%foo%')
You can convert the whole row to a string and then use LIKE on the result of that:
select *
from the_table
where lower(the_table::text) like '%foo%';
the_table::text returns all columns of each row as a comma separated list enclosed with parentheses, e.g. (42,Arthur,Dent). So the above is not 100 identical to a LIKE condition applied on each column - but probably does what you want.

SQL - just view the description for explanation

I would like to ask if it is possible to do this:
For example the search string is '009' -> (consider the digits as string)
is it possible to have a query that will return any occurrences of this on the database not considering the order.
for this example it will return
'009'
'090'
'900'
given these exists on the database. thanks!!!!
Use the Like operator.
For Example :-
SELECT Marks FROM Report WHERE Marks LIKE '%009%' OR '%090%' OR '%900%'
Split the string into individual characters, select all rows containing the first character and put them in a temporary table, then select all rows from the temporary table that contain the second character and put these in a temporary table, then select all rows from that temporary table that contain the third character.
Of course, there are probably many ways to optimize this, but I see no reason why it would not be possible to make a query like that work.
It can not be achieved in a straight forward way as there is no sort() function for a particular value like there is lower(), upper() functions.
But there is some workarounds like -
Suppose you are running query for COL A, maintain another column SORTED_A where from application level you keep the sorted value of COL A
Then when you execute query - sort the searchToken and run select query with matching sorted searchToken with the SORTED_A column

Extract alphanumeric value from varchar column

I have a table which contains a column having alphanumeric values which is stored as a string. I have multiple values in that column having values such as F4737, 00Y778, PP0098, XXYYYZ etc.
I want to extract values starting with a series of F and must have numeric values in that row.
Alphanumeric column is the unique column having unique values but the rest of the columns contain duplicate values in my table.
Futhermore, once these values are extracted I would like to pick up the max value from the duplicate row,for eg:
Suppose I have F4737 and F4700 as a unique Alphanumeric row, then F4737 must be extracted from it.
I have written a query like this but the numeric values are not getting extracted from this query:
select max(Alplanumeric)
from Customers
where Alplanumeric '%[F0-9]%
or
select max(Alplanumeric)
from Customers
where Alplanumeric like '%[0-9]%'
and Alplanumeric like 'F%'**
I run the above query but I am only getting the F series if I remove the numeric part from the above query. How do I extract both, the F starting series as well as the numeric values included in that row?
Going out on a limb, you might be looking for a query like this:
SELECT *, substring(alphanumeric, '^F(\d+)')::int AS nr
FROM customers
WHERE alphanumeric ~ '^F\d+'
ORDER BY nr DESC NULLS LAST
, alphanumeric
LIMIT 1;
The WHERE conditions is a regular expression match, the expression is anchored to the start, so it can use an index. Ideally:
CREATE INDEX customers_alphanumeric_pattern_ops_idx ON customers
(alphanumeric text_pattern_ops);
This returns the one row with the highest (extracted) numeric value in alphanumeric among rows starting with 'F' followed by one ore more digits.
About the index:
PostgreSQL LIKE query performance variations
About pattern matching:
Pattern matching with LIKE, SIMILAR TO or regular expressions in PostgreSQL
Ideally, you should store the leading text and the following numeric value in separate columns to make this more efficient. You don't necessarily need more tables like has been suggested.

Delete rows if letters in cell SQL Server

I needed some guidance as to how I can exlude rows that contains any form of letter in a cell that is supposed to contain integers only.
This is what the data looks like now:
CustomerID
----------
ea176680
27906857
62675436
62566247
1bed413e
61110053
61113323
34441416
AS you can see in the table, the first,ID contains letters. How can I remove complete rows if a cell were to contain any form of letters?
How about this where clause?
where CustomerId not like '%[^0-9]%'
It filters out any values that have a non-digit. Unfortunately, SQL Server doesn't support regular expressions, but this will do what you want.
If you specifically wanted to look for letters, you can do:
where CustomerId like '%[a-zA-Z]%'
SQL Server can match on a specific pattern in your statement using like. It looks somewhat like regex, but isn't exactly that.
Use this statement if you want to match on all CustomerIds that contains characters in the range A-Z and a-z:
select *
from table
where CustomerID like '%[A-za-z]%'

SQL: Update column value conditioned on same column value

On a SQLite database something I thought was very simple doesn't work at least under my conditions.
I have one column with names and some name contains apostrophe ('), which I want to remove. I know all names which contains an apostrophe, so I am not trying to query for apostrophes. I am doing something much simpler:
UPDATE table SET column_name="name surname1 surname2" WHERE column_name="name surname1'surname2";
which doesn't return what I expect. It doesn't produce an error but it doesn't modify any record.
SQL doesn't like reflexivity?
There should be no issue with querying the current value of a column to update the same column. Try escaping the single-quote by doubling it e.g. ''.
See: http://www.sqlite.org/lang_expr.html which reads:
A string constant is formed by enclosing the string in single quotes
('). A single quote within the string can be encoded by putting two
single quotes in a row - as in Pascal.
Therefore, your update should be:
UPDATE table SET column_name='name surname1 surname2' WHERE column_name='name surname1''surname2'
Update: Added explanation of escape mechanism.