Convert varchar(20) date to date that looks like mm/dd/yyyy

Good afternoon,
I am a total novice at SQL and have been tasked to change a varchar(20) date column (shows up as m/dd/yy) to show up as mm/dd/yyyy. I've looked at the previous posts, but being a novice, it's difficult to wade through the non-applicable stuff. The column
that needs to be changed is field2, but I'm not sure how to construct a statement to reflect my objective.
Thank you in advance. 

The sample uses a table variable. You can program it the same way for a table:
DECLARE @t TABLE (dt VARCHAR(20))
INSERT INTO @t
VALUES ('1/2/12')
,('3/10/10')
,('7/23/9')
,('aa')
,('12/31/99')
,('10/20/00')
,('10/20/01')
SET DATEFORMAT mdy
SELECT dt
,CONVERT(VARCHAR(10), CASE
WHEN isdate(dt) = 0
THEN NULL
ELSE cast(dt AS DATE)
END, 101) AS newDt
FROM @t;/******** TRY_CONVERT() method ************/
SELECT dt, newDt=CONVERT(varchar(10),TRY_CONVERT(date,dt),101) FROM @t;
GO
dt newDt
1/2/12 01/02/2012
3/10/10 03/10/2010
7/23/9 07/23/2009
aa NULL
12/31/99 12/31/1999
10/20/00 10/20/2000
10/20/01 10/20/2001
dt newDt
1/2/12 01/02/2012
3/10/10 03/10/2010
7/23/9 07/23/2009
aa NULL
12/31/99 12/31/1999
10/20/00 10/20/2000
10/20/01 10/20/2001
Starting with SQL Server 2012 you can use TRY_CONVERT().
You can start studying datetime functions here:
http://www.sqlusa.com/bestpractices/datetimeconversion/
Kalman Toth Database & OLAP Architect
T-SQL Scripts at sqlusa.com
New Book / Kindle: Exam 70-461 Bootcamp: Querying Microsoft SQL Server 2012

