SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

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

lesponce
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?

theboyholty
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.

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

madhivanan
Premature Yak Congratulator

India
22777 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

Madhivanan

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

visakh16
Very Important crosS Applying yaK Herder

India
52325 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
http://visakhm.blogspot.com/

Go to Top of Page

lesponce
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  
 New 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.05 seconds. Powered By: Snitz Forums 2000