Solved

t-sql month question

Posted on 2016-11-14
8
62 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
8 Comments
 
LVL 48

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 50

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 28

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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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 48

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 48

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now