Showing posts with label SQL Server. Show all posts
Showing posts with label SQL Server. Show all posts

Wednesday, July 29, 2015

Convert datetime to MMDDYY format in sql server

If you ever needed to convert datetime field to MMDDYY format in sql server, read on. I am going to present you a quick tips on how to get date or datetime field in sql server in MMDDYY format. Let's do it step by step.

First of all I would like to remind you to learn how to convert date to different date string using Convert function of ms sql server. This will prepare background for the technique used in this article.

For the sake of easiness, I will use current date and time as the date field we would like to convert to MMDDYY format. As you all know, getdate() returns you current date and time, as below:

select getdate() as Today
Output is: 2015/07/29 05:00:47.563

First we will get this date in mm/dd/yyyy format. Here is how:
select convert(varchar(10),getdate(),1)
Output is: 07/29/15

Now we are almost there! Let replace those forward slashes (/) with empty string. And voila! We have MMDDYY. Here is the script:

select replace(convert(varchar(10),getdate(),1),'/','')
Output is: 072915

All the steps are put together below.
















Now we have successfully converted datetime field in sql server into MMDDYY format.

Friday, April 27, 2012

Expecting non-empty string for 'providerInvariantName' parameter - database connection error in asp.net web application

Just a simple accidental error in asp.net web application's web.config file, and you see the following error:

Expecting non-empty string for 'providerInvariantName' parameter. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Expecting non-empty string for 'providerInvariantName' parameter.
Fig. 1 Error page showing providerInvariantName parameter error for asp.net web application's database connection error
 The error occurred only because I had missed the providerName property in my connection string.
My connection string is:
<connectionStrings>
  <add name="ConnectionString" connectionString="data source=my-pc;Integrated Security=true;Initial Catalog=mydbname"
   providerName="System.Data.SqlClient" />
  </connectionStrings>
And accidentally it was broken like:
<connectionStrings>
  <add name="ConnectionString" connectionString="data source=my-pc;Integrated Security=true;Initial Catalog=mydbname" />
  </connectionStrings>
I was stunned to get the error when I had just finished uploading the web application to my host. After some googling, I found the cause and fixed the error. It worked. Happy programming!

Thursday, March 22, 2012

The database principal owns a schema in the database, and cannot be dropped

While restoring a database backup file in my hosting I was prompted with some error message regarding existing user in the database. So I decided to delete those users. But while trying to delete the users from my local sql sever management studio, error occurred.
The database principal owns a schema in the database, and cannot be dropped.
I tried out browsing the property of the user and removing the assigned scheme from the lists. It was somehow unsucessful. So I decided to go search for the solution and stumbled at doing it the most powerful way - through command.
Here goes the solution - Revoke the database schema assigned to the user and assign it back to the default db object.
ALTER AUTHORIZATION ON SCHEMA :: my_user_name to db_owner
 If you want to assign the schema  to another user, here is how you accomplish it.
 ALTER AUTHORIZATION ON SCHEMA :: my_schema_name to my_user_name
Cheers!
kick it on DotNetKicks.com

Popular Posts

Recent Articles