Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
SQL
SELECT convert(varchar,DATEADD(wk,DATEDIFF(wk,0,GETDATE()),0),103)

In this query give the first day of current week . I have doubt about how the datediff() funtion works in this query . DateDiff() function returns some integer value.

Explain how the datediff() function works in this query........
Posted
Updated 5-Oct-12 2:05am
v2

Here, read about DATEDIFF: MSDN: DATEDIFF (Transact-SQL)[^]

& read about DATEADD: MSDN: DATEADD (Transact-SQL)[^]

Syntax expected:
SQL
DATEADD (datepart, number, date)
DATEDIFF (datepart, startdate, enddate)

I will let you figure out the rest.
 
Share this answer
 
Comments
Abhinav S 5-Oct-12 8:11am    
5!
Sandeep Mewara 5-Oct-12 8:19am    
Thanks. :)
SQL
DATEDIFF(wk,0,GETDATE())


this will give you the difference between current date and 1/1/1900 in weeks(wk stands for week difference)

so for today's date(oct 5, 2012) the difference is 5883 weeks .

Now when you say
SQL
DATEADD(wk,DATEDIFF(wk,0,GETDATE()),0)


this will add the weeks to date 1/1/1900 and the out put will be start date of the week 5883 i.e. '2012-10-01'

and finally when you do a convert of 103, it will display output as '01/10/2012'
 
Share this answer
 
v2
Comments
ridoy 5-Oct-12 11:24am    
good explanation..+5
sangeethamanikandan 6-Oct-12 0:20am    
Super explanation.... I realy impressed your answer.. Now i am cleard and i have the lot ideas..............

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900