Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Update date/time field issues

Posted on 2011-09-27
4
Medium Priority
?
229 Views
Last Modified: 2012-06-27
Hi,

I'm currently widening my skill set from MS Access to SQL and hoping to move further on to vb.net, ado.net etc.

As part of this, I've moved the backend of our internal Access DB to SQL Server, and it's mostly functional (I'm still using bound tables at the moment).

I'm having problems with an UPDATE statement. I have in fact removed this completely from Access now, and I'm trying to execute it directly from the SQL server management environment.

I'm attempting to update a date/time field in a table.

The SQL I started with is as follows:

UPDATE [JJSQL].[dbo].[LBEJobs] SET [LBEJobs].[Priority_id] = 2, [LBEJobs].[To_complete_by] = 27/09/2011 10:27:56 WHERE [LBEJobs].[Job_id]=16172

That's the statement, word for word (I can't see any reason to hide names as this is internal/testing only).

I've had a look around, and found that I probably need to use the Convert function, so:

UPDATE [JJSQL].[dbo].[LBEJobs] SET [LBEJobs].[Priority_id] = 2, [LBEJobs].[To_complete_by] = Convert(VarChar,'27/09/2011 10:27:56',120) WHERE [LBEJobs].[Job_id]=16172

However, that brings back the error: The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

But if I execute the statement:

SELECT Convert(VarChar,'27/09/2011 10:27:56',120)

This does bring back a single field containing the date, as I'd expect.

The field I'm inserting in to, 'To_Complete_by' (not named by me I'll hasten to add) is a 'datetime' format.

I'm quite new to SQL, so the solution might be simple. Help!

Thanks.
0
Comment
Question by:jjnet123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 8

Accepted Solution

by:
jawa29 earned 2000 total points
ID: 36708626
This could be a simple case of the SQL server having the wrong Locale set. Try swapping the day and month in the second statement to see if it accepts it.

Jawa29
0
 
LVL 1

Author Closing Comment

by:jjnet123
ID: 36708641
Unbelievable. I'd spent all morning trying to work that out! I look forward to becoming a little more fluent so I have more confidence with SQL server in future.

Thanks again.
0
 
LVL 5

Expert Comment

by:DerZauberer
ID: 36708645
Converting text to text does not make much sense i think, what you might want to do is convert to datetime:

convert(datetime, '27/09/2011 10:27:56', 120)

and i think your date format looks british, so use 103 instead of 120 (odbc canonical):

http://msdn.microsoft.com/en-us/library/aa226054%28v=sql.80%29.aspx
0
 
LVL 9

Expert Comment

by:sachinpatil10d
ID: 36708670
Try this

Convert(datetime,'27/09/2011 10:27:56',103)
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question