Solved

SQL statement (date format)

Posted on 2013-11-09
6
396 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add different cell to otherwise similiar row 4 38
Sql server function help 15 28
SQL View nearest date 5 35
T-SQL: New to using transactions 9 20
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

785 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