Monday, December 22, 2014

Get End Of Month in SQL Query

There is in built function of SQL is available to get the End of the Month Date.

EOMONTH(StartDate, MonthToAdd)



Examples:

SELECT EOMONTH ( GETDATE() ) AS 'This Month';
SELECT EOMONTH ( GETDATE(), 1 ) AS 'Next Month';
SELECT EOMONTH ( GETDATE(), -1 ) AS 'Last Month';

How to Get the logged in SQL User Name

Hi Dear All,

There is small in build function of SQL to get the logged in SQL User,

              SUSER_SNAME ( [ server_user_sid ] ) 
server_user_sid (Security Identification Number) is optional parameter for the function, it can be SQL Server Login or Windows User.
If this parameter is not passed it will result the currently logged in user name.

Use:
SUSER_SNAME can be used as DEFAULT constraint for the column values.