Return a string representing the day of the week

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bobwhosmiles
    New Member
    • Aug 2006
    • 5

    Return a string representing the day of the week

    Hi All,

    New to the forum and new to T-SQL

    I'm trying to add a column to the results of query that shows a text string representing the day of the week.

    Code:
    Select Activity.StartDateTime as DAY
    ...returns the full date and time. I just want the day of the week.

    I know it's possible to return an integer which represent the day of the week, and I know there's a way to specify which day of the week you want to call the first. Typically in the US the first day of the week is Sunday I think, but here in the UK it's Monday.

    Thing is, I don't just need an integer, I need a string that represents the abbreviated form of the actual name of the day.

    eg

    MON = Monday
    TUE = Tuesday

    etc etc

    Can anyone help please? Don't know where to start.

    Bob
  • Bobwhosmiles
    New Member
    • Aug 2006
    • 5

    #2
    After much hunting around online I've found the DATENAME function which apparently when used with "weekday" as per the example below returns a string like "Sunday", "Monday" or whatever based on the day of the week that correlates to the date

    Code:
    DATENAME(weekday, Activity.StartDateTime)
    However, now that I'm at home (not work where I have enterprise manager) I can't test this. Ideally I want to return the first three letters of the string and capitalize them. I'm guessing for the first three letters I could adapt the code above thus:
    Code:
    SELECT LEFT(DATENAME(weekday, Activity.StartDateTime),3) As DAY
    Anyone any ideas firstly if this will work and secondly if there's an equivalent to excel/vba's UPPERCASE to capitalize the string?

    Any help would be appreciated. :)

    Bob

    Comment

    • Bobwhosmiles
      New Member
      • Aug 2006
      • 5

      #3
      Maybe someone could paste...

      SELECT LEFT(DATENAME(w eekday, GETDATE()),3) As DAY

      ...into Query Analyzer and tell me what they get (should be the first three letters of the name of the current day of the week)

      Comment

      • Bobwhosmiles
        New Member
        • Aug 2006
        • 5

        #4
        For anyone else wanting to do the same - it does work!

        Thanks for all your help everyone!

        Comment

        • Tuesday Frase

          #5
          Awesome

          Thanks - this was something I needed, but I wasn't aware that it existed!

          Comment

          • ck9663
            Recognized Expert Specialist
            • Jun 2007
            • 2878

            #6
            As always, these kinds of things is always recommended to be done on your front-end. Unless of course this is some sort of data extraction.

            In any case, glad it worked.

            Happy Coding!!!

            ~~ CK

            Comment

            Working...