Solved

Need help with a query.

Posted on 2016-10-26
10
51 Views
Last Modified: 2016-10-27
Hi Experts,
I have the date in yyyymmdd format in the table. But I want to display it in mm/dd/yyyy. I am trying the following. But it does not work.
What can I do?

Thanks.
Select JobDate, convert(varchar(10),jobdate,101) from jobdetails
0
Comment
Question by:RadhaKrishnaKiJaya
  • 2
  • 2
  • 2
  • +4
10 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 41861056
Knock yourself out with this handy-dandy date conversion cheat sheet.

SELECT JobDate, convert(varchar, jobdate,101) as something
FROM jobdetails

Open in new window

0
 
LVL 12

Expert Comment

by:Jeff Darling
ID: 41861061
You could use Substring to grab the MM DD and YYYYY

Declare @myDate as varchar(8)

set @myDate = '20161025' 

select  SUBSTRING ( @myDate ,5 , 2 )+'/'+SUBSTRING ( @myDate ,7 , 2 )+'/'+SUBSTRING ( @myDate ,1 , 4 ) 

Open in new window

0
 
LVL 20

Accepted Solution

by:
Russ Suter earned 500 total points
ID: 41861063
Date, DateTime, and DateTime2 objects don't have a format in SQL Server. It sounds as if you're storing your date value as a string. If that's the case you'll need to convert it to a Date before converting it back to a VARCHAR. Messy but that's the way you need to do it if your original column type is a varchar.
SELECT CONVERT(VARCHAR, CONVERT(DATE, jobdate), 101)

Open in new window

2
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 48

Expert Comment

by:PortletPaul
ID: 41861162
As Russ Suter has rightly stated dates to NOT get stored in a format, and the fact that your existing approach does not work suggests the column isn't a real date as far as the database is concerned. So it is something else that looks like a date.

yyyymmdd
20161027 that column could be an integer or string (e.g. char/varchar)

If you do not know the actual data type of that column you will not know how to handle it properly.

This article may help:
DATE and TIME ... don't be scared, and do it right (the first time)

no points please
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41861687
I m not sure if you have SQL 2012 or not..If you have then Format is the best thing you can use.

--FOR SQL 2012+

DECLARE @DT AS VARCHAR(10) = '20160101'
SELECT FORMAT(CAST(@DT AS DATE),'MM/dd/yyyy')

GO

O/P
01/01/2016


--FOR SQL 2008+

DECLARE @DT AS VARCHAR(10) = '20160101'
SELECT CONVERT(VARCHAR(10),CAST(@DT AS DATE),101)

GO

O/P
01/01/2016

Hope it helps!!
0
 
LVL 27

Expert Comment

by:Ark
ID: 41861728
VB approach
Private Function ParseDate(dateString As String, format As String) As Date?
    Dim dt As Date
    If Date.TryParseExact(dateString, format, Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, dt) Then
        Return dt
    End If
    Return Nothing
End Function
Private Function FormatDate(dt As Date, format As String) As String
    Return dt.ToString(format, Globalization.CultureInfo.InvariantCulture)
End Function

Open in new window

Using:
Dim dt = ParseDate("20160215", "yyyyMMdd")
If dt.HasValue Then
    MsgBox(FormatDate(dt.Value, "MM/dd/yyyy"))
Else
    MsgBox("Invalid date")
End If

Open in new window

0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41861752
Try...

Dim provider As CultureInfo = CultureInfo.InvariantCulture
Dim dt As Date = DateTime.ParseExact("20160215", "yyyyMMdd", provider).ToString("MM/dd/yyyy")
0
 

Author Closing Comment

by:RadhaKrishnaKiJaya
ID: 41862429
Thanks. It worked!!
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 41862485
Radha - Just out of curiosity, were your values stored in a column with date format or varchar?  

As you can tell that was a big part of the answer that was not mentioned in the original question, and I don't see any follow-up from you before declaring 'it worked'.  

Thanks in advance.
0
 

Author Comment

by:RadhaKrishnaKiJaya
ID: 41862847
It is Varchar. I just tried Russ's solution and it worked for me.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 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