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 Datetime into Varchar

Author  Topic 

Johnph
Posting Yak Master

103 Posts

Posted - 2013-05-06 : 17:02:29
[code]Convert(varchar(500),[DATE])FROM TABLE[/code]

My datetime fields looks like this:

2013-05-02 17:41:35.790

and I want the output as:

2013-05-02 17:41:35.790

but in varchar. My query is giving me May 5 2013.

Sorry if this has been asked before, but I could not find it.


James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-05-06 : 17:04:21
Use a specific style - for example
CONVERT(varchar(32),[DATE],120)
All the styles are listed here. http://msdn.microsoft.com/en-us/library/ms187928.aspx
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-07 : 01:02:13
I dont understand why you want to change datatype if the value is already in the format you desire. In any case formatting should be done at front end wherever possible. Doing this at sql requires unnecessary convert/cast operations and would evenmake any further manipulations using these values difficult.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Johnph
Posting Yak Master

103 Posts

Posted - 2013-05-07 : 10:32:25
Thanks to both.
Visakh - I want to convert everything into a varchar so that I can concatenate a large string with multiple columns.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-08 : 00:19:02
quote:
Originally posted by Johnph

Thanks to both.
Visakh - I want to convert everything into a varchar so that I can concatenate a large string with multiple columns.


Whats the purpose of doing this?
Is it for display or is it for sending them as delimited list to some other function? If latter, there are much better approaches like XML

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Johnph
Posting Yak Master

103 Posts

Posted - 2013-05-08 : 16:09:04
Yup, a delimited text file which I can use later for a bulk insert. Visakh, can you send me maybe a beginner's guide to XML with SQL server?
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-05-08 : 16:35:45
quote:
Originally posted by Johnph

Yup, a delimited text file which I can use later for a bulk insert. Visakh, can you send me maybe a beginner's guide to XML with SQL server?



Here are few useful links re: SQL XML


Stairway to XML at SSC: http://www.sqlservercentral.com/stairway/92778/

Jacob Sebastian's XQuery labs at BeyondRelational. I think he also has tutorials and other stuff: http://beyondrelational.com/modules/2/blogs/28/posts/10279/xquery-labs-a-collection-of-xquery-sample-scripts.aspx

Michael Cole's book: http://www.amazon.com/Pro-Server-2008-Experts-Voice/dp/1590599837
Go to Top of Page
   

- Advertisement -