TipsOnLips.net

Your .net tips and tricks source

About the author

Author Name is someone.
E-mail me Send mail

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010


Some useful date processing SQL Snippets

---Calculates the first day of the previous month

SELECT DATEADD(mm, DATEDIFF(m, 0, GETDATE()) - 1, 0)

AS [First day of the previous month]

---Calculates the first day of current month

SELECT DATEADD(mm, DATEDIFF(m, 0, GETDATE()), 0)

AS [First day of the current month] [more]

---Calculates the first day of next month

SELECT DATEADD(mm, DATEDIFF(m, 0, GETDATE()) + 1, 0)

AS [First day of the next month]

---Calculates the last day of the previous month

SELECT DATEADD(d, -1, DATEADD(mm, DATEDIFF(m, 0, GETDATE()), 0))

AS [Last day of the previous month]

---Calculates the last day of the current month

SELECT DATEADD(d, -1, DATEADD(mm, DATEDIFF(m, 0, GETDATE()) + 1, 0))

AS [Last day of the current month]

--Calculates the last day of the next month

SELECT DATEADD(d, -1, DATEADD(mm, DATEDIFF(m, 0, GETDATE()) + 2, 0))

AS [Last day of the next month]

--Calculates the first day of the year

SELECT DATEADD(yy, DATEDIFF(yy,0,GetDate()), 0)

AS [First day of the year]

--Calculates the first day of the quater

SELECT DATEADD(qq, DATEDIFF(qq,0,GetDate()), 0)

AS [First day of the quarter]

--Calculates the first monday of the month

SELECT DATEADD(wk, DATEDIFF(wk,0,dateadd(dd, 6 - DATEPART(Day,GetDate()),GetDate())), 0)

AS [First monday of the month]

--Calculates the last day of the prior month

SELECT DATEADD(mm, DATEDIFF(mm,0,GetDate()), 0)

AS [Last day of the previous month]

--Calculates the last day of the prior year

SELECT DATEADD(yy, DATEDIFF(yy,0,GetDate()), 0)

AS [Last day of the previous year]

--Calculates the last day of the current year

SELECT DATEADD(mm, DATEDIFF(m,0,GetDate() ) + 1, 0)

AS [Last day of the current year]

--Calculates the monday of the current week

SELECT DATEADD(wk, DATEDIFF(wk,0,GetDate()), 0)

AS [Monday of the current week]

--Calculates the yesterdays date

SELECT DATEADD(dd, DATEDIFF(dd,0,getdate()), -1)

AS [Yesterdays date]

--Calculates the todays date

SELECT GetDate()

AS [Todays date]

--Calculates the tommorows date

SELECT DATEADD(dd, DATEDIFF(dd,0,getdate()), 1)

AS [Tommorows date]

---Calculates the 15th day of previous month

SELECT DATEADD(d, 14, DATEADD(mm, DATEDIFF(m, 0, GETDATE()) - 1 , 0))

AS [15th day of previous month]

---Calculates the 15th day of current month

SELECT DATEADD(d, 14, DATEADD(mm, DATEDIFF(m, 0, GETDATE()), 0))

AS [15th day of current month]

---Calculates the 15th day of next month

SELECT DATEADD(d, 14, DATEADD(mm, DATEDIFF(m, 0, GETDATE()) + 1, 0))

AS [15th day of next month]

--Gets the name of the current month

SELECT DATENAME(month, GetDate())

AS [Name of the current month]

--Gets the weekday name of the current date

SELECT DATENAME(dw, GetDate())

AS [Weekday name of the current date]

--Gets the weekday name of the current date next year

SELECT DATENAME(dw, DATEADD(yy, 1, GetDate()))

AS [Weekday name of the current date next year]

--Gets the weekday name of the current date last year

SELECT DATENAME(dw, DATEADD(yy, -1, GetDate()))

AS [Weekday name of the current date last year]

 

First day of the previous month

-------------------------------

2008-09-01 00:00:00.000

(1 row(s) affected)

First day of the current month

------------------------------

2008-10-01 00:00:00.000

(1 row(s) affected)

First day of the next month

---------------------------

2008-11-01 00:00:00.000

(1 row(s) affected)

Last day of the previous month

------------------------------

2008-09-30 00:00:00.000

(1 row(s) affected)

Last day of the current month

-----------------------------

2008-10-31 00:00:00.000

(1 row(s) affected)

Last day of the next month

--------------------------

2008-11-30 00:00:00.000

(1 row(s) affected)

First day of the year

-----------------------

2008-01-01 00:00:00.000

(1 row(s) affected)

First day of the quarter

------------------------

2008-10-01 00:00:00.000

(1 row(s) affected)

First monday of the month

-------------------------

2008-10-06 00:00:00.000

(1 row(s) affected)

Last day of the previous month

------------------------------

2008-10-01 00:00:00.000

(1 row(s) affected)

Last day of the previous year

-----------------------------

2008-01-01 00:00:00.000

(1 row(s) affected)

Last day of the current year

----------------------------

2008-11-01 00:00:00.000

(1 row(s) affected)

Monday of the current week

--------------------------

2008-10-27 00:00:00.000

(1 row(s) affected)

Yesterdays date

-----------------------

2008-10-28 00:00:00.000

(1 row(s) affected)

Todays date

-----------------------

2008-10-29 13:14:31.130

(1 row(s) affected)

Tommorows date

-----------------------

2008-10-30 00:00:00.000

(1 row(s) affected)

15th day of previous month

--------------------------

2008-09-15 00:00:00.000

(1 row(s) affected)

15th day of current month

-------------------------

2008-10-15 00:00:00.000

(1 row(s) affected)

15th day of next month

-----------------------

2008-11-15 00:00:00.000

(1 row(s) affected)

Name of the current month

------------------------------

October

(1 row(s) affected)

Weekday name of the current date

--------------------------------

Wednesday

(1 row(s) affected)

Weekday name of the current date next year

------------------------------------------

Thursday

(1 row(s) affected)

Weekday name of the current date last year

------------------------------------------

Monday

(1 row(s) affected)

 

Source: http://www.keithrull.com/2008/07/24/SomeUsefulDateProcessingSQLSnippets.aspx


Categories: SQL Server
Posted by developer on Wednesday, October 29, 2008 6:12 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Set a field value by Reflection

public class DomainObjectIdSetter<IdT>

{

 private const string NAME_OF_ID_MEMBER = "ID";

 public void SetIdOf(DomainObject<IdT> domainObject, IdT id)

 {

   // Set the data property reflectively

   PropertyInfo idProperty = domainObject.GetType().GetProperty(NAME_OF_ID_MEMBER, BindingFlags.Public | BindingFlags.Instance);     

   Check.Ensure(idProperty != null, "idProperty could not be found");

   idProperty.SetValue(domainObject, id, null);

 }

}


Categories: C#
Posted by developer on Tuesday, October 21, 2008 6:13 AM
Permalink | Comments (0) | Post RSSRSS comment feed

SQL Server Database Samples

1) AdventureWorks

2) Northwind and pubs databases


Categories: SQL Server
Posted by developer on Thursday, October 02, 2008 10:34 AM
Permalink | Comments (0) | Post RSSRSS comment feed