473,387 Members | 1,561 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Correct datetime syntax for MSSQL?

Hi,

Sometimes I find it handy to construct an sql string for action queries, and
then run it directly against the database and/or connection instead of using
Recordsets for simple updates.
eg. This query works in MS Access (using dbs.Execute):

UPDATE Customer SET LastContact = #2/5/2005 13:15:00#
WHERE Customer.CustomerID = 101;

I'm now upsizing an app to SQL server, using ADP front-end.
Date/Time fields were converted (by the wizard) to MSSQL datetime fields.

I'm finding that Date/Time literal expressions (formatted as #m/d/yyyy
hh:nn:ss#) are not acceptible to MSSQL? Running above using cnn.Execute
gives SQL syntax error at the # symbol.

If so, how do you write a literal date in an SQL query for executing against
SQL server?

Thanks,
Ian.
Nov 13 '05 #1
2 13946
On Sat, 15 Oct 2005 03:18:58 GMT, "Ian Hinson"
<pp******@bigpond.net.au> wrote:

In T-SQL, dates are wrapped in single quotes.
-Tom.

Hi,

Sometimes I find it handy to construct an sql string for action queries, and
then run it directly against the database and/or connection instead of using
Recordsets for simple updates.
eg. This query works in MS Access (using dbs.Execute):

UPDATE Customer SET LastContact = #2/5/2005 13:15:00#
WHERE Customer.CustomerID = 101;

I'm now upsizing an app to SQL server, using ADP front-end.
Date/Time fields were converted (by the wizard) to MSSQL datetime fields.

I'm finding that Date/Time literal expressions (formatted as #m/d/yyyy
hh:nn:ss#) are not acceptible to MSSQL? Running above using cnn.Execute
gives SQL syntax error at the # symbol.

If so, how do you write a literal date in an SQL query for executing against
SQL server?

Thanks,
Ian.


Nov 13 '05 #2
Use the Convert function against the string representation. e.g. with the
date as yyyy-mm-dd hh:mm:ss

CONVERT(datetime, '2005-02-05 13:15:00', 120)
Look in BOL for the CONVERT function for the list of formats converted.

--
Terry Kreft

"Ian Hinson" <pp******@bigpond.net.au> wrote in message
news:Cy******************@news-server.bigpond.net.au...
Hi,

Sometimes I find it handy to construct an sql string for action queries,
and then run it directly against the database and/or connection instead of
using Recordsets for simple updates.
eg. This query works in MS Access (using dbs.Execute):

UPDATE Customer SET LastContact = #2/5/2005 13:15:00#
WHERE Customer.CustomerID = 101;

I'm now upsizing an app to SQL server, using ADP front-end.
Date/Time fields were converted (by the wizard) to MSSQL datetime fields.

I'm finding that Date/Time literal expressions (formatted as #m/d/yyyy
hh:nn:ss#) are not acceptible to MSSQL? Running above using cnn.Execute
gives SQL syntax error at the # symbol.

If so, how do you write a literal date in an SQL query for executing
against SQL server?

Thanks,
Ian.

Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: bon | last post by:
I have a mssql view I am translating to oracle syntax One of the lines in the mssql view is like this: DATO=CASE (isnull(loandate,1)) WHEN 1 THEN 'N' ELSE 'Y' END In other words, the view will...
14
by: Andre | last post by:
Hello Can anyone help me translate this from access so that it can work in mssql (i need to get next value, but cannot use identity as if row is deleted, another must get new next column number...
1
by: thomas kern | last post by:
hi, i tried to insert a record into a mssql-table. one column is DateTime. i tried this one: insert into fixtures (dategame) VALUES ('24.01.2003') unfortunately i receive an error -- error...
2
by: va | last post by:
Why does this property "DateTime?" compile? public DateTime? test { get { return DateTime.Now; } }
0
by: Metalzed | last post by:
Hi I have a little problem If I use the this C# code DateTime.Now.ToString() output = 2006-11-03 06:15:17 But if i use this code in MSSQL (CONVERT(VARCHAR(35), { fn NOW() })) output= Nov 3...
2
by: gradeA | last post by:
Hi, I am having trouble inserting data into a datetime field in MSSQL. Here is the script I am trying to execute: $now = date('d/m/Y g:i:s A'); mssql_query("INSERT INTO clients...
4
by: pks83 | last post by:
In MSSQL I am using datetime as the column type and trying to execute the below mentioned query select time from tickets where ticket_id = '1'; when i execute the query on sql manager window it...
2
by: pks83 | last post by:
In MSSQL I am using datetime as the column type and trying to execute the below mentioned query select time from tickets where ticket_id = '1'; when i execute the query on sql manager window it...
2
by: akronymn | last post by:
I have an mssql database that I need to migrate to mysql. For some reason though any queries to any columns of type datetime are returned in French in the following format: 7/avr/2004 14:00 ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.