Solved

SQL statement (date format)

Posted on 2013-11-09
6
394 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
ID: 39636020
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
ID: 39636194
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
ID: 39636200
select LASTNAME, FRSTNAME, CONVERT(VARCHAR, [BRTHDATE], 110), DIVISIONCODE_I, DEPRTMNT
from UPR00100
where EMPLCLAS <> 'terminated'
order by EMPLCLAS
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 125 total points
ID: 39636212
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
ID: 39636409
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
ID: 39636413
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

911 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

18 Experts available now in Live!

Get 1:1 Help Now