?
Solved

ASP.NET format string as date

Posted on 2012-03-20
4
Medium Priority
?
388 Views
Last Modified: 2012-03-20
I have parts of a date, and I want to insert them to SQL server.
M="04"
D="26"
Y="1961"

How do I format these into SQL Server smalldatetime?
0
Comment
Question by:rrhandle8
[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
  • 2
4 Comments
 
LVL 12

Assisted Solution

by:Alfredo Luis Torres Serrano
Alfredo Luis Torres Serrano earned 501 total points
ID: 37744382
SqlStatement = "Insert into Table (DateField) Value (Convert(Smalldatetime,'" + D + '/' + M + '/' + y + "',103))"

Hope this helps
0
 
LVL 12

Expert Comment

by:Alfredo Luis Torres Serrano
ID: 37744393
The final statement should look like thie:

Insert into YourTable (TheFieldDate) Value (Convert(Smalldatetime,'26/04/1961',103))
0
 
LVL 20

Assisted Solution

by:BuggyCoder
BuggyCoder earned 498 total points
ID: 37744514
if you have these string in your asp.net code then create a datetime object out of them as:-

DateTime dt=new DateTime(Convert.ToIn32(Y),Convert.ToIn32(M),Convert.ToIn32(D));

You can also use Int.TryParse to check for null values.
Now You can simply insert this date into your sqlcommand's sqlparameter, which i am sure you know how to do....
0
 
LVL 7

Accepted Solution

by:
Lalit Chandra earned 501 total points
ID: 37744753
try this

string strdate = y+ "-" + m+ "-" + d;
INSERT INTO Table (datefield) VALUES ( '" + strdate + "')


The safest date format to use is probably yyyy-mm-dd hh:mm:ss, where the hour ranges from 0 to 23.  Some countries use dd/mm/yyyy, some use mm/dd/yyyy; the problem is that these two formats can be confused with one another for the first twelve days of each month.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

765 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