Created
July 28, 2023 17:59
-
-
Save pedroinfo/cf57354b69b2ee40e4def6161ac464b2 to your computer and use it in GitHub Desktop.
Exemplo de CASE WHEN para Vivi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IF OBJECT_ID('tempdb..#Tb_Pessoa') IS NOT NULL | |
DROP TABLE #Tb_Pessoa | |
CREATE TABLE #Tb_Pessoa | |
( | |
Id INT, | |
Nome VARCHAR(100), | |
Estado VARCHAR(100) | |
) | |
INSERT INTO #Tb_Pessoa(Id, Nome, Estado) VALUES(1, 'Fulano 1', 'São Paulo') | |
INSERT INTO #Tb_Pessoa(Id, Nome, Estado) VALUES(2, 'Fulano 2', 'Rio de Janeiro') | |
INSERT INTO #Tb_Pessoa(Id, Nome, Estado) VALUES(3, 'Fulano 3', 'Minas Gerais') | |
INSERT INTO #Tb_Pessoa(Id, Nome, Estado) VALUES(4, 'Fulano 4', 'Espirito Santo') | |
INSERT INTO #Tb_Pessoa(Id, Nome, Estado) VALUES(5, 'Fulano 5', 'Texas') | |
SELECT Id, | |
Nome, | |
Estado, | |
CASE | |
WHEN Estado = 'São Paulo' THEN 'SP' | |
WHEN Estado = 'Rio de Janeiro' THEN 'RJ' | |
WHEN Estado LIKE '%Minas%' THEN 'MG' | |
WHEN Estado LIKE '%Santo%' THEN 'ES' | |
ELSE '??' | |
END AS UF | |
FROM #Tb_Pessoa | |
-- SQL Server |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment