'strftime' is not a recognized built-in fu

2019-03-02 13:19发布

问题:

I am using Microsoft SQL Database Management Studio and it will not allow me to use the strftime() function to run a query. I have to create a table by months with new users and unsubscribers for each month.

This is what I had essentially which creates the error:

SELECT strftime('%m', createddate) AS 'Month', COUNT(createddate) AS 'Subscribers',
       COUNT(dateunsubscribed) AS 'UNsubscribers'
FROM subscriber
GROUP BY 1
ORDER BY 1;

how else could I run this query without strftime() or how can I get strftime() to work?

回答1:

strftime is a mysql function, and isn't available in Microsoft's sql-server.

For this simple usecase (extracting a month from a date), you could user month:

SELECT   MONTH(createddate) AS [Month], 
         COUNT(createddate) AS [Subscribers],
         COUNT(dateunsubscribed) AS [UNsubscribers]
FROM     subscriber
GROUP BY 1
ORDER BY 1;

EDIT:
To address the question in the comment, the group by clause doesn't take an ordinal like the order by clause does. You'll need to specify the expression you want to group by:

SELECT   MONTH(createddate) AS [Month], 
         COUNT(createddate) AS [Subscribers],
         COUNT(dateunsubscribed) AS [UNsubscribers]
FROM     subscriber
GROUP BY 1
ORDER BY MONTH(createddate);