Solved

t-sql month question

Posted on 2016-11-14
8
40 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
Comment Utility
"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
Comment Utility
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 17

Assisted Solution

by:Pawan Kumar Khowal
Pawan Kumar Khowal earned 150 total points
Comment Utility
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
Comment Utility
Sorry for the delay in response, but this is what I needed.
Thank you!
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Author Comment

by:bfuchs
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>>"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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
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
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

771 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

11 Experts available now in Live!

Get 1:1 Help Now