Solved

Need help with a query.

Posted on 2016-10-26
10
52 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

840 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