SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Updating datetime2 column not working


Updating datetime2 column not working

Author
Message
Glenn Tucker
Glenn Tucker
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11009 Visits: 1267
Hello,

Trying to update some datetime2 columns.

UPDATE T_MYTABLE SET dateTransferred = '2018-03-26 05.00.00' where id = 1223

Causes an error. So, I tried casting it.

UPDATE T_MYTABLE SET dateTransferred = Cast('2018-03-26 05.00.00' as datetime2) where id = 1223

Got another error. Trying using convert.

UPDATE T_MYTABLE SET dateTransferred = Convert(Datetime2,'2018-03-26 05.00.00',103) where id = 1223

Got an error.

Error message:

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

I must be missing something and I need another set of eyes. Can anyone see where my syntax is wrong?

Thanks.

Things will work out. Get back up, change some parameters and recode.
Glenn Tucker
Glenn Tucker
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11009 Visits: 1267
This is working with SQL Server 2016.

Things will work out. Get back up, change some parameters and recode.
ScottPletcher
ScottPletcher
SSC Guru
SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)

Group: General Forum Members
Points: 96763 Visits: 10337
Get rid of the dashes in the date, they can cause issues.

UPDATE T_MYTABLE SET dateTransferred = '20180326 05.00.00' where id = 1223

SQL DBA,SQL Server MVP(07, 08, 09) Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial:
If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (439K reputation)SSC Guru (439K reputation)SSC Guru (439K reputation)SSC Guru (439K reputation)SSC Guru (439K reputation)SSC Guru (439K reputation)SSC Guru (439K reputation)SSC Guru (439K reputation)

Group: General Forum Members
Points: 439408 Visits: 44207
Looks like the date strings are wrong. Take a close look at this: https://docs.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-2017

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Glenn Tucker
Glenn Tucker
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11009 Visits: 1267
Removing the dashes didn't seem to resolve it. Got the same error message.

Things will work out. Get back up, change some parameters and recode.
ScottPletcher
ScottPletcher
SSC Guru
SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)

Group: General Forum Members
Points: 96763 Visits: 10337
There must be a trigger(s) on the table that is incorrectly handling a date string (?).

SQL DBA,SQL Server MVP(07, 08, 09) Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial:
If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (439K reputation)SSC Guru (439K reputation)SSC Guru (439K reputation)SSC Guru (439K reputation)SSC Guru (439K reputation)SSC Guru (439K reputation)SSC Guru (439K reputation)SSC Guru (439K reputation)

Group: General Forum Members
Points: 439408 Visits: 44207
Look closely at the help page I posted the link to. Your string dates are wrong.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Glenn Tucker
Glenn Tucker
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11009 Visits: 1267
I do appreciate the thoughts and ideas. But I checked and no triggers. Can't remember when I was stomped by an update statement.Smile

Things will work out. Get back up, change some parameters and recode.
ScottPletcher
ScottPletcher
SSC Guru
SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)

Group: General Forum Members
Points: 96763 Visits: 10337
Yeah, you must use colons instead of dots in the time. I'm used to seeing either one ... but SQL isn't.

SELECT CAST('20180326 05:00:00' as datetime2)

SQL DBA,SQL Server MVP(07, 08, 09) Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial:
If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (439K reputation)SSC Guru (439K reputation)SSC Guru (439K reputation)SSC Guru (439K reputation)SSC Guru (439K reputation)SSC Guru (439K reputation)SSC Guru (439K reputation)SSC Guru (439K reputation)

Group: General Forum Members
Points: 439408 Visits: 44207
ScottPletcher - Monday, May 7, 2018 3:58 PM
Yeah, you must use colons instead of dots in the time. I'm used to seeing either one ... but SQL isn't.

SELECT CAST('20180326 05:00:00' as datetime2)


Duh!

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum









































































































































































SQLServerCentral


Search