Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 393
  • Last Modified:

ASP.NET format string as date

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
rrhandle8
Asked:
rrhandle8
  • 2
3 Solutions
 
Alfredo Luis Torres SerranoDevelopment Director / DBACommented:
SqlStatement = "Insert into Table (DateField) Value (Convert(Smalldatetime,'" + D + '/' + M + '/' + y + "',103))"

Hope this helps
0
 
Alfredo Luis Torres SerranoDevelopment Director / DBACommented:
The final statement should look like thie:

Insert into YourTable (TheFieldDate) Value (Convert(Smalldatetime,'26/04/1961',103))
0
 
BuggyCoderCommented:
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
 
Lalit ChandraCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now