Skip to main content

Posts

Showing posts with the label #SQL

SQL Server - How to get first and last day of month

----Today SELECT GETDATE () 'Today' ----Yesterday SELECT DATEADD ( d ,- 1 , GETDATE ()) 'Yesterday' ----First Day of Current Week SELECT DATEADD ( wk , DATEDIFF ( wk , 0 , GETDATE ()), 0 ) 'First Day of Current Week' ----Last Day of Current Week SELECT DATEADD ( wk , DATEDIFF ( wk , 0 , GETDATE ()), 6 ) 'Last Day of Current Week' ----First Day of Last Week SELECT DATEADD ( wk , DATEDIFF ( wk , 7 , GETDATE ()), 0 ) 'First Day of Last Week' ----Last Day of Last Week SELECT DATEADD ( wk , DATEDIFF ( wk , 7 , GETDATE ()), 6 ) 'Last Day of Last Week' ----First Day of Current Month SELECT DATEADD ( mm , DATEDIFF ( mm , 0 , GETDATE ()), 0 ) 'First Day of Current Month' ----Last Day of Current Month SELECT DATEADD ( ms ,- 3 , DATEADD ( mm , 0 , DATEADD ( mm , DAT...

SQL Server - Call a webservice/WebAPI from TSQL code?

Stored Procedure to call Web Service sp_configure 'Ole Automation Procedures', 1 GO RECONFIGURE; GO sp_configure 'show advanced options', 1 GO RECONFIGURE; CREATE PROCEDURE CALLWEBSERVICE AS BEGIN     Declare @Object as Int;     Declare @ResponseText as Varchar(8000);     Exec sp_OACreate 'MSXML2.XMLHTTP', @Object OUT;     Exec sp_OAMethod @Object, 'open', NULL, 'get', 'https://jsonplaceholder.typicode.com/todos/1','false'     Exec sp_OAMethod @Object, 'send'     Exec sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT     Select @ResponseText     Exec sp_OADestroy @Object END Ref: https://stackoverflow.com/questions/33449/can-you-call-a-webservice-from-tsql-code

SQL Server Backup to multiple files (file size is different)

Please Use this Script to Backup your db in 10 files BACKUP DATABASE [Databas_Name] TO DISK = 'C:\MSSQL_Backup\Database_FileName1.bak', DISK = 'C:\MSSQL_Backup\Database_FileName2.bak', DISK = 'C:\MSSQL_Backup\Database_FileName3.bak', DISK = 'C:\MSSQL_Backup\Database_FileName4.bak', DISK = 'C:\MSSQL_Backup\Database_FileName5.bak', DISK = 'C:\MSSQL_Backup\Database_FileName6.bak', DISK = 'C:\MSSQL_Backup\Database_FileName7.bak', DISK = 'C:\MSSQL_Backup\Database_FileName8.bak', DISK = 'C:\MSSQL_Backup\Database_FileName9.bak', DISK = 'C:\MSSQL_Backup\Database_FileName10.bak' WITH INIT , NOUNLOAD , NAME = 'Databas_Name backup', NOSKIP , STATS = 10 , NOFORMAT  

How to Query Comma Separated Value Column in SQL

Introduction This is a small SQL function which will be useful for querying a Table which is having a comma separated value column. Background Sometimes, you encounter a column that stores comma separated values. This solution makes it easy to search such a column. However, it is recommended, if possible, to split the tables into two according to the First Normal Form . For example : Storing the days of week in CSV format [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Sunday], for months in numbers [1,2,3,4,5,6,7,8,9,10,11,12] I came across this requirement for storing the days in CSV format as reminder follow up for the user, and to query it. Problem Sometimes, we used to store a comma separated value in a column and which we will be using for some purpose in order to avoid creating a new table for that functionality. In this case, equals = search or contains Search or Like query will not work as expected to find one or more item presen...

with(nolock) or (nolock) - Is there a difference?

There is no functional difference, but eventually the syntax without   WITH   will not work. This has been deprecated: select customer , zipcode from customers c ( nolock ) So you should be using this format: select customer , zipcode from customers c with ( nolock ) Not using the   WITH   keyword for table hints has been deprecated since at least SQL Server 2008. Search the following topic for the phrase   Specifying table hints without using the WITH keyword. : http://msdn.microsoft.com/en-us/library/ms143729%28SQL.100%29.aspx Ref :  https://stackoverflow.com/questions/12112855/withnolock-or-nolock-is-there-a-difference

75 Important queries in SQL Server every developer should know

1. Create Table: In this SQL Server query we will learn How to create table in SQL Server . 2. Create table with primary key: In this query we will Create SQL Server table with primary key . 1 2 3 4 5 6 CREATE TABLE TableName (     Id INT PRIMARY KEY,     Name Nvarchar(500),     Age INT ) 3. Create table with primary key and Identity Column: 1 2 3 4 5 6 CREATE TABLE TableName (     Id INT  IDENTITY(1,1) PRIMARY KEY,     Name Nvarchar(500),     Age INT ) 4. Insert values into SQL Server table: In this SQL Server query we will learn about How to insert values into Sql Server table. 1 INSERT INTO TableName (Name,Age) VALUES ( 'Max' ,30); 5. Insert multiple rows in a single query: In this Sql query we will learn How to Insert Multiple Rows in a Single SQL Query . This query is very important and generall...