Categories: Snippets

Switch case (INSERT)

INSERT INTO c_office 
            (office_code, 
             parent_code) 
SELECT DISTINCT office_code, 
                CASE 
                  WHEN Char_length (office_code) = 4 THEN 
                  Substring (office_code, 1, 2) 
                  WHEN Char_length (office_code) = 7 THEN 
                  Substring (office_code, 1, 4) 
                  WHEN Char_length (office_code) = 10 THEN 
                  Substring (office_code, 1, 7) 
                END 
FROM   office_user
SELECT 
(CASE 
  WHEN Substring (dsor_code, 1, 2) = 'MR' THEN 
  Substring (dsor_code from 3) 
  WHEN Substring (dsor_code, 1, 2) = 'KW' THEN 
  Substring (dsor_code from 3) 
ELSE
Substring (dsor_code from 1)
END)::Integer 
AS dorder, 
Substring (dsor_code, 1, 2) as dtype,
dsor_year, dsortype_code, dsor_code, dsor_description, unit_code, rate
 FROM m_dsor WHERE dsor_year=2014 and dsortype_code=5 ORDER BY  dtype, dorder

 

Recent Posts

Generate Slug URL in MySQL

A slug is a short name using human-readable keywords to identify a web page. For…

5 years ago

How to use SQL LIKE condition with multiple values in PostgreSQL?

[crayon-662f2bc36ec83823272414/]  

6 years ago

SELECT Null

[crayon-662f2bc36ed8a164136408/]  

6 years ago

ALTER Column SET NOT NULL – pgSQL

[crayon-662f2bc36eefc647551566/]  

6 years ago

ADD new Column – pgSQL

[crayon-662f2bc36f009920629533/] [crayon-662f2bc36f00f583699047/]  

6 years ago