473,414 Members | 1,626 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,414 software developers and data experts.

Date format in parameterised query.

Hello,

I am building a parameterised query in vb.net for execution against a SQL
server database.
I am using a OLEDB command and OLEDB parameters. If one of the parameters is
a date I sometimes experience a problem in the interpretation of the format.
I populate the parameter value from a user input text box. I am in the UK so
the use inputs in the format dd/mm/yy. I know SQL user the US format of
mm/dd/yy.

Is there any simple way of telling the query or the the connection or
anything, the data format I am using is dd/mm/yy.
I could convert the date to mm/dd/yy before setting the parameter value, but
the same logic is used for all data types.

Regards Tim.


Nov 20 '05 #1
5 10695
Assuming that the syntax of your query is correct and the parameter is
correctly defined as the appropriate date type then you feed it the value of
a date type. There is no need to format it however you may need to use
CDate(<textbox>.Text).

"Tim Marsden" <TM@UK.COM> wrote in message
news:Or**************@TK2MSFTNGP11.phx.gbl...
Hello,

I am building a parameterised query in vb.net for execution against a SQL
server database.
I am using a OLEDB command and OLEDB parameters. If one of the parameters is a date I sometimes experience a problem in the interpretation of the format. I populate the parameter value from a user input text box. I am in the UK so the use inputs in the format dd/mm/yy. I know SQL user the US format of
mm/dd/yy.

Is there any simple way of telling the query or the the connection or
anything, the data format I am using is dd/mm/yy.
I could convert the date to mm/dd/yy before setting the parameter value, but the same logic is used for all data types.

Regards Tim.

Nov 20 '05 #2
Thanks for Stephany's reply.

Tim,

I'd like to give more information on this. CDate() sometimes doesn't work
in different cultures. To specify the culture in parsing a date you can try
the following codes:

Dim dt As DateTime
Dim culture As IFormatProvider = New
System.Globalization.CultureInfo("en-GB")
dt = DateTime.Parse(Me.TextBox1.Text, culture)

dt will contain the DateTime value and you can assign it to a DateTime
parameter in a SQL statement.

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 20 '05 #3
Cor
Kevin Yu,
Normaly culture is normaly arranged by the operating system.
I think Stephany's answer is basicly correct (without error handling and
so).
Your answer makes the program culture dependable.
I think that when a culture fails it is better to look for the
language-culture settings in the computer (and in 9X computers the keyboard
settings )
Cor
Nov 20 '05 #4
Tim,

Agree with Stephany & Kevin.

You have a 2 stage problem:
1 - Get a valid correct date from user input text
2 - Pass date to DB.

Don't combine the 2 and pass the input string direct to the DB - bad
coding and lays you open to attack.

The first is culture specific but I don't find this reliable (an
English user on a French client talking to a server in the US??). I
prefer to keep control over date formats by using a UserProfile or
being clear. I accept this may not be an option in public
applications (as opposed to identified users). If you use a
prescribed date format use a Validator to check the format and save a
server round trip.

The second should not be an issue if you use parameters. If you don't
use parameters then use an unambigous format.

SqlCommand.Text = "SELECT ... FROM ... WHERE ( START_DATE = '" +
myDate.ToString("yyyy/MM/dd") + "')"
- but its better to use parameters

Charles

"Tim Marsden" <TM@UK.COM> wrote in message
news:Or**************@TK2MSFTNGP11.phx.gbl...
Hello,

I am building a parameterised query in vb.net for execution against a SQL server database.
I am using a OLEDB command and OLEDB parameters. If one of the parameters is a date I sometimes experience a problem in the interpretation of the format. I populate the parameter value from a user input text box. I am in the UK so the use inputs in the format dd/mm/yy. I know SQL user the US format of mm/dd/yy.

Is there any simple way of telling the query or the the connection or anything, the data format I am using is dd/mm/yy.
I could convert the date to mm/dd/yy before setting the parameter value, but the same logic is used for all data types.

Regards Tim.

Nov 20 '05 #5
Hi Cor,

Thanks for you reply. Stephany's answer is right if the culture of the
machine has already been set to English (United Kingdom). My code makes the
input fixed to the dd/mm/yy style.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 20 '05 #6

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

Similar topics

7
by: vnl | last post by:
I'm trying to run a SQL query but can't find any records when trying to select a certain date. Here's the sql: SELECT field 1, field2, date_and_time, FROM table1 WHERE date_and_time =...
6
by: kevin carter | last post by:
hi i have a table conataining several fields one of which is date i want to be able to search the table on the date field using code. the code below generates the query from a form, however i get...
3
by: Lyn | last post by:
Hi, I am developing a project in which I am checking for records with overlapping start/end dates. Record dates must not overlap date of birth, date of death, be in the future, and must not...
5
by: Macca | last post by:
Hi, I have a table which has a date/time field. I am storing them as follows :- 01/01/2005 11:25 01/01/2005 19:44 02/01/2005 05:04
11
by: Dixie | last post by:
How can I programatically, take some Date/Time fields present in a table in the current database and change their type to text? dixie
3
by: glenn | last post by:
Hi folks, I have 2 code snippets. One works and one does not. This first hard-coded snippet works Dim queryString As String = "UPDATE SET = '8/16/2006' WHERE (. = @id)" This second...
1
by: jimmy | last post by:
I'm trying to insert a date into a MySQL date column. The string i am trying to insert takes the following format: 2007-02-23 which corresponds to the date format that MySQL uses which is...
2
by: thewilldog | last post by:
Hello, I've reviewed the archives here to address the issue, but I'm still running into problems. I've got a table field populated with the record date in text "YYYYMMDD" To convert it into a...
7
by: pb | last post by:
Hi, I am updating a database based and adding the time the entry was added with the query below... strQuery = "INSERT INTO Visits ( EmailAddress, , TimeCreated ) values ('" & emailaddress &...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.