Solved

SQL date formatting

Posted on 2011-02-15
8
330 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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 500 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

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.

Question has a verified solution.

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

Suggested Solutions

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

726 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