question

Layo avatar image
Layo asked

is it possible to add a datepart to the alias of a summed case statement

SELECT

SUM(CASE WHEN Month(Trans_Date) = 1 THEN 1 ELSE 0 END) AS CONVERT(CHAR(4), Trans_Date, 100) + CONVERT(CHAR(4), Trans_Date, 120)

FROM table.Trans

case-statementdatepart
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
WRBI avatar image
WRBI answered

Looks like a candidate for some dynamic sql to me, although I'm not a massive fan of it.

Something like:

DECLARE @DynamicSql NVARCHAR(MAX) = '';

SELECT @DynamicSql = 'SELECT YourColumnsETC as ' + CONVERT(CHAR(4), YourColumn, 100)+
' FROM YourTable;'

EXECUTE SYS.SP_EXECUTESQL @DynamicSql 
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.