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)
 Dynamic Year

Author  Topic 

Villanuev
Constraint Violating Yak Guru

478 Posts

Posted - 2013-11-25 : 01:44:43
Hi Guys,

I need to make the year dynamically in my query.
Or kindly please give me other idea on how to integrate this in my query. Initially, I place a hard coded dates in my query. your help is very much appreciated. thanks.


Create table #sample
(Itemid nvarchar(1),ConfirmedDate datetime)
go
insert into #sample(itemid,ConfirmedDate) values ('A','2013-11-25 00:00:00.000')
insert into #sample(itemid,ConfirmedDate) values ('A','2013-11-27 00:00:00.000')
insert into #sample(itemid,ConfirmedDate) values ( 'A','2013-11-28 00:00:00.000')
insert into #sample(itemid,ConfirmedDate) values ('A','2013-12-01 00:00:00.000')
insert into #sample(itemid,ConfirmedDate) values ('B','2013-12-24 00:00:00.000')
insert into #sample(itemid,ConfirmedDate) values ('B','2013-12-26 00:00:00.000')
insert into #sample(itemid,ConfirmedDate) values ('B','2014-01-01 00:00:00.000')
insert into #sample(itemid,ConfirmedDate) values ('B','2014-01-08 00:00:00.000')
insert into #sample(itemid,ConfirmedDate) values ('C','2014-01-25 00:00:00.000')
insert into #sample(itemid,ConfirmedDate) values ('C','2014-01-28 00:00:00.000')
insert into #sample(itemid,ConfirmedDate) values ('C','2014-02-20 00:00:00.000')
insert into #sample(itemid,ConfirmedDate) values ('C','2014-02-24 00:00:00.000')
insert into #sample(itemid,ConfirmedDate) values ('D','2014-02-28 00:00:00.000')
insert into #sample(itemid,ConfirmedDate) values ('D','2014-03-01 00:00:00.000')
insert into #sample(itemid,ConfirmedDate) values ('D','2014-03-25 00:00:00.000')
insert into #sample(itemid,ConfirmedDate) values ('D','2014-03-28 00:00:00.000')
insert into #sample(itemid,ConfirmedDate) values ('D','2099-01-01 00:00:00.000')

select
Itemid
,ConfirmedDate
,Case When ConfirmedDate between('11/26/2013') AND('12/25/2013') THEN 'DEC ETA'
When ConfirmedDate between('12/26/2013') AND('1/25/2014') THEN 'JAN ETA'
When ConfirmedDate between('1/26/2014') AND('2/25/2014') THEN 'FEB ETA'
When ConfirmedDate between('2/26/2014') AND('3/25/2014') THEN 'MAR ETA'
When ConfirmedDate between('3/26/2014') AND('4/25/2014') THEN 'APR ETA'
Else 'NO ETA' END AS Remarks
From #sample

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-25 : 02:18:30
use like

..
Case When ConfirmedDate between DATEADD(mm,DATEDIFF(mm,0,GETDATE())+1,-4) AND DATEADD(mm,DATEDIFF(mm,0,GETDATE())+2,-5) THEN 'DEC ETA'
When ConfirmedDate between DATEADD(mm,DATEDIFF(mm,0,GETDATE())+2,-4) AND DATEADD(mm,DATEDIFF(mm,0,GETDATE())+3,-5) THEN 'JAN ETA'
...


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Villanuev
Constraint Violating Yak Guru

478 Posts

Posted - 2013-11-25 : 04:13:16
Thank you very much Visakh for your prompt reply.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-25 : 05:13:22
you're welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Villanuev
Constraint Violating Yak Guru

478 Posts

Posted - 2013-11-25 : 21:17:52
Hi Visakh, did you know any forum for excel ( Macro issue) to help me. thanks.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-26 : 05:30:51
quote:
Originally posted by Villanuev

Hi Visakh, did you know any forum for excel ( Macro issue) to help me. thanks.


this?
http://social.msdn.microsoft.com/Forums/office/en-US/home?forum=exceldev

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Villanuev
Constraint Violating Yak Guru

478 Posts

Posted - 2013-11-26 : 20:08:34
Thanks Visakh.
Go to Top of Page

Prav4u
Starting Member

15 Posts

Posted - 2013-11-27 : 02:36:25
Hi,
You can also try below forum
[url]http://www.mrexcel.com/forum[/url]



Praveen D'sa
MCITP - Database Administrator 2008
http://sqlerrors.wordpress.com
Go to Top of Page
   

- Advertisement -