Wednesday, March 7, 2012

I need SQL Query Help

Sir,

We are try to insert query like this

Insert into Table_1 (smDate,Description) Values ('Thu, Feb 15, 2007 03:00 PM', ' Hai, It's Just for Testing')

This is Exact Query about this...

We know about MS ACCESS it has Built in function like Format('Thu, Feb 15, 2007 03:00 PM','mm/dd/yyyy HH:MM')

But I am new to SQL. I dont know the The Date Format built in function in SQL Server.

How can i Insert this Quesry to SQL Table

and also How can insert "it's " this type of words into SQL..

Can you help me

this is very helpful for my project

Thanks With Regards

S.Senthil Nathan

The quickest way i've used and seen being used to put an apostrophe into a SQL statment, is to do a replace on the string with a double quote, then you will need to do a reverse replace on the double quote back to an apostrophe, not the best way but one solution, the other is to escape the apostrophe with another so it would beit''sbut this would still require some parsing of the string.

Have a look at this artilce for working with formatting the date/time in SQL :http://sqljunkies.com/Article/6676BEAE-1967-402D-9578-9A1C7FD826E5.scuk

|||

For your date format issue, if you can remove the "Day, " from the date, you can insert the string you have. I.e. 'Feb 15, 2007 3:00 PM' is a valid date string and will be formatted automatically by SQL server according to how SQL server is set up. For example, if using the defaults on a US-based setup, SQL server formats the date like you requested.

if you need to insert something with single quotes, you have two options. First, you can replace it with two single quotes, i.e. 'it''s' would putit's in the database.

Second, you can use parameters. Your sql statement would be like "INSERT INTO dbo.MyTable (StringValue) Values (@.StringValue)", and then add a sql parameter to your sqlcommand as if you were using a stored procedure. If you had a Data.SqlClient.SqlCommand named command:

command.CommandType = Data.CommandType.Textcommand.CommandText = strSQL' this is your insert statement with a parametercommand.Parameters.AddWithValue("@.StringValue", "it's")
I prefer the second choice becuase it's easier than remembering to replace quotes and other sql syntax.

No comments:

Post a Comment