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
 datetime

Author  Topic 

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2013-10-22 : 22:38:35
i would like to add in column name on first row. i did this as below:

select * from (
select convert(nvarchar,'CaseDate')CaseDate
union all
select convert(nvarchar,dateadd(hour,8,CreatedOn)) CaseDate from tableA
)a

and i get this:

CaseDate
CaseDate
Oct 22 2013 1:00PM

how can i get the date like this?

2013-10-22 13:00:00.000

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2013-10-22 : 22:49:13
i got it.

I used this:
CONVERT(NVARCHAR, date, 121)
Go to Top of Page

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2013-10-22 : 22:55:43
what if i want to convert to dateadd hour 8?

select dateadd(hour,8,casedate)casedate from (
select CONVERT(NVARCHAR, 'CaseDate', 121)
union all
select CONVERT(NVARCHAR, CaseDate, 121) CaseDate from tableA
)a

i get this error:

Conversion failed when converting date and/or time from character string.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2013-10-23 : 00:28:19
[code]select 'CaseDate' as CaseDate
union all
select CONVERT(NVARCHAR(10), dateadd(hour, 8, CaseDate), 121) CaseDate from tableA[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-23 : 01:30:47
quote:
Originally posted by peace

what if i want to convert to dateadd hour 8?

select dateadd(hour,8,casedate)casedate from (
select CONVERT(NVARCHAR, 'CaseDate', 121)
union all
select CONVERT(NVARCHAR, CaseDate, 121) CaseDate from tableA
)a

i get this error:

Conversion failed when converting date and/or time from character string.


See this

http://visakhm.blogspot.in/2011/12/why-iso-format-is-recommended-while.html

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

- Advertisement -