Solved

t-sql month question

Posted on 2016-11-14
8
52 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 49

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 25

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
 
LVL 3

Author Closing Comment

by:bfuchs
ID: 41888735
Sorry for the delay in response, but this is what I needed.
Thank you!
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 3

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 3

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

863 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

24 Experts available now in Live!

Get 1:1 Help Now