?
Solved

Need help with a query.

Posted on 2016-10-26
10
Medium Priority
?
58 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
[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
  • 2
  • 2
  • 2
  • +4
10 Comments
 
LVL 66

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 13

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 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 49

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 29

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 28

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 29

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 66

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

777 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