Solved

ASP.NET format string as date

Posted on 2012-03-20
4
387 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 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

695 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