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
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Date Format to VarChar Column

Author  Topic 

lesponce
Starting Member

8 Posts

Posted - 2013-01-06 : 21:39:08
I'm trying to set a date with value or min value to a VarChar column, but I always get the date format as Jan 1, 2013 instead of the default SQL Server date as "date".

How do I get the date format in a VarChar column?

theboyholty
Posting Yak Master

226 Posts

Posted - 2013-01-07 : 03:32:36
I don't think its possible without engaging in some manic overuse of SUBSTRING etc to get the format '2013-01-07 08:26:00.000' in a varchar field. My personal preference when trying to get the date into a varchar is to use the CONVERT function:

e.g. CONVERT(VARCHAR(8),GETDATE(),112) - this will give the output '20130107'.

Its the third value (the 112) which determines the format of the date. You can have a play around with different values here to get different formats covering the date and time. Without looking it up I think the values run from about 100 to about 115. If you do choose to experiment with these values, don't forget to change the varchar value from 8 characters to something like 20 as most of the others will require more characters.

---------------------------------------------------------------------------------
http://www.mannyroadend.co.uk A Bury FC supporters website and forum
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2013-01-07 : 07:58:29
Why does the formation matter when you use SQL? If you want to show dates in formatted way in a front end application, use format function there. If you do not have an option, you can also make use of FORMAT function introduced in version 2012

Madhivanan

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

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-07 : 10:09:36
why are you making column as varchar if its containing date values? always try to use correct datatype

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

Go to Top of Page

lesponce
Starting Member

8 Posts

Posted - 2013-01-07 : 19:59:17
Thanks everyone...this resolved my issue CONVERT(VARCHAR(8),GETDATE(),112)

I agree about having a Date column defined as datetime. I'm populating a table created by a third party application. Thx again!
Go to Top of Page
   

- Advertisement -