Click here to Skip to main content
12,631,109 members (41,290 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: SQL-Server ASP.NET4
Hi,

I require datetime format as 'YYYY-MM-DD HH:MM:SS' from sql server data table where I've stored date, time in separate columns.

I tried to convert in code behind

DateTime.ParseExact(ds.tables(0).rows(0).item(0).ToString & " " & ds.tables(0).rows(0).item(1), "YYYY-MM-DD HH:MM:SS", CultureInfo.InvariantCulture)

Error: String was not recognized as a valid DateTime.

I also tried to convert and select in sql server query
select convert(datetime, DateColumnName + TimeColumnName, 120) as NewDatetime, from TableName

Error: Operand data type date is invalid for add operator.


But I get errors

Help me to resolve it.

Thanks
Posted 21-Nov-12 23:37pm
Updated 21-Nov-12 23:39pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Try
SELECT replace(convert(varchar, DateColumnName, 111), '/', '-') + ' ' + convert(varchar, TimeColumnName,108) AS NewDatetime FROM TableName
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

try something like below :
SELECT CONVERT(VARCHAR(10), GETDATE(), 120) + ' ' + CONVERT(VARCHAR, GETDATE(),108) AS NEWDATETIME
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161205.3 | Last Updated 22 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100