Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Is there a shorter syntax?
Message
 
À
Tous
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Titre:
Is there a shorter syntax?
Divers
Thread ID:
00510693
Message ID:
00510693
Vues:
53
I have written a query and I am pretty sure that there is a shorter syntax for that.

I want to extract and format and group by the year and the month included in a datetime field.

For now, I have this:
SELECT CAST(YEAR(TB.Date) AS CHAR(4)) + '/' + REPLICATE('0',2-LEN(MONTH(TB.Date))) + CAST(Month(TB.Date) AS CHAR(2)) AS Date,
Is there a shorter way of writing it?



The complete query is:
SELECT CAST(YEAR(TB.Date) AS CHAR(4)) + '/' + REPLICATE('0',2-LEN(MONTH(TB.Date))) + CAST(Month(TB.Date) AS CHAR(2)) AS Date, 
	SH.Symbol, SH.Cusip, 	 
       SUM(TB.Shares) AS Shares, SUM(TB.Amount) AS Amount, AVG(TB.Amount / TB.Shares) AS Average
FROM TrxBuy AS TB 
INNER JOIN StockHdr AS SH 
ON SH.StockID = TB.StockID 
WHERE TB.Date BETWEEN '2001-04-01' AND '2001-04-30' 
AND TB.PortFolioID = 13 
GROUP BY CAST(YEAR(TB.Date) AS CHAR(4)) + '/' + REPLICATE('0',2-LEN(MONTH(TB.Date))) + CAST(Month(TB.Date) AS CHAR(2)), Symbol, Cusip
ORDER BY Date, Symbol
Éric Moreau, MCPD, Visual Developer - Visual Basic MVP
Conseiller Principal / Senior Consultant
Moer inc.
http://www.emoreau.com
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform