SQL Nested Case When with numeric check - sql

I'm trying to create a "filter" with SQL.
I have a table named ARTICLE like this :
CODE | NAME | QUANTITY |
_______________|_________________|_______________
0020717270084 | MANGO FRUIT 1L | 3 |
0884394000774 | ALOE VERA 50CL | 4 |
16 | CHEWING GUM | 10 |
IGLOO | IGLOO ICE | 5 |
I want to do a SELECT with a verification on CODE.
If CODE is a number AND has a length of 8 OR 10 OR 13 digits, i display CODE ELSE i have to display * before CODE (simple concat).
I can do CASE WHEN but this one is a little bit tricky for me.
Thanks for your help.

You would do this with a case statement. Databases do the checks differently. The following is an approach using SQL Server:
select (case when len(code) in (8, 10, 13) and code not like '%[^0-9]%'
then code
else '*' + code
end)

Related

bit varying in Postgres to be queried by sub-string pattern

The following Postgres table contains some sample content where the binary data is stored as bit varying (https://www.postgresql.org/docs/10/datatype-bit.html):
ID | Binary data
----------------------
1 | 01110
2 | 0111
3 | 011
4 | 01
5 | 0
6 | 00011
7 | 0001
8 | 000
9 | 00
10 | 0
11 | 110
12 | 11
13 | 1
Q: Is there any query (either native SQL or as Postgres function) to return all rows where the binary data field is equal to all sub-strings of the target bit array. To make it more clear lets look at the example search value 01101:
01101 -> no result
0110 -> no result
011 -> 3
01 -> 4
0 -> 5, 10
The result returned should contain the rows: 3, 4, 5 and 10.
Edit:
The working query is (thanks to Laurenz Albe):
SELECT * FROM table WHERE '01101' LIKE (table.binary_data::text || '%')
Furthermore I found this discussion about Postgres bit with fixed size vs bit varying helpful:
PostgreSQL Bitwise operators with bit varying "cannot AND bit strings of different sizes"
How about
WHERE '01101' LIKE (col2::text || '%')
I think you are looking for bitwise and:
where col2 & B'01101' = col2

Build SQL query that summarizes total values for different categories and varying criteria

I'm using PostgreSQL 9.6 and I am trying to create a single query on one table that can summarize a category's total area in different sites.
Below is a simplified version of my table.
Id | category (text) | area (real) | site (text)
1 | short grass 1 | 10 | park 1
2 | short grass 2 | 15 | park 2
3 | long grass 1 | 25 | park 1
4 | long grass 2 | 18 | park 3
5 | short grass 1 | 7 | roadside 1
6 | short grass 2 | 4 | roadside 2
7 | long grass 1 | 14 | roadside 1
8 | long grass 2 | 7 | roadside 3
Id is the standard primary key setup
Category identifies different lengths of grass (reduced for example)
Area is a decimal figure for the area of the grass
Site is the location of the grass
My finished table that I'm aiming for is the total area categorized by the grass category (column 2) and the site (column 4):
site (text) | short grass | long grass
total area total area
park (grouped) | 25 | 43
roadside (grouped) | 11 | 21
I thought I had some success using a CASE WHEN statement inside the SUM function, but instead of creating multiple rows with area totals based on varying criteria, it just added them together inside the single SUM, which in hindsight I realize is what I was asking it to do. (a brief example of my code below):
SELECT SUM(CASE WHEN category LIKE 'short grass%' AND site LIKE 'park%' THEN area
WHEN category LIKE 'long grass%' AND site LIKE 'park%' THEN area END)
from my_table;
What I was expecting was two rows with the different area totals. This is as far I've got with my code and I don't think I'm even going in the right direction with it.
I've considered nested SELECT queries and joins but my SQL knowledge isn't that great so I wouldn't know where to start to write it.
I think this is what you are trying to do.
SELECT case when site like 'park%' then 'park'
when site like 'roadside%' then 'roadside'
--add else if required
end as site,
SUM(CASE WHEN category LIKE 'short grass%' THEN area ELSE 0 END) as short_grass
SUM(CASE WHEN category LIKE 'long grass%' THEN area ELSE 0 END) as long_grass
from my_table
group by case when site like 'park%' then 'park'
when site like 'roadside%' then 'roadside'
--add else if required
end
How do you define the groups? If you just want the first word from site, then:
select substring(site from '#"%#" %' for '#') as sitetype,
sum(case when category like 'short grass%' then area else 0 end) as shortgrass,
sum(case when category like 'long grass%' then area else 0 end) as longgrass
from my_table
group by sitetype;

SQL - Exclude four-digit-number

I have an SQL-Server with and simple table on it.
ID | Code
---+--------
1 | 1234
2 | TEST
3 | 12556
4 | TEST1
5 | 5678
6 | WART
I want to exclude all four-digit-number. In my case that would be 1234 and 5678.
I know I can use ISNUMERIC() tp check if code is numeric.
I also know I can use:
SELECT * FROM Codes WHERE code NOT LIKE '____';
to check if my value has four digits, but i dont get it how to combine them.
Any suggentions?
Thanks in advance!
Just use not like:
where code not like '[0-9][0-9][0-9][0-9]'

Multiples of a number oracle SQL

How do you check if the content of a field is a multiple of 2 in oracle sql
n_id | n_content
-------------------------
1 | Balloon
2 | Drill
3 | Cup
4 | Bottle
5 | Pencil
6 | Ball
I have tried:
select*from num_w where (n_id%2>0);
and also
select*from num_w where n_id%2=0;
Neither of these worked
Wasn't this multiple of 5 a few seconds ago? :)
Anyway, in Oracle I believe the query would be:
select * from num_w where MOD(n_id,2) = 0;

parse multiline varchar field into rows (T-sql)

I am making a report in SSRS. Database contains table "Project" with a "Notes" field which is formated by users in this way:
#Completed
-line 1 description
-line 2 description
-line3 and so on
#Planned
-line 1 etc.
#Risks
- line1 ...etc
There are always only those 3 categories and in that order. Bullet points can be from 0 to unlimited (but i never seen more than 10)
I would like to get output(dataset) in format (so I can group them in tablix):
ProjectID, Maincategory, itemID, subcategories.
For example
1 | Completed | 1 | line1
1 | Completed | 2 | line2
1 | Completed | 3 | line3
...
1 | Planned | 1 | Line1
...
1 | Risks | 1 | line1
...
I cant change source DB so I cant create stored procedure, it should be regular query.
I looked at various solutions with CTE recursions but I just cant figure out how they work in oreder to change them for my case.
Thank you!