Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 convert date ( yyyy-mm-dd) to (mm-dd-yyyy)

Author  Topic 

rds207
Posting Yak Master

198 Posts

Posted - 2010-02-11 : 14:26:38
could any body please let me know how i can convert convert datetime ( yyyy-mm-dd) to date mm/dd/yyyy(mm-dd-yyyy) in sql server 2008

Kristen
Test

22859 Posts

Posted - 2010-02-11 : 14:35:10
Best to do it in the front-end application

If that is not possible use CONVERT(varchar(10), MyDateTime, 101) which will give you "mm/dd/yyyy" - will that do? if no use REPLACE to convert "/" to"-"

I am assuming that your source date (which you refer to as being in "yyyy-mm-dd" format) is actually in a DATETIME datatype object (column / variable), but if it is in a string you will have to first convert it to DATETIME with CONVERT(datetime, MyYYYYMMDDobject, 120), so:

SELECT REPLACE(CONVERT(varchar(10), CONVERT(datetime, '2010-02-11', 120), 101), '/', '-')
gives
"02-11-2010"
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-02-11 : 14:45:04
Another way is to use convert option 110, which would give you the date in mm-dd-yyyy
SELECT CONVERT(varchar(10), CONVERT(datetime, '2010-02-11', 120), 110)
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-02-11 : 14:57:56
"Another way is to use convert option 110, which would give you the date in mm-dd-yyyy"

Ah yes, so it does
Go to Top of Page

rds207
Posting Yak Master

198 Posts

Posted - 2010-02-11 : 16:34:37
Sorry i reedited...

My SOURCE column datatype is datetime , format is yyyy-mm-dd
My DESTINATION column datatype is date , and the format should be mm/dd/yyyy


quote:
Originally posted by rds207

My destination column datatype is datetime , format is yyyy-mm-dd
My source column datatype is date , and the format should be mm/dd/yyyy

when i execute the below query( there are some date conversions included in the query)

Select CONVERT(varchar(10),dateadd(d, datediff(d, 0, dateadd(HOUR,-7,EC_FINISH_TIME)), 0), 101)
from DW_T_EC_SBA

this work fine with the output(mm/dd/yyyy) , but when i insert the data INTO THE TABLE , it is getting inserted as yyyy-mm-dd...

could anyone please help ...

do i need to change the datatype of my destination column? if yes what it has to be?
i dont want time to be displayed in the destination column , thats the reason i used date..

please help


quote:
Originally posted by Kristen

"Another way is to use convert option 110, which would give you the date in mm-dd-yyyy"

Ah yes, so it does



Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-02-11 : 17:23:53
Why are you worried about format of 'date' datatype. SQL server stores it as yyyy-mm-dd. You can change the format while selecting the values based on the CONVERT types that is already mentioned.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-02-12 : 01:15:54
quote:
Originally posted by rds207

could any body please let me know how i can convert convert datetime ( yyyy-mm-dd) to date mm/dd/yyyy(mm-dd-yyyy) in sql server 2008


Where do you want to show formatted dates?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-12 : 01:28:03
quote:
Originally posted by rds207

Sorry i reedited...

My SOURCE column datatype is datetime , format is yyyy-mm-dd
My DESTINATION column datatype is date , and the format should be mm/dd/yyyy


quote:
Originally posted by rds207

My destination column datatype is datetime , format is yyyy-mm-dd
My source column datatype is date , and the format should be mm/dd/yyyy

when i execute the below query( there are some date conversions included in the query)

Select CONVERT(varchar(10),dateadd(d, datediff(d, 0, dateadd(HOUR,-7,EC_FINISH_TIME)), 0), 101)
from DW_T_EC_SBA

this work fine with the output(mm/dd/yyyy) , but when i insert the data INTO THE TABLE , it is getting inserted as yyyy-mm-dd...

could anyone please help ...

do i need to change the datatype of my destination column? if yes what it has to be?
i dont want time to be displayed in the destination column , thats the reason i used date..

please help


quote:
Originally posted by Kristen

"Another way is to use convert option 110, which would give you the date in mm-dd-yyyy"

Ah yes, so it does






provided your destination is sql server and datatype used is date it will always be stored in ccyy-mm-dd format. you dont need to worry on format while storing dates as you can always use formatting functions while retrieving from table to display date in the way you want

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-02-12 : 01:36:12
Actually dates are internally stored as Integers in the table

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-12 : 01:38:33
quote:
Originally posted by madhivanan

Actually dates are internally stored as Integers in the table

Madhivanan

Failing to plan is Planning to fail


agreed. But what I told was how we see data when we query the table.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-02-12 : 01:40:01
quote:
Originally posted by visakh16

quote:
Originally posted by madhivanan

Actually dates are internally stored as Integers in the table

Madhivanan

Failing to plan is Planning to fail


agreed. But what I told was how we see data when we query the table.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/




Yes it is

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -