Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Is there a shorter syntax?
Message
From
23/05/2001 22:58:48
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
00510693
Message ID:
00510704
Views:
10
>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?
>

You could use:
SELECT datepart(yyyy,convert(datetime,TB.Date)) + "/" +
       datepart(mm,convert(datetime,TB.Date)) as Date
...
If you don't need two digit months ('4' vs. '04') you could use:
SELECT year(convert(datetime,TB.Date)) + "/" +
       month(convert(datetime,TB.Date)) as Date
...
Andres M. Chiriboga, MCSE, MCP+I
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform