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, 2011 at 11:15 AM in Default

avatar image

katie 2
74 6 6 10

(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)


 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)
 select convert(nvarchar,(cast('9/16/2011' as datetime)),120)

both give

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

answered Sep 16, 2011 at 11:40 AM

avatar image

Kev Riley ♦♦
66.8k 48 65 81

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, 2011 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, 2011 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, 2011 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



Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.



asked: Sep 16, 2011 at 11:15 AM

Seen: 2762 times

Last Updated: Sep 16, 2011 at 11:41 AM

Copyright 2018 Redgate Software. Privacy Policy