CREATE FUNCTION fnDohvatiKupceTmpTbl(@ID INT)
RETURNS @tbl TABLE
(
	Ime NVARCHAR(50),
	Prezime NVARCHAR(50)
)
AS
	BEGIN
		INSERT INTO @tbl (Ime, Prezime) 
		SELECT Ime, Prezime FROM Kupac WHERE IDKupac<@ID
		RETURN
	END

SELECT * FROM dbo.fnDohvatiKupceTmpTbl(100)

CREATE FUNCTION fnSkrati(@s NVARCHAR(MAX))
RETURNS NVARCHAR(10)
AS
	BEGIN
		RETURN CASE
			WHEN LEN(@s) <= 10 THEN @s
			ELSE SUBSTRING(@s, 1, 7) + '...'
		END
	END



SELECT r.DatumIzdavanja, dbo.fnSkrati(r.DatumIzdavanja) as Skraceno FROM Racun as r

CREATE FUNCTION fnDohvatiRacunePoDatumu(@B DATETIME, @E DATETIME)
RETURNS @tbl TABLE (
	BrojRacuna NVARCHAR(25),
	DatumIzdavanja DATETIME,
	Ime NVARCHAR(50),
	Prezime NVARCHAR(50)
)
AS
	BEGIN
		INSERT INTO @tbl (BrojRacuna, DatumIzdavanja, Ime, Prezime)
		SELECT r.BrojRacuna, r.DatumIzdavanja, k.Ime, k.Prezime FROM Racun as r
		INNER JOIN Kupac as k ON r.KupacID = k.IDKupac
		WHERE r.DatumIzdavanja BETWEEN @B AND @E

		RETURN
	END

SELECT * FROM dbo.fnDohvatiRacunePoDatumu('20040601', '20040603')

