Solved

t-sql month question

Posted on 2016-11-14
8
94 Views
Last Modified: 2016-11-16
Hi Experts,

Just wondering what is the simplest way to write the following in t-sql?
if today's month is Oct or Nov or Dec or Jan or Feb or Mar then...
(meaning only execute the following line in those months)
0
Comment
Question by:bfuchs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 49

Accepted Solution

by:
PortletPaul earned 200 total points
ID: 41887183
"only execute the following line"

The question isn't clear I'm afraid... what line?

Is this a stored procedure or trigger? a cursor? (in particular: Is it already "procedural"?)

or is is a select query?

Does a case expression assist?

select ...,  case when MONTH(some_date) In (10,11,12,1,2) then .... else ... end as x
from ...
0
 
LVL 52

Assisted Solution

by:Ryan Chong
Ryan Chong earned 150 total points
ID: 41887186
try something like this:
Declare @Mth int

Set @Mth = Datepart(Month, getdate())

IF @Mth in (10, 11, 12, 1, 2, 3) 
	print 'do your stuff'
Else
	print 'skip process'

Open in new window

0
 
LVL 29

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 150 total points
ID: 41887206
Can you please explain more... What you want to do with this is not clear. You may require IF or CASE - - it depends on your operation you want.

DECLARE @M AS DATE
SET @M = GETDATE()

DECLARE @Month AS VARCHAR(3) 
SET @Month = LEFT(DATENAME(MONTH, GETDATE()),3)

IF @Month =  'Oct' or @Month = 'Nov' or @Month = 'Dec' or @Month = 'Jan' or @Month = 'Feb' or @Month = 'Mar'
BEGIN
	
		 PRINT @Month

		--EXEC 

		--SELECT/INSERT/UPDATE/DELETE 

END

Open in new window

0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 4

Author Closing Comment

by:bfuchs
ID: 41888735
Sorry for the delay in response, but this is what I needed.
Thank you!
0
 
LVL 4

Author Comment

by:bfuchs
ID: 41888938
Hi Experts,

I integrated your solution in a UDF that returns a string according to the above (meaning only if its one of those months), Perhaps you can help me figure out how to present users with a testing scenario?

If this would be running in a personal pc I would tell them to change the computers date and see if get desired results, however in a server its not possible..

Thanks,
Ben
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 41889013
Would need details of the udf....

If the function is driven by getdate () or similar system date not sure there is much we can offer.
0
 
LVL 4

Author Comment

by:bfuchs
ID: 41890416
Hi Paul,
If the function is driven by getdate ()
well this is what your guys suggested above, is there another way to accomplish this anyway?
details of the udf....
The UDF is accepting a DocDate and an empID as parameters and is suppose to return yes or no if the doc is expired or not, however user is now requesting that only in certain season of the year should it check for that.
FYI- The function is very large as it checks for all documents and rules..I only described the portion relevant.

Thanks,
Ben
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 41890698
>>"well this is what your guys suggested above"
but we don't know what you did with any of the suggestions, that's why we need to ask

getdate() is derived from the operating system that is running your mssql server

testing really needs the system date/time to be changed, anything else requires some alteration to code

e.g. using another UDF to substitute for getdate()
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question