To get the next identity value from SQL Server

If you have table with no. of rows and you want to find out next id, following query can be used to identify:

SELECT IDENT_CURRENT(‘<Table Name>’) + IDENT_INCR(‘<Table Name>’)

You cannot reliably find out the next identity value – until you’ve actually inserted a new row into the table.

From the IDENT_CURRENT documentation:

When the IDENT_CURRENT value is NULL (because the table has never contained rows or has been truncated), the IDENT_CURRENT function returns the seed value.

Happy Coding :)

SQL

Rename SQL Table

You can rename a table in SQL Server in following way:

sp_rename OLD_TABLE_NAME, NEW_TABLE_NAME

You must require alteration permission.

Renaming table will not rename stored procedures user-defined functions or programs refer to that table. You must manually modify all object which refer renamed table.

Note: Think twice before you rename a table. If existing stored procedures user-defined functions or programs refer to that table, the name modification will give you an error.

javascript

Auto Update Copyright Year using JavaScript

Copy the script code below and paste it into your website where you want your copyright notice to appear.

<script language=”javascript” type=”text/javascript”>

document.write(‘©’ );

document.write(‘YourSiteName.com’);

document.write(new Date().getFullYear());

</script>

Happy Coding :)

SQL

Find Last Day of Previous, Current and Next Month using MSSQL

DECLARE @date DATETIME

SELECT @date = GETDATE()

SELECT CONVERT(VARCHAR(25), DATEADD(dd, – (DAY(@date)), @date), 101) AS ‘Last Day of Previous Month’

SELECT CONVERT(VARCHAR(25), DATEADD(dd, – (DAY(@date) – 1), @date), 101) AS ‘First Day of Current Month’

SELECT CONVERT(VARCHAR(25), DATEADD(dd, – (DAY(DATEADD(mm, 1, @date))), DATEADD(mm, 1, @date)), 101) AS ‘Last Day of Current Month’

SELECT CONVERT(VARCHAR(25), DATEADD(dd, – (DAY(DATEADD(mm, 1, @date)) – 1), DATEADD(mm, 1, @date)), 101) AS ‘First Day of Next Month’

SELECT DATENAME(MONTH, GETDATE()) + ‘ ‘ + DATENAME(YEAR, GETDATE())

SQL

Get a table with (months + year) between two dates using MSSQL

DECLARE @start DATE = GETDATE()
DECLARE @nth INT = 3
DECLARE @end DATE = DATEADD(MONTH, @nth, GETDATE());

WITH cte
AS (
SELECT dt = DATEADD(DAY, – (DAY(@start) – 1), @start)

UNION ALL

SELECT DATEADD(MONTH, 1, dt)
FROM cte
WHERE dt < DATEADD(DAY, – (DAY(@end) – 1), @end)
)
SELECT CONVERT(CHAR(4), dt, 100) + CONVERT(CHAR(4), dt, 120)
FROM cte

Happy Coding :)