x
login about faq Site discussion (meta-askssc)

Nvarchar conversion changing datetime from style 120 to 109

I am doing an up date statement like the following:

update table
set table.dateUpdated=convert(datetime,table2.d1,120)
from table inner join table2 on table.id=table2.id
where table.dateupdated is null

table.dateupdated is nvarchar(255) so when the update happens it's converting the datetime to style 109.

Here's a test:

select convert(datetime,'9/16/2011',120)
select cast(convert(datetime,'9/16/2011',120) as nvarchar(255))  

How do I get the style to stay at 120 with in the nvarchar datatype??

Thanks! K

more ▼

asked Sep 16 '11 at 11:15 AM in Default

katie 2 gravatar image

katie 2
74 6 6 8

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

To get a style in the nvarchar, you need to specify it when converting to the nvarchar, so:

select convert(nvarchar,(convert(datetime,'9/16/2011',120)),120)

gives

2011-09-16 00:00:00

in fact the inner convert to 120, is un-needed

select convert(nvarchar,(convert(datetime,'9/16/2011')),120)
--or
select convert(nvarchar,(cast('9/16/2011' as datetime)),120)

both give

2011-09-16 00:00:00
more ▼

answered Sep 16 '11 at 11:40 AM

Kev Riley gravatar image

Kev Riley ♦♦
46.1k 38 43 69

Ok, so when I do the update the above scenario does not work but if I do a select it does. I need the value stored to be in the style 120 format, so i need to get it working in the update statement.

update table set Phase_1_Date_Delivered=convert(datetime,Phase_1_Date_Delivered,120)

This still converts back to style 109.

Thanks for your help!

Sep 16 '11 at 12:04 PM katie 2

just tried the double convert again and I must have been doing something wrong because it appears to have converted properly.

Sep 16 '11 at 12:09 PM katie 2

double checked and it's for sure for sure! :) Thanks so much. Fyi..I did the double convert method.

Sep 16 '11 at 12:16 PM katie 2
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x1834
x84
x22

asked: Sep 16 '11 at 11:15 AM

Seen: 1334 times

Last Updated: Sep 16 '11 at 11:41 AM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.