Solved

t-sql month question

Posted on 2016-11-14
8
72 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 51

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
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.

 
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

808 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