Solved

CONVERT DATE TO MM/DD/YY(YY)

Posted on 2013-10-24
10
375 Views
Last Modified: 2013-10-25
i have the following date 2013-09-25 00:00:00.000 how can I convert it to

MM/DD/YY(YY) in the sql query
0
Comment
Question by:Star79
[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
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39598539
SQL expert PortletPaul wrote an excellent article on SQL Server Date Styles using Convert, that demonstrates all styles.

The mm/dd/yy (and yyy) answer is
-- with the century
SELECT convert(varchar, GETDATE() ,1)

-- without the century
SELECT convert(varchar, GETDATE() ,101)

Open in new window

Keep in mind that this is for display only.
0
 

Author Comment

by:Star79
ID: 39598582
Hello jim,
can the date be converted to MM/DD/YY(YY) .Please note the brackets
0
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 250 total points
ID: 39598655
It could, but there's no single function to pull that off, so you'll need to do a lot of parsing...
Declare @dt datetime = GETDATE()

SELECT 
	RIGHT('0' + CAST(MONTH(@dt) as varchar(2)),2) + '/'	+
	RIGHT('0' + CAST(DAY(@dt) as varchar(2)),2) + '/' + 
	CAST(YEAR(@dt) / 100 as varchar(2)) + '(' + RIGHT(CAST(YEAR(@dt) as CHAR(4)),2) + ')'

Open in new window

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 26

Accepted Solution

by:
Zberteoc earned 250 total points
ID: 39598657
SELECT STUFF(convert(varchar, GETDATE(),101),9,0,'(')+')'

101 conversion is always MM/DD/YYYY

with double digits for MM and DD
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39599438
2013-09-25 00:00:00.000 as "MM/DD/YY(YY)"

So you expect? 09/25/20(13) ... is that correct? I think the following would be simpler

DECLARE @dt datetime = GETDATE()

SELECT
        convert(varchar(8),@dt ,103)
      + '('
      + convert(varchar(2),@dt ,12)
      + ')'

Open in new window

0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39599452
:) marginally easier in sql 2012
DECLARE @dt datetime = GETDATE()

SELECT
    convert(varchar(8),@dt ,103) + '(' + convert(varchar(2),@dt ,12) + ')'  AS "MM/DD/YY(YY) sql2000 +"

  , left(FORMAT(@dt, 'MM/dd/yyyy'),8) + FORMAT(@dt, '(yy)')                 AS "MM/DD/YY(YY) sql2012 +"

Open in new window

0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 39600014
And how is that simpler then my solution?
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39600087
Sorry Zbertoc, no offence intended - it's an opinion only. Nothing wrong with yours.
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 39600286
I wasn't offended at all I just wanted to know the reasoning behind that opinion. :)
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39600301
Note to self:  I must get in the habit of using STUFF() more often, as the above one-line solution demonstrates.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

740 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