Solved

SQL statement (date format)

Posted on 2013-11-09
6
397 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Database ERD 4 32
Dynamic SQL select query 4 38
Current Month Filter in Visual Studio 10 23
MS SQL SERVER and ADODB.commands 8 23
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

830 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