Can we use a CASE...WHEN inside SUBSTRING?

Harry Lime

(Here's a much simpler case of my actual conundrum at work)

Let's say I have a table, called 'a', with a column named 'col' with the following values (say a column of length 2 with many random combination of characters):

col
A3 
D2 
@5
#)
...

I want to use a select statement that outputs two columns called 'letter' and 'number' where 'letter' is the first character of 'col' & 'number' is the second character of 'col', but with the following mapping:

If substring(col FROM 1 for 1) in ('!','@','#'),
    then letter = 'A' and 'number' = substring(col FROM 2 for 1)

(i.e., if the first character of something in 'col' is '!', '@', or '#', map it to 'letter' as 'A' while keeping the second character of 'col' the same and mapping that value to 'number')

If col = '%9', 
    then 'letter' = 'H' and 'number' = '9'

(i.e., if a specific value in 'col' is '%9', then map it to 'letter' as 'H' and 'number' as '9')

If substring(col FROM 2 for 1) = '4',
    then 'letter' = substring(col FROM 1 for 1) and 'number' = '7'

(i.e., if the second character of a value in 'col' is '4', leave the first character unchanged and map it to 'letter' and map the second character to 'number' as '7')

Lastly, only select values where 'letter' is a letter and 'number' is a one character number. That is,

'letter' in ('A','B',...'Z') and 'number' in ('0','1',...'9')

What query would I run to solve this? That is, with the mapping hardcoded using CASE..WHEN?

Ideally, I'm trying to do something like:

SELECT substring((Case...When) FROM 1 for 1) AS letter,
substring((Case...When) FROM 2 for 1) AS number
FROM a;

Thanks!

rs.

Try this

SELECT * FROM (
SELECT
CASE 
  WHEN SUBSTRING(COL from 1 for 1) IN ('!','@','#') THEN 'A'
  WHEN COL LIKE '%9' THEN 'H'
  WHEN SUBSTRING(COL,2,1) = '4' THEN SUBSTRING(COL,1)
END Letter,
CASE 
  WHEN SUBSTRING(COL from 1 for 1) IN ('!','@','#') THEN substring(col FROM 2 for 1)
  WHEN COL LIKE '%9' THEN '9'
  WHEN SUBSTRING(COL,2,1) = '4' THEN '7'
END Number
FROM A
) 
WHERE (Letter ~ '^[A-Za-z]$') = true AND (Number ~ '^[0-9]$') = true;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can we use a CASE in BETWEEN

From Dev

When can we use async?

From Dev

Can we use quantifiers inside a lookbehind expression?

From Dev

Can we use UmlStateMachineModelFactory inside a StateMachineBuilder

From Dev

Can we use '&&' logic operator inside of the for?

From Dev

Can I use a case inside another case in a Switch Case in C

From Dev

Can we have label inside switch case java

From Dev

Is it possible to use a process inside a 'case is when' structure?

From Dev

How can I use case_when inside dplyr mutate() function?

From Dev

can we use empty parameter with pointer and when?

From Dev

How to/Can we apply a "limit" when we use replication in Cloudant?

From Dev

Can we use camel case in java package naming?

From Dev

According to the Use case <<view reports>>, can we say that the diagram is correct?

From Dev

When do we include an actor in a use case scenario

From Dev

Can we call a "case" inside another case in the same switch statement in Java?

From Dev

Can we use ng-transclude inside $compile?

From Dev

Can we use PHP inbuilt function inside Twig File in Symfony

From Dev

can we use html button onclick event inside php?

From Dev

Can we use PHP inbuilt function inside Twig File in Symfony

From Dev

can we use php tags inside html attributes?

From Dev

Can we use variable in ng-init inside controller?

From Dev

Embedded system Can we use any function inside ISR?

From Dev

when use type inference? we can always use VAR?

From Dev

Can i use user input statement inside switch case statement?

From Dev

How to use CASE WHEN THEN inside aggregated SELECT with GROUP BY?

From Dev

SQL Case inside WHEN

From Dev

Why do we use UserManager to acess user profile info when we can use application dbcontext?

From Dev

Why do we use pthread_exit() when we can use return?

From Dev

Can we use AmqpItemReader and AmqpItermWriter for request/reply use case in spring batch?

Related Related

  1. 1

    Can we use a CASE in BETWEEN

  2. 2

    When can we use async?

  3. 3

    Can we use quantifiers inside a lookbehind expression?

  4. 4

    Can we use UmlStateMachineModelFactory inside a StateMachineBuilder

  5. 5

    Can we use '&&' logic operator inside of the for?

  6. 6

    Can I use a case inside another case in a Switch Case in C

  7. 7

    Can we have label inside switch case java

  8. 8

    Is it possible to use a process inside a 'case is when' structure?

  9. 9

    How can I use case_when inside dplyr mutate() function?

  10. 10

    can we use empty parameter with pointer and when?

  11. 11

    How to/Can we apply a "limit" when we use replication in Cloudant?

  12. 12

    Can we use camel case in java package naming?

  13. 13

    According to the Use case <<view reports>>, can we say that the diagram is correct?

  14. 14

    When do we include an actor in a use case scenario

  15. 15

    Can we call a "case" inside another case in the same switch statement in Java?

  16. 16

    Can we use ng-transclude inside $compile?

  17. 17

    Can we use PHP inbuilt function inside Twig File in Symfony

  18. 18

    can we use html button onclick event inside php?

  19. 19

    Can we use PHP inbuilt function inside Twig File in Symfony

  20. 20

    can we use php tags inside html attributes?

  21. 21

    Can we use variable in ng-init inside controller?

  22. 22

    Embedded system Can we use any function inside ISR?

  23. 23

    when use type inference? we can always use VAR?

  24. 24

    Can i use user input statement inside switch case statement?

  25. 25

    How to use CASE WHEN THEN inside aggregated SELECT with GROUP BY?

  26. 26

    SQL Case inside WHEN

  27. 27

    Why do we use UserManager to acess user profile info when we can use application dbcontext?

  28. 28

    Why do we use pthread_exit() when we can use return?

  29. 29

    Can we use AmqpItemReader and AmqpItermWriter for request/reply use case in spring batch?

HotTag

Archive