Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL date formatting

Posted on 2011-02-15
8
Medium Priority
?
333 Views
Last Modified: 2012-05-11
Hey all

I am trying to format my date to

"Monday 21/12/2010" or "DAY dd/MM/YY"


Can I do that in SQL?
0
Comment
Question by:samelamin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 6

Expert Comment

by:MuffyBunny
ID: 34898468
Do you need the output to remain datetime data type or is it ok for the value to be a string?
0
 
LVL 6

Expert Comment

by:MuffyBunny
ID: 34898553
If a string is ok, this will do it

SELECT
CASE DATEPART(WEEKDAY,getdate())
      WHEN 1 THEN 'Sunday ' + CONVERT(varchar(10), GETDATE(), 103)
      WHEN 2 THEN 'Monday ' + CONVERT(varchar(10), GETDATE(), 103)
      WHEN 3 THEN 'Tuesday ' + CONVERT(varchar(10), GETDATE(), 103)
      WHEN 4 THEN 'Wednesday ' + CONVERT(varchar(10), GETDATE(), 103)
      WHEN 5 THEN 'Thursday ' + CONVERT(varchar(10), GETDATE(), 103)
      WHEN 6 THEN 'Friday ' + CONVERT(varchar(10), GETDATE(), 103)
      WHEN 7 THEN 'Saturday ' + CONVERT(varchar(10), GETDATE(), 103)
END AS formatteddate
0
 

Author Comment

by:samelamin
ID: 34898666
Muffy thanks thats good but what if I am passing it a date. This only works for todays date right

so what if I am passing it a date via a parameter will it still work?
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 34898692
Just use the date parameter or field

CONVERT(varchar(10), @MyDateParam, 103)

or

CONVERT(varchar(10), MyDateField, 103)
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 34898699
0
 
LVL 6

Accepted Solution

by:
MuffyBunny earned 2000 total points
ID: 34898712
This will work on any datetime value. Just replace the getdate() with the name of your date variable. If you want to create a whole new variable to use after reformatting...

DECLARE @FormattedDateString VARCHAR(30)
SELECT @FormattedDateString =
CASE DATEPART(WEEKDAY,getdate())
      WHEN 1 THEN 'Sunday ' + CONVERT(varchar(10), GETDATE(), 103)
      WHEN 2 THEN 'Monday ' + CONVERT(varchar(10), GETDATE(), 103)
      WHEN 3 THEN 'Tuesday ' + CONVERT(varchar(10), GETDATE(), 103)
      WHEN 4 THEN 'Wednesday ' + CONVERT(varchar(10), GETDATE(), 103)
      WHEN 5 THEN 'Thursday ' + CONVERT(varchar(10), GETDATE(), 103)
      WHEN 6 THEN 'Friday ' + CONVERT(varchar(10), GETDATE(), 103)
      WHEN 7 THEN 'Saturday ' + CONVERT(varchar(10), GETDATE(), 103)
END
0
 

Author Comment

by:samelamin
ID: 34898741
Thanks it worked Muffy
0
 

Author Closing Comment

by:samelamin
ID: 34898744
Thanks mate
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Screencast - Getting to Know the Pipeline

610 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