Posts

Custom Function string_split for SQL Server 2008R2 (T-SQL split string)

For Those who have problem using  string_split function in SQL Server 2008R2 CREATE FUNCTION dbo.string_split (     @stringToSplit VARCHAR(MAX),     @separator VARCHAR(MAX) ) RETURNS   @returnList TABLE ([value] [nvarchar] (500)) AS BEGIN  DECLARE @name NVARCHAR(255)  DECLARE @pos INT  WHILE CHARINDEX(@separator, @stringToSplit) > 0  BEGIN   SELECT @pos  = CHARINDEX(@separator, @stringToSplit)    SELECT @name = SUBSTRING(@stringToSplit, 1, @pos-1)   INSERT INTO @returnList   SELECT @name   SELECT @stringToSplit = SUBSTRING(@stringToSplit, @pos+1, LEN(@stringToSplit)-@pos)  END  INSERT INTO @returnList  SELECT @stringToSplit  RETURN END

Find the number of columns in a table

Though its a rare situation when we need to know the number of columns in a table. You can get the number of columns information for a particular table by running the following query on SQL Server SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_catalog = ' Your_Database_Name '  AND table_name = ' Your_Table_Name ' Good luck!

How To Generate Random Dates Between Two Date Range in SQL Server

Those who wants to generate random dates between two date ranges in SQL Server can use the following query: DECLARE @FromDate DATETIME = DATEADD(DAY, -2, '2011-01-01') DECLARE @ToDate   DATETIME = DATEADD(DAY, -1, '2017-01-01') DECLARE @Seconds INT = DATEDIFF(SECOND, @FromDate, @ToDate) DECLARE @Random INT = ROUND(((@Seconds-1) * RAND()), 0) DECLARE @Milliseconds INT = ROUND((999 * RAND()), 0) SELECT DATEADD(MILLISECOND, @Milliseconds, DATEADD(SECOND, @Random, @FromDate)) Hope it works for you!

Find all tables and views containing column with specified name

Many of us some times wants to know that where a specific column is located as reference to other tables or views. If this is the case for you, then you can execute the following query:  Just replace the YourColumnName with your desired column name.  SELECT      COLUMN_NAME AS 'ColumnName' , TABLE_NAME AS  'TableName' FROM          INFORMATION_SCHEMA.COLUMNS WHERE       COLUMN_NAME LIKE '% YourColumnName %' ORDER BY  TableName , ColumnName; Please note that, if you follow the convention to place every where with same column name, then it will brings good output result for you. Thanks

No 'Access-Control-Allow-Origin' header is present on the requested resource

Most of us will saw this error message in web browser console while working with ASP.NET WEB API. The reason behind this error is CORS. That means Cross Origin Resource Sharing. When we try to access a web service from our application, if the application domain and the web service domain is different, then this problem will be occurs. For example: Suppose our application is running on www.examplesite.com from where we are trying to access the web service which domain address is www.exampleservice.com/api/employee . Since the domain address is different, it through the error. What we can do? In our Web API Application: 1. Go to Tools 2. Nuget Package Manager 3. Package Manager Console 4. Write  install-package Microsoft.AspNet.WebApi.Cors 5. Now go to WebApiConfig.cs file 6. Paste the following lines: EnableCorsAttribute  cors =  new   EnableCorsAttribute ( "*" ,  "*" ,  "*" ); config.EnableCors(cors); and you are DONE!

ASP.NET Interview Questions And Answers

While you facing an interview for ASP.NET Developer, you might be asked several question related to ASP.NET and other development area. Here i am sharing a good source of information for some sample  ASP.NET Interview Questions And Answers . Hope it will help you all a lot. 

50 Important Queries in SQL Server

Sometimes we need some sql server query which is really hard to find from the internet in one place. Here in this link, you can find  50 Important Queries in SQL Server.