SQL Server
SQL RIGHT JOIN Keyword
The RIGHT JOIN keyword returns all rows from the right table (table2), with the matching rows in the left table (table1). The result is NULL in the left side when there is no match.SQL RIGHT JOIN SyntaxSELECT column_name(s) FROM table1 RIGHT JOIN table2 ON...
SQL LEFT JOIN Keyword
The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.SQL LEFT JOIN Syntax SELECT column_name(s) FROM table1 LEFT JOIN table2 ON...
SQL INNER JOIN Keyword
The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns in both tables.SQL INNER JOIN Syntax SELECT column_name(s) FROM table1 INNER JOIN table2 ON table1.column_name=table2.column_name;or: SELECT column_name(s) FROM table1 JOIN table2 ON...
Funções Concat e Format
Exemplos - Função - Concat:1 - Retornando a concatenação da Letra A e B, retornando AB:SELECT 'A' + 'B', CONCAT('A','B');2 - Retornando a concatenação dos números 2 e 40, retornando respectivamente 42 e 240:SELECT 2 + '40', CONCAT(2,40);3 - Retornando a concatenação do número 2 e a...
Verificar seo ano é bissexto (leap year)
Verificar seo ano é bissexto (leap year)declare @AnoaVerificar int declare @EAnoBissexto bitselect @AnoaVerificar=2000if ((@AnoaVerificar % 4 = 0) And (Not(@AnoaVerificar % 100 = 0))) Or (@AnoaVerificar % 400 = 0) select @EAnoBissexto = 1 else select @EAnoBissexto = 0select @EAnoBissexto
Função para tornar a primeira letra em maiúsculo (first letter capitalized - upper)
CREATE FUNCTION fn_RightCase (@Nome VARCHAR(100)) RETURNS Varchar(100) ASBEGINDECLARE @strtemp VARCHAR(100)DECLARE @i INTSET @strtemp = LOWER(@Nome)SET @strtemp = UPPER(LEFT(@strtemp,1)) +SUBSTRING(@strtemp,2,LEN(@strtemp))WHILE CHARINDEX(' ',@strtemp,1) > 0BEGINSET @i = CHARINDEX('...
Função para colocar os zeros a esquerda (left zero)
Ex: select dbo.zeros('30',4) O Resultado será "0030"create function zeros (@codigo Int, @quantidade int)returns varchar(10)asbeginreturn ( replicate('0′,(@quantidade - len(cast(@Codigo as varchar)))) + cast(@Codigo as Varchar))end
Número aleatório no T-SQL (Rand Number)
Selecionando um valor aleatório no banco de dados MS-SQL ServerO Ms-Sql Server possui uma função que retorna valores aleatórios.Esta é uma funcionalidade da função RAND().O único problema é que ela só retorna os números entre 0 e 1(Exemplo.: 0.494443).Para fazer com que ele retorne valores...
Show all columns name from object name
select l.name + ', 'from sysobjects sinner join sys.schemas c on c.schema_id = s.uidinner join syscolumns l on l.id = s.idwhere s.name like '%object_name%'
HTML Result - Sample
EXEC sp_makewebtask 'C:\Temp\test.html', 'SELECT top 10 * from sysobjectsFor more parameters:https://technet.microsoft.com/en-us/library/aa238843(v=sql.80).aspx