Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Is there a shorter syntax?
Message
From
23/05/2001 22:31:07
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
00510693
Message ID:
00510695
Views:
16
Hey, whatever works... But, you may want to look at datepart and convert...

SELECT left(convert(datetime,TB.Date,111),7) as Date, ...
WHERE ...
GROUP BY datepart(yyyy, TB.date), datepart(mm, TB.date)

>>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
>
>
>I want the output to be yyyy/mm.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform