Date Truncation in SQL Server

Writing by shivdev 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 Server

SQL Server Quick Workaround To Declare Arrays

Writing by shivdev 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 Server

Shivdev Kalambi's Blog

Shivdev Kalambi is a Principal Software Engineer working at ArcSight. He has over 11 years' experience in software development and has worked with several technologies and played different roles and contributed to all phases of projects. Other non-tech activies include Ping-pong, Racquetball, Rock Climbing at PG and Swimming.