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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Date question

Author  Topic 

kfluffie
Posting Yak Master

103 Posts

Posted - 2011-10-22 : 03:33:46
Hi,
I have one column, tidpunkt, with a date (2011/10/22 for example) and I have a procedure which are doing some stuff.

In this procedure I have a year and a month variable and I want to, based on the column tidpunkt modify some data.

How do you check that the column tidpunkt is within the range of the two variables?

Variable @startYear = 2011, @startMonth = 10
Tidpunkt = 2010/10/13

I am not interested in the day and I want the day to be ignored so I can do a equal check on the year and month.

Best regards,
KF

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-22 : 04:38:37
[code]
declare @startYear int = 2011, @startMonth int = 10

SELECT DATEADD(mm,@startMonth-1,DATEADD(yy,@startyear-1900,0)) AS PeriodStartDate,DATEADD(mm,@startMonth,DATEADD(yy,@startyear-1900,0)) AS NextPeriodStartDate

SELECT *
FROM table
WHERE Tidpunkt>=DATEADD(mm,@startMonth-1,DATEADD(yy,@startyear-1900,0))
AND Tidpunkt<DATEADD(mm,@startMonth,DATEADD(yy,@startyear-1900,0))
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

kfluffie
Posting Yak Master

103 Posts

Posted - 2011-10-22 : 06:48:29
Thanks Visakh!
I just ended up using DATEPART. I just forgot about it and it seems very convenient to use it here!

Best regards,
KF
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-22 : 06:56:11
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -