Click here to Skip to main content
15,921,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi friends,

wats the insert statement in sql query to insert a date from a textbox, i need query along with date formats,
Posted

Hi,

The default format of sql is MM/DD/YYYY. So it is good to construct a sql query with the default format. Like MySql db we don't have any Date Format.

Sample Query:

SQL Server/Ms Access Database:

INSERT INTO tbl_tableName (Name,Address,DateOfBirth) VALUES('John','Trichy','11/31/2011');

MySql Database:
C#
public class MySQLDateFormat
       {
           public const string MONTH_DATE_YEAR = "%m/%d/%Y";
           public const string MONTH_DATE_YEAR_SEC = "%m/%d/%Y %H:%i:%s";

           public const string DATE_MONTH_YEAR = "%d/%m/%Y";

           public const string DATE_MONTH_YEAR_SEC = "%d/%m/%Y %H:%i:%s";

           public const string YEAR_MONTH_DATE = "%Y/%m/%d";
           public const string YEAR_MONTH_DATE_SEC = "%Y/%m/%d %H:%i:%s";

           public const string HOUR_MINUTE_SECONDS = "%H:%i:%s";
           public const string HOUR_MINUTE_SECONDS_AM_PM = "%H:%i:%s";
       }


Query:
INSERT INTO tbl_tableName (Name,Address,DateOfBirth) VALUES('John','Trichy',DATE_FORMAT('11/31/2011','%m/%d/%Y');

Note:
You can have a separate class like I have given above and concadinate in the date format place.
 
Share this answer
 
Comments
Vivek Shankar 2-Dec-11 4:23am    
thanku
RaviRanjanKr 2-Dec-11 18:25pm    
My 5+
check this out:

textbox with date format
C#
textbox1.text = DateTime.Now.ToString("yyyyMMddmmss");


insert query
SQL
insert into (value1) 
values(textbox1.text);



hope it helps,

don't forget to vote if it helps you

thanks
 
Share this answer
 
Comments
Vivek Shankar 2-Dec-11 4:16am    
can't we insert the date in this format dd-MM-yyyy?
RaviRanjanKr 2-Dec-11 18:25pm    
4+
D K N T H 4-Dec-11 20:33pm    
yes you can, try this: textbox1.text = DateTime.Now.ToString("dd-MM-yyyy");

thanks RaviRanjankr

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900