Date Truncation in SQL Server

Writing by on Tuesday, 25 of March , 2008 at 11:47 am

Need to trunc dates in SQL Server? Follow the examples below.

SELECT DATEADD(HOUR, DATEDIFF(HOUR, 0, GETDATE()), 0)   -- 2008-03-25 10:00:00.000
SELECT DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)     -- 2008-03-25 00:00:00.000
SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) -- 2008-03-01 00:00:00.000
SELECT DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0)   -- 2008-01-01 00:00:00.000

Leave a comment

Category: SQL DB

SQL Server Quick Workaround To Declare Arrays

Writing by on Tuesday, 18 of March , 2008 at 5:11 pm

Need a quick workaround to declaring arrays in SQL Server?

-- Trying to accomplish something like this?
int[] @list = {1864, 1966} -- INCORRECT SQL 
SELECT * FROM Employees
WHERE id IN @list

Here’s a workaround

DECLARE @list TABLE (id int)          -- create an int[] "array"
INSERT @list VALUES (1864)            -- hardcode values
INSERT @list VALUES (1966)            -- hardcode values
SELECT * FROM Employees               -- my query
WHERE id IN (SELECT id FROM @list)    -- use the int[] array

Leave a comment

Category: SQL DB

Shivdev Kalambi's Blog

Shivdev Kalambi is a Software Development Manager, previously a Principal Software Engineer at ArcSight/HP. With over 16 years' experience in software development, he's worked on several technologies and played different roles and contributed to all phases of projects. Non-tech activies include Ping-pong, Rock Climbing and Yoga at PG, Golf, Skiing, Swimming & a beer enthusiast.