Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 22:37pm
Edited 21-Nov-12 22: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
0 OriginalGriff 369
1 Sergey Alexandrovich Kryukov 365
2 Maciej Los 285
3 George Jonsson 185
4 King_Fisher 160
0 Sergey Alexandrovich Kryukov 10,934
1 Maciej Los 5,614
2 OriginalGriff 5,603
3 CPallini 3,879
4 BillWoodruff 3,579


Advertise | Privacy | Mobile
Web03 | 2.8.141030.1 | Last Updated 22 Nov 2012
Copyright © CodeProject, 1999-2014
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