Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 423
  • Last Modified:

SQL statement (date format)

How do I change a date format of 1951-07-15 to 07-15-1951?

thanks!
0
pstre
Asked:
pstre
4 Solutions
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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
 
pstreAuthor Commented:
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
 
guswebbCommented:
select LASTNAME, FRSTNAME, CONVERT(VARCHAR, [BRTHDATE], 110), DIVISIONCODE_I, DEPRTMNT
from UPR00100
where EMPLCLAS <> 'terminated'
order by EMPLCLAS
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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
 
PortletPaulCommented:
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
 
PortletPaulCommented:
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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now