Solved

ASP.NET format string as date

Posted on 2012-03-20
4
368 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
  • 2
4 Comments
 
LVL 12

Assisted Solution

by:Alfredo Luis Torres Serrano
Alfredo Luis Torres Serrano earned 167 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 166 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 167 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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

810 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