Similar Messages

  • Convert Varchar to Date

    Hi
    im trying to convert varchar to date.
    So, i used To_date ( cilumn, 'DD-MM-YYYY') , Since i have to order the out put in Decending Order i used the same convertion function with Order by clause.
    The problem is ... the out put is not ordered,
    <sql>
    select date_created
    from
    select distinct
    to_date(created_date,'DD-MM-YYYY') as date_created
    from schedule_backup
    order by date_created desc
    <sql>
    out put is
    =================
    DATE_CREA
    26-NOV-09
    23-NOV-09
    19-NOV-09
    16-NOV-09
    11-NOV-09
    04-NOV-09
    30-OCT-09
    11-SEP-09
    04-MAR-10
    01-MAR-10
    25-FEB-10
    DATE_CREA
    22-FEB-10
    17-FEB-10
    15-FEB-10
    11-FEB-10
    09-FEB-10
    03-FEB-10
    27-JAN-10
    20-JAN-10
    15-JAN-10
    04-JAN-10
    thanks
    raj

    Hi, Raj,
    When you store dates in a VARCHAR2 column, you're asking for touble, so it seems that everything is working according to plan.
    Even aside from that, your query works perfectly for me.
    In the output you posted, the order is correct within each year, and you're only showing 2 digits of the year. Are you sure the centuries are right in the schedule_backup table? Perhaps everything that was supposed to be '2010' got enetereed as '1910' or '0010'.
    Post some sample data (CREATE TABLE and INSERT statements) that gets the wrong results with your query.
    For example:
    CREATE TABLE     schedule_backup
    (     created_date     VARCHAR2 (10)
    INSERT INTO schedule_backup (created_date) VALUES ('11-11-2009');
    INSERT INTO schedule_backup (created_date) VALUES ('04-11-2009');
    INSERT INTO schedule_backup (created_date) VALUES ('30-10-2009');
    INSERT INTO schedule_backup (created_date) VALUES ('11-09-2009');
    INSERT INTO schedule_backup (created_date) VALUES ('04-03-2010');
    COMMIT;

  • Convert VARCHAR to date - don't know format in advance

    Hi,
    If I want to convert varchar to date, but I don't kniow the format of the varchar in advance. For example, it may be '20-MAY-99', '2009-01-10 10:01:00', but it is not known in advance. How can I convert it to date?
    Many thanks

    You'll have to define a hierarchy of date formats to try. There are some strings that map to many different valid dates depending on the format. The string '01/02/03' might translate to January 2, 2003, January 2, 1903, February 1, 2003, February 1, 1903, February 3, 2001, February 3, 1901, etc.
    Assuming you define a hierarchy of formats, you can write a function that tries each format in turn, i.e.
    CREATE FUNCTION my_to_date( p_string IN VARCHAR2 )
      RETURN DATE
    IS
      l_date_variable DATE;
    BEGIN
      l_date_variable := to_date( p_string, <<format 1>> );
      return l_date_variable;
    EXCEPTION
      WHEN OTHERS THEN
        BEGIN
          l_date_variable := to_date( p_string, <<format 2>> );
          return l_date_variable;
        EXCEPTION
          WHEN OTHERS THEN
    END;You'll need to define the N date formats to try and the order in which to try them.
    Justin

  • How to convert varchar to date datatype while insert or update in table

    Hai All
    I need to convert to varchar to date.
    I have two Tables T1,T2
    T1 Structure
    Code varchar
    Time varchar
    Date varchar
    T2 Structure
    Empname var
    Empcode var
    Intime date
    Outtime date
    Intrin date
    Introut date
    Att_date
    Now i need to move Time form T1 to T2 Intime,outtime,intrin,introut according some condition
    So now i need to convert Varchar to Date while insert or update
    I have tried something
    Insert into T1 (code,intime,att_date)values
    (code,To_date(Date||time,'dd-mon-yyyy hh24mi'),att_date);
    OR While update
    Update T2 set Outtime=To_date(Date||time,'dd-mon-yyyy hh24mi') where...
    I got an error Ora-01861
    Regards
    Srikkanth.M

    You didn't show any example of your date or time values, butyou might need to add a space between them, like
    To_date(Date || ' ' || time,'dd-mon-yyyy hh24mi')

  • How can I unlock my iphone??? on the screen I only have the apple logo and  a thing that looks like if the phone is working but no advance

    how can I unlock my iphone??? on the screen I only have the apple logo and  a thing that looks like if the phone is working but no advance

    Hi,
    Try a Reset of the Phone... You will Not Lose Any Data...
    Turn the Phone Off...
    Press and Hold the Sleep/Wake Button and the Home Button at the Same Time...
    The Apple logo will Appear and then Disappear...
    Usually takes about 10 - 15 Seconds...
    Turn the Phone On...
    If that does not help... See Here:
    Backing up, Updating and Restoring
    http://support.apple.com/kb/HT1414

  • Is there an alternative email view that look like outlook?

    Is there n alternative view in Apple email that looks like Outlook?  A line for each email with date/time and subject.

    Thank You that was so simple,  in Yosemite it is Preferences->Viewing -> Classic

  • Create T.Code for a query that looks like Report VF05

    Hi,
    I'm facing a problem right now.. I made a Query with SQVI, then I converted it with SQ01.. It is a report that looks like VF05 but has the column EAN/UPC Code added on it.
    Now I am trying to create a T.code to run this report, using SE93.
    So, I called the T.Code ZVF05, and then I tried to Create it, but then I don't know what Start Object I should choose: I first choosed "Program and screen", I gave the field Program value "SAPMS38R" and Screen name "180" as seen in the my Query
    , but when trying to test it, I receive the message: "Incorrect program type with Submit"
    What would you suggest to make this TCode work?
    PS. Please note that I am SD functional consultant, not a ABAPer...
    Edited by: Cristiana Vasile on Oct 13, 2008 2:23 PM

    Hi,
    In SQ01, Menu-> Query->More Funcitons->Generate Program
    Once done, Get the program name:
    SQ01, Menu-> Query->More Funcitons->Display report name..
    With this report name. goto SE93 and create a report transaction with the program name = Query program name obtained above and Screen no 1000.
    Alternatively Goto SE38 and give the Query program name obtained above  and then Execute
    Regards
    Shiva

  • Sending email using mail that looks like a web page

    I want to send an email that looks like a web page (kind of like those emails from apple or other companies). I tried making a document in Pages, but then that didn't quite work. Of course, I could make the pages document into a pdf file, but that's not ideal. Any recommended application?

    Check Composing HTML messages in Apple Mail.

  • HT4527 I can't get all my music I purchased from Itunes on my iphone to work.  When displaying them on my computer I can't play them or on my phone.  They have a little icon to the left that looks like a repeat icon.  It won't let me play them.  Please he

    I can play the music on my itunes under my purchased section but when I go to my phone when I have it connected the a lot of the songs won't play on my song or from the itunes under my phone.  They have a little icon to the left of them that looks like a repeat icon.  It won't let me delete them from there nor play them.  When on my phone only it won't play them it just brings up the clip art but will not play.  Please can anyone help me.
    Thanks,

    Thank you - I thought that might be the case BUT why doesn't the music play that I have bought from itunes - do you happen to know please.
    All that happens is it just flicks through it all and won't play any of it???
    As in I select the song and it just flicks back one screen (if I am making sense).....
    I am really stuck and cannot work out what's gone wrong that's all.
    Considering a reset of my new iphone and trying again but done that once and it's still the same.

  • How do I restore my Ipad 4 from my desktop computer? My charger was broken. I bought a new one but now it won't charge. I have a BATTERY bar in the middle of the screen that is flashing RED and I have a symbol on the bottom that looks like a charger.

    How do I restore my Ipad 4 from my desktop computer? My charger was damaged so I bought a new one but it won't charge. On my Ipad, I have a BATTERY bar in the middle that is flashing RED and an icon on the bottom that looks like the end of a charger. I have tried to force into reset mode, by holding the sleep key and home button SEVERAL times but it does NOT work. I have plugged my Ipad into my desktop computer, but I do not have a screen on my desktop that is coming up. THIS HAPPENED BEFORE AFTER I DID AN UPDATE AND WAS LOCKED OUT AND I USED EITHER ICLOUD OR ITUNES TO RESTORE, NOT SURE WHICH AND I FORGOT HOW.

    Hi MrsMoosie,
    I'm sorry to hear you are having these issues with your iPad. If your iPad isn't responding or doesn't appear to be charging, you may find the information and troubleshooting steps outlined in the following articles helpful, in particular the following portion:
    If you don't see the charging screen within an hour, or you see the connect to power screen, check your connector, USB cable, and power adapter. Make sure that everything is plugged in firmly, free of debris, and not damaged. You might want to try a different USB cable or power adapter.
    If your iPhone, iPad, or iPod touch doesn't respond or doesn't turn on - Apple Support
    If you can’t charge your iPhone, iPad, or iPod touch - Apple Support
    Regards,
    - Brenden

  • I have a 2nd gen itouch and i tried to restore it...but i took the cord out on accident while restoring...so everytime it turns on it stops at the apple screen with the little loading thing that looks like a flower. it wont even connect to my laptop.help?

    ok, so my itouch 2nd gen starts up only to the apple logo but stops there with the loading thing that looks like a flower. it doesnt connect to my laptop either. it got unplugged while trying to restore it on my laptop and now its not rly working. any help? i alredy tried holding both buttons down like more than 100 times and it still doesnt work. so any solutions?

    Try placing the iPod in recovery mode and then try restoring the iPOd via iTunes.  For recovery mode see:
    http://support.apple.com/kb/HT1212

  • HT3204 I updated my iphone 4 and now it won't turn on. All i get is the itunes logo and an image that looks like the charger plug.

    I UPDATED MY IPHONE 4 AND NOW IT WILL NOT TURN ON. ALL IGET IS THE ITUNES LOGO AND AN IMAGE THAT LOOKS LIKE THE CHARGER PLUG.

    plumbin wrote:
    .... ALL IGET IS THE ITUNES LOGO AND AN IMAGE THAT LOOKS LIKE THE CHARGER PLUG.
    1)  Connect to iTunes on the computer you usually Sync with and Restore
    http://support.apple.com/kb/HT1414
    2)  If necessary Place the Device into Recovery mode...
    http://support.apple.com/kb/HT1808
    Note on Recovery Mode.
    You may need to try this More than Once...
    Be sure to Follow ALL the Steps...
    Once you have Recovered your Device...
    Re-Sync your Content or Restore from the most Recent Backup...
    Restore from Backup
    http://support.apple.com/kb/ht1766
    Make sure you have the Latest Version of iTunes Installed on your computer ( v 11.1)
    iTunes free download from www.itunes.com/download

  • HT2188 I'm presuming my old iPod id dead meat, but after 24 charging a get the Apple Logo followed by an image that looks like a file folder with an exclamation point. Is there anyway we can resurrect all trhe music on it & transfer into a new iPod?

    I'm presuming my old iPod id dead meat, but after 24 charging a get the Apple Logo followed by an image that looks like a file folder with an exclamation point. Is there anyway we can resurrect all trhe music on it & transfer into a new iPod?

    You can try putting it into Disk Mode
    http://support.apple.com/kb/HT1363
    If that works, you can see if the computer will recognize it when connected, either in iTunes or by the system.
    NOTE:  You can also try putting it into Disk Mode WHILE it is already connected to the computer.
    If the iPod is recognized, you may be able to use one of the third-party methods or utilities to transfer song files from iPod to computer.  You can do a Google search on something like "ipod transfer" to get some links (add "mac" to the search if your computer is a Mac).
    Once the song files are on your computer, add them to that computer's iTunes library, then sync it to the new iPod.

  • Does Apple make a monitor that looks like the iMac27, for dual monitor setup?

    I'm thinking about buying an iMac, but I'm used to a multiple monitor setup. Does Apple make a monitor that looks like the iMac27, for dual monitor setup?

    Look at these links.
    http://store.apple.com/us/product/MC914LL/B/apple-thunderbolt-display-27-inch?fn ode=53
    http://store.apple.com/us/product/MC007LL/A/apple-led-cinema-display-(27%22-flat -panel)?fnode=53
    Allan

  • Can someone tell me how to make the letters that look like fabric?

    Can someone tell me how to make the letters that look like fabric?

    pharrout wrote:
    Can someone tell me how to make the letters that look like fabric?
    It could be as simple as clipping an image of the fabric of your choice, to a text layer.
    In this example the Hessian texture was downloaded from Google images, and placed over a Type layer.  You then Alt (Opt) click the intersection of the Type and Hessian layer to clip them together.  This just means that only the shape of the underlying layer will show in the clipped layer.

Maybe you are looking for

  • READ-ONLY CHARACTER MODE REPORT OUTPUT

    Hi, I am using Developer Suite 10g Report Builder to create a character mode report. I use commands as below to open the report output; PROCEDURE Run_Report (vReportname varchar2) IS LINEBUF VARCHAR2(2000); BEGIN linebuf := NULL; linebuf :='C:\DevSui

  • Error message F5151 using RAPERB2000

    Hello! I've displayed error message F5151 using RAPERB2000. "Incorrect doc.no.: 4306000002. Select document number between 4307000001and4307999999". I don't know why.. the document to be posted is referred to 2007 fiscal year and the latest one is po

  • Kicked Out of All Apps, Including Standard iPhone Ones

    Whether I try to use the web browser, the video player, or an app picked up from the application store, I'm kicked back out to the main screen after a few seconds. I'm reticent to install the latest update based off the posts I'm seeing here as the p

  • Pinned app tabs don't logout on browser close

    Pinned app tabs are restored on browser restart, however I have gmail pinned, with save passwords disabled, and still when I close the browser, it automatically logs in. I tried it with private browsing, disabling cookies, deleting cookies on browser

  • Kzdocl

    When erasing an user, it shows the following error: drop user xxxx cascade; ORA-006000: internal error code, arguments: [kzdocl], [],[],[],[],[],[],[] OS: Redhat 5.0 ES (64 bits) Database: 10g 10.2.0.4.0 (64Bit production) Standart Edition thank Izai