Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting Dates to YYYYMMDD 2

Status
Not open for further replies.

jazerr

Programmer
Dec 13, 2000
152
US
SQL stores dates as YYYY-MM-DD HH:MM:SS.I need to return the dates as YYYYMMDD.I can't use
DATEPART(YEAR, objname.columnID)+
DATEPART(MONTH, objname.columnID)+
DATEPART(DAY, objname.columnID)
because this converts each value to data type int, then adds them together.Any IDEAS?
 
select cast(datepart(year, enterdate) as varchar(4)) + cast(datepart(month, enterdate) as varchar(2)) + cast(datepart(day, enterdate) as varchar(2)) from tablename

This works but the less than 10 parts of month and day throw off the alignment. maybe a combination of sql and high order laguage manipulation.

good luck

 
I use convert(varchar,@date,103) to get dd/mm/yyyy
Edwin Dalorzo
edalorzo@hotmail.com

 
mvanbro, is enterdate the column you are casting?
 
Is there a way to go back from yyyymmdd to yyyy-mm-dd? what would the syntax be?
 
Yes @date means any date value you want to format. I just used this variable for sample purposes.

You can go back to date format like this:

Code:
convert(datetime,'11/12/2000') to go back to 'yyyy-mm-dd'


Edwin Dalorzo
edalorzo@hotmail.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top