I need similar function of strtotime( "next monday" )
in SQL Server.
Asked
Active
Viewed 750 times
1

halfer
- 19,824
- 17
- 99
- 186

Kanak Vaghela
- 7,750
- 10
- 32
- 37
-
1Voted to close this question as this is somewhat related to http://stackoverflow.com/questions/5420988/strtotime-equivalent-in-net. In this case, it will require it making a CLR function. – shahkalpesh Apr 27 '11 at 11:40
-
Yes which is the quivalent in .NET? – Kanak Vaghela Apr 27 '11 at 11:41
-
1Because this function relies on regex (among other things), it is better to write it in a programming language (such as c#, php). SQL Server 2005 supports CLR functions, to which this code can be ported. – shahkalpesh Apr 27 '11 at 11:43
-
But I want this function is sql server... – Kanak Vaghela Apr 27 '11 at 11:47
-
1@Kanak, shahkalpesh means that you can write a DLL in .NET to do the conversion, then add it to SQL Server as a CLR assembly (see CREATE ASSEMBLY etc. in the documentation) and call it from CLR functions or procedures. So you write the code in .NET but you call it from TSQL. Google "Creating CLR Stored Procedures" for more details, if this doesn't sound familiar. – Pondlife Apr 27 '11 at 12:14
-
1@shahkalpesh: "Somewhat related to" doesn't make it a duplicate, and .NET has nothing to do with the answer. It's possible in straight TSQL without .NET/CLR at all. – Ken White May 05 '11 at 01:38
1 Answers
3
DateAdd(d, (8+@@DateFirst+DatePart(dw,GetDate()))%7, GetDate())
This gives you next Monday, at the current time-of-day; i.e. if you use it at 11:49 AM Wednesday, you'll get next Monday at 11:49 AM. If that's not what you want, look up functions DatePart, DateAdd, and DateDiff - you should be able to adjust it.

Allan W
- 580
- 1
- 3
- 8
-
+1. Nice answer - tested in SSMS, and it works perfectly as described. – Ken White May 05 '11 at 01:35