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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Date Format to VarChar Column
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

8 Posts

Posted - 01/06/2013 :  21:39:08  Show Profile  Reply with Quote
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?

Posting Yak Master

United Kingdom
226 Posts

Posted - 01/07/2013 :  03:32:36  Show Profile  Visit theboyholty's Homepage  Reply with Quote
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.

--------------------------------------------------------------------------------- A Bury FC supporters website and forum
Go to Top of Page

Premature Yak Congratulator

22864 Posts

Posted - 01/07/2013 :  07:58:29  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
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


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

Very Important crosS Applying yaK Herder

52326 Posts

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

SQL Server MVP

Go to Top of Page

Starting Member

8 Posts

Posted - 01/07/2013 :  19:59:17  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000