Click here to Skip to main content
11,500,106 members (64,480 online)
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 Sergey Alexandrovich Kryukov 345
1 Maciej Los 290
2 DamithSL 230
3 Peter Leow 181
4 OriginalGriff 167
0 Sergey Alexandrovich Kryukov 335
1 Maciej Los 290
2 DamithSL 220
3 Peter Leow 181
4 OriginalGriff 167


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