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

Convert dd/MM/yyyy to sql server format

Hi i have a value entered into an asp text box,
procedureDateTxtBx.Text, thet has the format dd/MM/yyyy. I need to
convert this into a format recognisable by SQL server in order to
properly query the database.

I've tried a few DateTime.Parse methods but can't get any to work.

The format of these values in the database is
yyyy-MM-dd hh:mm:ss.000

Thanks

Nov 17 '05 #1
8 12975
Hi,

What are you doing? are you using a SP or a query?

These are the steps:

1- Make sure the text is correct ! , why not use a Calendar instead of a
Textbox?

2- Convert the text to DateTime :
DateTime.ParseExact( textbox.Text, "dd/MM/yyyy" ,
DateTimeFormatInfo.InvariantInfo);
3- If you are using a sp, it's easy :
command.Parameters.Add("@assigneddate", SqlDbType.DateTime).Value =
DateTime.ParseExact( textbox.Text, "dd/MM/yyyy" ,
DateTimeFormatInfo.InvariantInfo);

4- If you are using a query then yo ushould use a parameterize query:
string selectSQL = "SELECT * FROM table1 WHERE tehdate= ? ";
SqlCommand selectCMD = new SqlCommand(selectSQL, theconn);
selectCMD.Parameters.Add("@thedate", SqlDbType.NVarChar, 15).Value =
DateTime.ParseExact( textbox.Text, "dd/MM/yyyy" ,
DateTimeFormatInfo.InvariantInfo);

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Assimalyst" <c_******@hotmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
Hi i have a value entered into an asp text box,
procedureDateTxtBx.Text, thet has the format dd/MM/yyyy. I need to
convert this into a format recognisable by SQL server in order to
properly query the database.

I've tried a few DateTime.Parse methods but can't get any to work.

The format of these values in the database is
yyyy-MM-dd hh:mm:ss.000

Thanks

Nov 17 '05 #2
Hi,

What are you doing? are you using a SP or a query?

These are the steps:

1- Make sure the text is correct ! , why not use a Calendar instead of a
Textbox?

2- Convert the text to DateTime :
DateTime.ParseExact( textbox.Text, "dd/MM/yyyy" ,
DateTimeFormatInfo.InvariantInfo);
3- If you are using a sp, it's easy :
command.Parameters.Add("@assigneddate", SqlDbType.DateTime).Value =
DateTime.ParseExact( textbox.Text, "dd/MM/yyyy" ,
DateTimeFormatInfo.InvariantInfo);

4- If you are using a query then yo ushould use a parameterize query:
string selectSQL = "SELECT * FROM table1 WHERE tehdate= ? ";
SqlCommand selectCMD = new SqlCommand(selectSQL, theconn);
selectCMD.Parameters.Add("@thedate", SqlDbType.NVarChar, 15).Value =
DateTime.ParseExact( textbox.Text, "dd/MM/yyyy" ,
DateTimeFormatInfo.InvariantInfo);

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Assimalyst" <c_******@hotmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
Hi i have a value entered into an asp text box,
procedureDateTxtBx.Text, thet has the format dd/MM/yyyy. I need to
convert this into a format recognisable by SQL server in order to
properly query the database.

I've tried a few DateTime.Parse methods but can't get any to work.

The format of these values in the database is
yyyy-MM-dd hh:mm:ss.000

Thanks

Nov 17 '05 #3
Assimalyst wrote:
Hi i have a value entered into an asp text box,
procedureDateTxtBx.Text, thet has the format dd/MM/yyyy. I need to
convert this into a format recognisable by SQL server in order to
properly query the database.

I've tried a few DateTime.Parse methods but can't get any to work.

The format of these values in the database is
yyyy-MM-dd hh:mm:ss.000

Thanks

To "properly" query the database, use a Command object and store the
date in the parameters, using the DateTime type, and letting the
database objects handle the transition from .NET to the database.

If you need to rewrite it so that it can be used in a string to query
the database, use DateTime.ToString("format here") and specify the
correct format, "yyyy-MM-dd HH:mm:ss.000" would probably do the trick.

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Nov 17 '05 #4
Assimalyst wrote:
Hi i have a value entered into an asp text box,
procedureDateTxtBx.Text, thet has the format dd/MM/yyyy. I need to
convert this into a format recognisable by SQL server in order to
properly query the database.

I've tried a few DateTime.Parse methods but can't get any to work.

