Solved

how to have the number of the corresponde day of the week

Posted on 2013-01-26
7
467 Views
Last Modified: 2013-01-29
How to get the number of the corresponde day of the week, lest say 1,2,3,4,5,6 or 7?
0
Comment
Question by:rafaelrgl
7 Comments
 
LVL 35

Assisted Solution

by:David Todd
David Todd earned 100 total points
ID: 38823420
Hi,

This is almost completely fraught with danger - it depends on a number of things such as the datefirst setting, culture etc.

Look up the datepart function:
http://msdn.microsoft.com/en-us/library/ms174420.aspx

you want something like this:
select datepart( weekday, getdate())

HTH
  David
0
 
LVL 7

Expert Comment

by:Scobber
ID: 38823421
datepart(dw,getdate())

HTH
Scobber
0
 
LVL 6

Assisted Solution

by:esolve
esolve earned 100 total points
ID: 38823526
select datename(dw,getdate()) -- Saturday
select datepart(dw,getdate()) -- 7

or


SELECT  CASE DATEPART(WEEKDAY,GETDATE())  
    WHEN 1 THEN 'SUNDAY'
    WHEN 2 THEN 'MONDAY'
    WHEN 3 THEN 'TUESDAY'
    WHEN 4 THEN 'WENSDAY'
    WHEN 5 THEN 'THURSDAY'
    WHEN 6 THEN 'FRIDAY'
    WHEN 7 THEN 'SATURDAY'
END
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 100 total points
ID: 38824246
Scobber and esolve,

As dtodd indicated, relying on DATEPART for this is a potentially very bad practice: depending on the setting for @@DATEFIRST, you may not get the result you are looking for.  Specifically, assuming that the Asker intends Sunday = 1, then @@DATEFIRST must equal 7.

In my opinion, the safest and simplest way to test this would be to use the DATENAME function.  Assuming English for the localization...

SELECT CASE LEFT(DATENAME(dw, GETDATE()), 3)
    WHEN 'SUN' THEN 1
    WHEN 'MON' THEN 2
    WHEN 'TUE' THEN 3
    WHEN 'WED' THEN 4
    WHEN 'THU' THEN 5
    WHEN 'FRI' THEN 6
    ELSE 7 END AS WeekdayIndex

Open in new window

0
 
LVL 37

Assisted Solution

by:ValentinoV
ValentinoV earned 100 total points
ID: 38826040
As has been said before, you need to take the DATEFIRST setting into account to make sure the same day, in example below we use Monday , is always the first day of the week.

Here's how you can do that:

select ((DATEPART(dw, getdate()) + @@DATEFIRST-1-1)%7)+1

Open in new window

See following article for an explanation on that formula: Normalizing the First Day of the Week

Using above formula you'll always get a consistent day of week result, no matter what settings are in use.
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 100 total points
ID: 38827486
Assuming you want Monday to be day #1 -- we can adjust the "0" if needed to many any day #1.

This code always works, regardless of any date/language settings, AND IT'S UNDERSTANDABLE.

    DATEDIFF(DAY, 0, date_being_checked) % 7 + 1


For example:

SELECT
    DATEDIFF(DAY, 0, date_being_checked) % 7 + 1
FROM (
    SELECT GETDATE() AS date_being_checked UNION ALL
    SELECT GETDATE() - 1 UNION ALL
    SELECT '20130117'
) AS test_data
0
 
LVL 1

Author Closing Comment

by:rafaelrgl
ID: 38832136
thanks guys.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TSQL convert date to string 4 34
Dynamic SQL select query 4 36
query optimization 6 14
Stored Procedure needs owner to execute 5 12
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

809 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