1

Here I am Giving The Queries What I am using:

1.INSERT into sample values(convert(varchar,'19-11-2014 10:10:41',103))

2.INSERT into sample values (convert(varchar,'2014-11-19 10:10:41',103))

3.INSERT into sample values(convert(varchar,'11-19-2014 10:10:41',103))

The database Format is:yyyy/mm/dd HH:mm:ss:mmm

In Above Queries First one Throwing Error and Remaining Two Queries Working Fine.so How to insert Any Datetime Format into Sql No change in Query.

Please Reply As Early As Possible,Thank You.

1
  • What date is "02-03-2015"? The second of march, or february the third? You can't just accept both formats and expect "the system" to figure out the correct date. Commented Nov 19, 2014 at 10:11

3 Answers 3

1

Since you said using ASP.Net then why are you using plain SQL queries? Use parameters instead.

Lets say you have a date in string format 19/11/2014 and you need to insert into db so the correct way will be first convert the string into a date like

DateTime date= DateTime.ParseExact("19/11/2014 00:00:00","dd/MM/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);

now use parametrised query to insert see details-->http://www.aspsnippets.com/Articles/ASPNet-SqlDataSource-pass-value-to-SelectParameter-using-QueryString-Parameter-Example.aspx

Using parameters will serve you two main purpose

  1. You will be safe from SQL Injection attack
  2. the problem that you are currently facing with format of DateTime will go away since the serialization will be done by ASP.Net.
Sign up to request clarification or add additional context in comments.

Comments

0

this is very general , but complicated situation to get the input date which in proper format or not. Which ultimately gives an error at run-time , even in the production server we get the error and we rectify the issue hour and hour.

Ultimately sqlserver introduce the new function called "ISDATE" or TRY_PARSE-only sql2012 or above, which return the 1 if input data is date format.

declare @dTable table ( datecolumn datetime)

INSERT into @dTable values (case isdate('19-11-2014 10:10:41') when 1 then '19-11-2014 10:10:41' else null end )

INSERT into @dTable values ('2014-11-19 10:10:41')

INSERT into @dTable values ('11-19-2014 10:10:41')

select * from @dTable

If still you want , this is not right way, then you can create a function of dateformat, in which you can give string datevalue in any format and function give the data date or null return.

check this links.

http://www.codeproject.com/Articles/576178/cast-convert-format-try-parse-date-and-time-sql#4

create function convertStringIntoDate      
(
    @stringValue varchar(50)
)
RETURNS datetime
AS          
BEGIN          
    DECLARE @datereturn datetime          

    set @datereturn = TRY_PARSE( @stringValue)


RETURN @datereturn 
END 

or you can check this logic too.

declare @dt varchar(50) = '19-11-2014 10:10:41'

declare @dTable table ( datecolumn datetime)

INSERT into @dTable values (
case 
    when isdate(CONVERT( varchar(50), @dt)) = 1 then CONVERT( varchar(50), @dt) --'19-11-2014 10:10:41' 
    when isdate(CONVERT( varchar(50), @dt, 103) ) = 1 then CONVERT( datetime, @dt , 103 )  --'19-11-2014 10:10:41'  
    when isdate(CONVERT( varchar(50), @dt, 102) ) = 1 then CONVERT( datetime, @dt , 102 )  --'19-11-2014 10:10:41'  
    --when --give other format as above given and if not set in any dateformat , then simply return null
    else  
        null
end )


select * from @dTable

3 Comments

Thanks Ajay For responce but i want to return Date and so i can insert in DB I dont want any null values.
yes, for that you have to explicitly create a function or procedure which return the proper value, not throw error. please check link which I shared
Ya Got it I will Use Like This Thanks Ajay2707 For Responce.
0

Try this.

INSERT into sample values (convert(datetime,'19-11-2014 10:10:41',103))

INSERT into sample values (convert(datetime,'2014-11-19 10:10:41',102))

INSERT into sample values (convert(datetime,'11-19-2014 10:10:41',102))

1 Comment

Thanks For Response But the first Query is having 103 code and remaining having 102 but i dont want to change later in coding please get back to me with all formats should execute

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.