Home > Functions and DMVs, SQL Queries, SQL Server, T-SQL > Get WeekNumber in SQL server using datepart() function

Get WeekNumber in SQL server using datepart() function

February 11, 2010 Leave a comment Go to comments

Many applications and organizations use the week number of a date for some sort of identification, classification, or formatting purpose. Most of the organisation uses week number in their reports related to revenue , refunds etc. SQL server provides an inbuilt function to get the week number of any given date. Remember, the week number that I mention here is the calender week number. The financial week number differs from one institution to another.

The Datepart() function in SQL server can be used for this purpose.

Read More . . . . 

Advertisements
  1. February 8, 2011 at 5:49 pm

    –Tuesday, February 8th, 2011
    –12:43:57.993 PM

    –7
    –SELECT DATEPART( wk, GETDATE() )
    –SELECT DATEPART( week, GETDATE() )

    –3
    –SELECT DATEPART( weekday, GETDATE() )
    –SELECT DATEPART( dw, GETDATE() )

    –2011
    –SELECT DATEPART( yy, GETDATE() )
    –SELECT DATEPART( yyyy, GETDATE() )
    –SELECT DATEPART( year, GETDATE() )

    –1
    –SELECT DATEPART( quarter, GETDATE() )
    –SELECT DATEPART( qq, GETDATE() )
    –SELECT DATEPART( q, GETDATE() )

    –2
    –SELECT DATEPART( month, GETDATE() )
    –SELECT DATEPART( mm, GETDATE() )
    –SELECT DATEPART( m, GETDATE() )

    –8
    –SELECT DATEPART( day, GETDATE() )
    –SELECT DATEPART( d, GETDATE() )

    –39
    –SELECT DATEPART( dayofyear, GETDATE() )
    –SELECT DATEPART( dy, GETDATE() )

    –12
    –SELECT DATEPART( hour, GETDATE() )
    –SELECT DATEPART( hh, GETDATE() )

    –43
    –SELECT DATEPART( minute, GETDATE() )
    –SELECT DATEPART( mi, GETDATE() )
    –SELECT DATEPART( n, GETDATE() )

    –57
    –SELECT DATEPART( ss, GETDATE() )
    –SELECT DATEPART( s, GETDATE() )

    –993
    –SELECT DATEPART( ms, GETDATE() )

  2. June 30, 2011 at 8:59 am

    So simple; so clear! Thank you, sir!

  3. D
    October 25, 2012 at 12:58 am

    SELECT DATEPART( WW , ’10/24/2012′) gives 43 . But i want it as 4th week of the month. How do i do it?

    • sunny
      March 6, 2013 at 4:37 am

      answer for this plz??

  4. November 29, 2012 at 9:12 am

    hi please i need a little help , i have to write a query that states the number of accounts that is created in the week , in the database i have a table called account which has a field called date created , so want to take the present data and see the account created in the week

    • tom
      January 21, 2013 at 11:12 am

      Hi you’ll need something like
      Count account
      FROM table_name
      WHERE column_name
      BETWEEN Date 1AND Date 2

  5. July 21, 2014 at 8:47 pm

    Great goods from you, man. I have understand your stuff previous to and you’re just too
    great. I really like what you’ve acquired here, really like what you’re saying and the way in which you say it.
    You make it enjoyable and you still care for to keep it smart.
    I can’t wait to read much more from you. This is really a terrific website.

  6. February 22, 2016 at 1:15 am

    I am genuinely glad tto rad this webog posts which
    contains plenty of useful information, thanks for providing these kinds of information.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: