Solved

SQL statement (date format)

Posted on 2013-11-09
6
393 Views
Last Modified: 2013-11-14
How do I change a date format of 1951-07-15 to 07-15-1951?

thanks!
0
Comment
Question by:pstre
6 Comments
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
SQL expert PortletPaul wrote an excellent article on SQL Server Date Styles (formats) using CONVERT() that shows this and a ton of other date formats ...
Declare @dt date = '1951-07-15'
SELECT convert(varchar, @dt ,110)

Open in new window

0
 

Author Comment

by:pstre
Comment Utility
the statement above is for 1 date.  I have 1300 dates to convert.  How would I update this SQL statement to the desired date format?  

select LASTNAME, FRSTNAME, BRTHDATE, DIVISIONCODE_I, DEPRTMNT
from UPR00100
where EMPLCLAS <> 'terminated'
order by EMPLCLAS
0
 
LVL 9

Assisted Solution

by:guswebb
guswebb earned 125 total points
Comment Utility
select LASTNAME, FRSTNAME, CONVERT(VARCHAR, [BRTHDATE], 110), DIVISIONCODE_I, DEPRTMNT
from UPR00100
where EMPLCLAS <> 'terminated'
order by EMPLCLAS
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 125 total points
Comment Utility
Dates are stored as dates, and not with any specific style.  
The custom date formats shown is a varchar, not a date, so assuming BIRTHDATE is a date and BIRTHDATE_varchar is a varchar..

UPDATE UPR00100
SET BIRTHDATE_varchar = convert(varchar, BIRTHDATE ,110)
where EMPLCLAS <> 'terminated'
order by EMPLCLAS
0
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 250 total points
Comment Utility
WAIT!

if that field is a varchar, and you convert it to MM-DD-YYYY format, then kiss goodbye any ability to sort by that field easily (or use between and so on)

put simply, YYYY-MM-DD is a far more sensible date format than MM-DD-YYYY

IF on the other hand the field is a datetime, then you don't need to update it at all.

could you confirm the actual data type of this field please?
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 250 total points
Comment Utility
by the way: on your other question regarding calculating age you provided some sample data

Hortin                     1951-07-15 00:00:00.000     001005       001    
...
Tsalik                      1977-02-02 00:00:00.000      001007       001

the existence of 00:00:00.000 in that data strongly points to the field being a datetime data type

in which case you cannot update the field to any specific format because datetime values are actually stored as 2 integers (i.e. what you see isn't what is stored).

As Jim indicated in the first reply, you can change what you see from a query by using a "format style" and style number 110 gives you DD-MM-YYYY

---------
IF you are using SQL Server 2012 then you could use format instead.

SELECT FORMAT(brthdate, 'MM-dd-yyyy') AS "brthdate MM-DD-YYYY"
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

744 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

15 Experts available now in Live!

Get 1:1 Help Now