Useful SQL Snippets

Returns first day of a week (Most recent Monday)

SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0)

Returns Date of Previous Sunday

SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), -1)

To get midnight of a timestamp

DECLARE @dtTimestamp DATETIME = '2017-10-12 15:26:00.000'
SET @dtTimestamp = DATEADD(DAY, DATEDIFF(DAY, 0, @dtTimestamp + 1), 0)
PRINT @dtTimeStamp

To concatenate an INT and Varchar

SET ConcatString = ('Name' + CAST(ID AS VARCHAR)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s