0% found this document useful (0 votes)
13 views

Ejemplos T SQL

This document contains examples of basic T-SQL commands including declaring variables and data types, performing calculations and conversions with variables, and using date/time functions. It declares variables of different data types, sets variable values, performs calculations on variables, and runs queries using variables. Examples also demonstrate converting between data types and formats, and using date/time functions to retrieve parts of a date.

Uploaded by

Alexis VQ
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Ejemplos T SQL

This document contains examples of basic T-SQL commands including declaring variables and data types, performing calculations and conversions with variables, and using date/time functions. It declares variables of different data types, sets variable values, performs calculations on variables, and runs queries using variables. Examples also demonstrate converting between data types and formats, and using date/time functions to retrieve parts of a date.

Uploaded by

Alexis VQ
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

UNAP-EPIS GBD

Comandos básicos del T-SQL


-- variables y tipos de datos

/* tipos de datos
Texto: char, varchar, nchar, nvarchar
enteros: bigint, int, smallint, tinyint
decimal exacto: decimal, numeric
decimal aprox: real, float
moneda: money, smallmoney
Fecha: datetime, smalldatetime
Booleano: bit
*/
DECLARE @nombre varchar(50), @apellido varchar(50)
DECLARE @fechaNacimiento smalldatetime

SET @apellido = 'Arcaya'


SET @fechaNacimiento = DATEADD(WEEK, -1,GETDATE())

PRINT @fechaNacimiento

-------

DECLARE @nr int


--SET @nr =0

SET @nr = @nr +3


SET @nr = ISNULL(@nr,0) +3
--SET @nr = @nr *5

SELECT @nr AS NR

---
DECLARE @tasa tinyint
DECLARE @neto smallmoney, @bruto smallmoney

SET @tasa = 19
SET @neto = 450
SET @bruto = @neto * ( 1 + @tasa/100)

SELECT @neto AS Neto, @bruto AS bruto


----
DECLARE @tasa tinyint
DECLARE @neto smallmoney, @bruto smallmoney

SET @tasa = 19
SET @neto = 450
SET @bruto = @neto * ( 100 + @tasa)/100

SELECT @neto AS Neto, @bruto AS bruto


---

DECLARE @tasa tinyint


DECLARE @neto smallmoney, @bruto smallmoney

SET @tasa = 19

[email protected]
UNAP-EPIS GBD

SET @neto = 450


SET @bruto = @neto * ( 1 + CAST(@tasa AS real)/100)

SELECT @neto AS Neto, @bruto AS bruto


---

DECLARE @f1 float


DECLARE @d1 decimal

SET @f1 = 3.14


SET @d1 = 3.14

IF(@f1 = @d1)
PRINT 'V'
ELSE
PRINT 'F'

--select @f1,@d1
---
DECLARE @d2 decimal
DECLARE @d1 decimal

SET @d2 = 3.14


SET @d1 = 3.14

IF(@d2 = @d1)
PRINT 'V'
ELSE
PRINT 'F'
---
DECLARE @id varchar(5)
SET @id ='3'
SELECT * FROM Northwind.dbo.Employees
WHERE EmployeeID = @id
---
SELECT @@language

SET language russian

SET language english

SET language spanish

SELECT @@servername
SELECT @@version

SELECT SUBSTRING(@@version, 29, 4) AS Version

SELECT @@rowcount

------------
SELECT DATEADD(week, 3,'26.05.2014')

SELECT DATEDIFF(month, '08.01.2007','15.11.2007')

SELECT DATENAME(weekday, '26.05.2014')--día de la semana

[email protected]
UNAP-EPIS GBD

SELECT DATEPART(weekday, '15.02.2011')

SELECT DAY('26.05.2014') DIA, MONTH('20.04.2007') MES, YEAR('20.04.2011') AÑO

SELECT GETDATE()

-----
SELECT ROUND(5.129,2), ROUND(18452, -2), ROUND(1.99,1,1)

SELECT CEILING(5.129), FLOOR(5.9)


--------

SELECT ASCII('m'), CHAR(109)

SELECT CHARINDEX(' ','SQL Server')

SELECT MAX(LEN(LastName)) [El ApeNw]


from Employees

SELECT MAX(LEN(Contact.LastName)) [El ApeAdW]


from Person.Contact

SELECT LEFT('William Arcaya', CHARINDEX(' ','William Arcaya') -1)


---
select getdate()

SELECT convert(varchar, GETDATE(),112) --102 112

SELECT CONVERT(datetime, '2014.05.26',102)


SELECT CONVERT(datetime, '20140626',112)

[email protected]

You might also like