The format of these values in the database is
yyyy-MM-dd hh:mm:ss.000

Thanks

To "properly" query the database, use a Command object and store the
date in the parameters, using the DateTime type, and letting the
database objects handle the transition from .NET to the database.

If you need to rewrite it so that it can be used in a string to query
the database, use DateTime.ToString("format here") and specify the
correct format, "yyyy-MM-dd HH:mm:ss.000" would probably do the trick.

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Nov 17 '05 #5
"Assimalyst" <c_******@hotmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
Hi i have a value entered into an asp text box,
procedureDateTxtBx.Text, thet has the format dd/MM/yyyy. I need to
convert this into a format recognisable by SQL server in order to
properly query the database.

I've tried a few DateTime.Parse methods but can't get any to work.

The format of these values in the database is
yyyy-MM-dd hh:mm:ss.000

Thanks


Use DateTime.ParseExact and pass it the exact format you are giving it.

--
Adam Clauss
Nov 17 '05 #6
"Assimalyst" <c_******@hotmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
Hi i have a value entered into an asp text box,
procedureDateTxtBx.Text, thet has the format dd/MM/yyyy. I need to
convert this into a format recognisable by SQL server in order to
properly query the database.

I've tried a few DateTime.Parse methods but can't get any to work.

The format of these values in the database is
yyyy-MM-dd hh:mm:ss.000

Thanks


Use DateTime.ParseExact and pass it the exact format you are giving it.

--
Adam Clauss
Nov 17 '05 #7

"Assimalyst" <c_******@hotmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
Hi i have a value entered into an asp text box,
procedureDateTxtBx.Text, thet has the format dd/MM/yyyy. I need to
convert this into a format recognisable by SQL server in order to
properly query the database.

I've tried a few DateTime.Parse methods but can't get any to work.


string SQL ="Select * from yourTable were yourDateColumn =
'"+this.procedureDateTxtBx.ToString()

that is open for SQL injection but it will work. You could also have a SP
in the db where your :
exec yourProc procedureDateTxtBx.ToString()

Nov 17 '05 #8

"Assimalyst" <c_******@hotmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
Hi i have a value entered into an asp text box,
procedureDateTxtBx.Text, thet has the format dd/MM/yyyy. I need to
convert this into a format recognisable by SQL server in order to
properly query the database.

I've tried a few DateTime.Parse methods but can't get any to work.


string SQL ="Select * from yourTable were yourDateColumn =
'"+this.procedureDateTxtBx.ToString()

that is open for SQL injection but it will work. You could also have a SP
in the db where your :
exec yourProc procedureDateTxtBx.ToString()

Nov 17 '05 #9

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

Similar topics

4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
2
by: Franck | last post by:
Hi, 'm gettin mad about date conversion. Here is the point. Got and add-in for Excel which call functions from a web service (on a remote server) The remote server has regional settings...
0
by: Assimalyst | last post by:
Hi i have a value entered into an asp text box, procedureDateTxtBx.Text, thet has the format dd/MM/yyyy. I need to convert this into a format recognisable by SQL server in order to properly query...
1
by: abcabcabc | last post by:
I write an application which can let user define own date format to input, How to convert the date string to date value with end-user defined date format? Example, User Defined Date Format as...
10
by: bonnie.tangyn | last post by:
Dear all In my ASP page, user can enter date in dd/mm/yyyy format. How can I use Javascript to convert dd/mm/yyyy to yyyy-mm-dd hh:mm:ss. Please give me some advices. Cheers Bon
2
by: kirke | last post by:
Hi, I have a datetime column named dtDateTime. its format is "Oct 27 2006 12:00:00 " I want to group by only date part of it and count my code is $sql1="SELECT ...
4
by: RP | last post by:
I am using SQL Server 2005 as backend. I have a Text Box that accepts Date in the format dd-MM-yyyy. But when I attempt to insert a record, an error is displayed: Cannot convert char to DateTime...
4
by: Ashraf Ansari | last post by:
Hi, How Can I convert MM/dd/yyyy format into dd/MM/yyyy and the date which is converted into dd/MM/yyyy should be in DateTime format. I do not want to store it as a string. Please help ...
3
by: Bobby Edward | last post by:
In mysql the db stores date/time as this: yyyy-mm-dd Is there an easy way to convert it to mm/dd/yyyy? How about if it shows up in a datagrid column? How about if the time is appended. For...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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,...

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.