Solved

Need help with a query.

Posted on 2016-10-26
10
42 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
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 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 27

Expert Comment

by:Ark
Comment Utility
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 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
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
Comment Utility
Thanks. It worked!!
0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
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
Comment Utility
It is Varchar. I just tried Russ's solution and it worked for me.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now