Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 date logic

Author  Topic 

govindts
Starting Member

33 Posts

Posted - 2009-02-13 : 12:40:10
Hello - I have below query in sqlserver...

SELECT
cast(cast(year(getdate()) as varchar(4)) + '-' + cast((((month(getdate())-1) / 3) * 3) +1 as varchar(2)) + '-1' as datetime)


The output is 2009-01-01 00:00:00.000

I believe, output of the above query is first day of the current quarter.. Is this correct?? Please correct me if i am wrong..


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-13 : 12:46:58
its first day of first quarter as well as first day of year. which one is you interested in?

also try this

SELECT DATEADD(qq,DATEDIFF(qq,0,GETDATE()),0)
Go to Top of Page

govindts
Starting Member

33 Posts

Posted - 2009-02-13 : 12:54:14
It is not always First day of the first quarter... It is first day of the current quarter...

I changed the getdate() to getdate()+50, getdate+200, getdate()+70 etc.. and ran the query.. I see first day of the current quarter... I just want to confirm this before i move this code to Oracle database... Thanks

quote:
Originally posted by visakh16

its first day of first quarter as well as first day of year. which one is you interested in?

also try this

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

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-13 : 13:04:49
quote:
Originally posted by govindts

It is not always First day of the first quarter... It is first day of the current quarter...

I changed the getdate() to getdate()+50, getdate+200, getdate()+70 etc.. and ran the query.. I see first day of the current quarter... I just want to confirm this before i move this code to Oracle database... Thanks

quote:
Originally posted by visakh16

its first day of first quarter as well as first day of year. which one is you interested in?

also try this

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




the query i gave you gives first date of current quarter in which you're in. what are you actually looking for? always the first day of first quarter?
Go to Top of Page
   

- Advertisement -