<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SQL Server - MSSQL Query</title>
	<atom:link href="https://mssqlquery.com/category/sql-server/feed" rel="self" type="application/rss+xml" />
	<link>https://mssqlquery.com</link>
	<description>MSSQL and TSQL Programming and TSQL Examples</description>
	<lastBuildDate>Sun, 03 Sep 2023 23:17:35 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.3.1</generator>

<image>
	<url>https://mssqlquery.com/wp-content/uploads/2023/06/cropped-mssql-query-icon-32x32.png</url>
	<title>SQL Server - MSSQL Query</title>
	<link>https://mssqlquery.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Supply a Valid Database Name in SQL Server. To See Available Databases, Use Sys.Databases Error</title>
		<link>https://mssqlquery.com/supply-a-valid-database-name-in-sql-server-to-see-available-databases-use-sys-databases-error</link>
		
		<dc:creator><![CDATA[Yavuz Selim Kart]]></dc:creator>
		<pubDate>Sun, 03 Sep 2023 23:14:42 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[See Available Databases]]></category>
		<category><![CDATA[Supply a Valid Database Name in SQL Server]]></category>
		<category><![CDATA[Sys.Databases Error]]></category>
		<guid isPermaLink="false">https://mssqlquery.com/?p=1653</guid>

					<description><![CDATA[<p>Hello everyone. In this article, Supply a valid database name in SQL Server. I will give information about To see available databases, use sys.databases. In SQL Server, you may encounter such an error in some cases. You can solve this error by using the code below. USE master; ALTER DATABASE&#46;&#46;&#46;</p>
<p>The post <a href="https://mssqlquery.com/supply-a-valid-database-name-in-sql-server-to-see-available-databases-use-sys-databases-error">Supply a Valid Database Name in SQL Server. To See Available Databases, Use Sys.Databases Error</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Hello everyone. In this article, Supply a valid database name in SQL Server. I will give information about To see available databases, use sys.databases.</p>
<p>In SQL Server, you may encounter such an error in some cases.</p>
<p>You can solve this error by using the code below.</p>
<pre class="line-numbers"><code class="language-sql">USE master;
ALTER DATABASE Old_Database_Name SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
ALTER DATABASE Old_Database_Name MODIFY NAME = [New_Database_Name];
ALTER DATABASE Old_Database_Name SET MULTI_USER;</code></pre>
<p>Good luck to everyone in business and life.</p>
<div class='epvc-post-count'><span class='epvc-eye'></span>  <span class="epvc-count"> 6</span><span class='epvc-label'> Views</span></div><p>The post <a href="https://mssqlquery.com/supply-a-valid-database-name-in-sql-server-to-see-available-databases-use-sys-databases-error">Supply a Valid Database Name in SQL Server. To See Available Databases, Use Sys.Databases Error</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Error 15023 in SQL Server</title>
		<link>https://mssqlquery.com/error-15023-in-sql-server</link>
		
		<dc:creator><![CDATA[Yavuz Selim Kart]]></dc:creator>
		<pubDate>Sat, 12 Aug 2023 19:47:37 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Error 15023 in SQL Server]]></category>
		<category><![CDATA[Error 15023 SQL Server]]></category>
		<category><![CDATA[TSQL 15023]]></category>
		<guid isPermaLink="false">https://mssqlquery.com/?p=1648</guid>

					<description><![CDATA[<p>Hello everyone. In this article, I will provide information about error 15023 in SQL Server. When you restore from backup in SQL Server, when you restore to a different server, you may get such an error because the user record does not match. To fix the user and login mapping,&#46;&#46;&#46;</p>
<p>The post <a href="https://mssqlquery.com/error-15023-in-sql-server">Error 15023 in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Hello everyone. In this article, I will provide information about error 15023 in SQL Server.</p>
<p>When you restore from backup in SQL Server, when you restore to a different server, you may get such an error because the user record does not match.</p>
<p>To fix the user and login mapping, you need to open a query window in SQL Server Management Studio and run the following code on the relevant database.</p>
<pre class="line-numbers"><code class="language-sql">USE [IslemYapacaginizVeritabaniAdi]
EXEC sp_change_users_login 'Auto_Fix','VeritabaniKullaniciAdi'</code></pre>
<p>Good luck to everyone in business and life.</p>
<p>&nbsp;</p>
<div class='epvc-post-count'><span class='epvc-eye'></span>  <span class="epvc-count"> 268</span><span class='epvc-label'> Views</span></div><p>The post <a href="https://mssqlquery.com/error-15023-in-sql-server">Error 15023 in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Listing All Jobs Running in SQL Server</title>
		<link>https://mssqlquery.com/listing-all-jobs-running-in-sql-server</link>
		
		<dc:creator><![CDATA[Yavuz Selim Kart]]></dc:creator>
		<pubDate>Fri, 11 Aug 2023 10:46:48 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Listing All Jobs]]></category>
		<category><![CDATA[sql server Listing All Jobs]]></category>
		<category><![CDATA[tsql Listing All Jobs]]></category>
		<guid isPermaLink="false">https://mssqlquery.com/?p=1643</guid>

					<description><![CDATA[<p>Hello everyone. In this article, I will provide information about listing all running jobs in SQL Server. In certain situations, you might want to list all the running jobs in SQL Server. You can easily perform this operation with the help of the code below: SELECT database_name, notify_level_email, name, enabled,&#46;&#46;&#46;</p>
<p>The post <a href="https://mssqlquery.com/listing-all-jobs-running-in-sql-server">Listing All Jobs Running in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Hello everyone. In this article, I will provide information about listing all running jobs in SQL Server.</p>
<p>In certain situations, you might want to list all the running jobs in SQL Server.</p>
<p>You can easily perform this operation with the help of the code below:</p>
<pre class="line-numbers"><code class="language-sql">SELECT database_name,
       notify_level_email,
       name,
       enabled,
       description,
       step_name,
       command
FROM msdb.dbo.sysjobs job
    INNER JOIN msdb.dbo.sysjobsteps steps
        ON job.job_id = steps.job_id
WHERE job.enabled = 1;</code></pre>
<p>When you execute the above code, you will see a result similar to the following:</p>
<p><img decoding="async" fetchpriority="high" class="size-full wp-image-1646" src="https://mssqlquery.com/wp-content/uploads/2023/08/listing-all-jobs-running-in-sql-server-1.jpg" alt="" width="700" height="371" srcset="https://mssqlquery.com/wp-content/uploads/2023/08/listing-all-jobs-running-in-sql-server-1.jpg 700w, https://mssqlquery.com/wp-content/uploads/2023/08/listing-all-jobs-running-in-sql-server-1-300x159.jpg 300w" sizes="(max-width: 700px) 100vw, 700px" /></p>
<p>As you can see, all jobs have been listed.</p>
<p>Wishing everyone success and ease in their professional and personal lives.</p>
<div class='epvc-post-count'><span class='epvc-eye'></span>  <span class="epvc-count"> 16</span><span class='epvc-label'> Views</span></div><p>The post <a href="https://mssqlquery.com/listing-all-jobs-running-in-sql-server">Listing All Jobs Running in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Converting XML to Table in SQL Server</title>
		<link>https://mssqlquery.com/converting-xml-to-table-in-sql-server</link>
		
		<dc:creator><![CDATA[Yavuz Selim Kart]]></dc:creator>
		<pubDate>Thu, 22 Jun 2023 10:43:06 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server XML to Table]]></category>
		<category><![CDATA[SQL XML to Table]]></category>
		<category><![CDATA[XML to Table]]></category>
		<guid isPermaLink="false">https://mssqlquery.com/?p=1635</guid>

					<description><![CDATA[<p>Hello everyone. In this article, I will try to give information about converting XML to table in SQL Server. In SQL Server you may want to convert XML to table in some cases. You can easily do this by revising the sample code below. DECLARE @TempTablo TABLE ( KullaniciId INT&#46;&#46;&#46;</p>
<p>The post <a href="https://mssqlquery.com/converting-xml-to-table-in-sql-server">Converting XML to Table in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Hello everyone. In this article, I will try to give information about converting XML to table in SQL Server.</p>
<p>In SQL Server you may want to convert XML to table in some cases.</p>
<p>You can easily do this by revising the sample code below.</p>
<pre class="line-numbers"><code class="language-sql">DECLARE @TempTablo TABLE (
	KullaniciId INT
   ,KullaniciAdi NVARCHAR(50)
   ,Sifre NVARCHAR(50)
)
DECLARE @xml XML
SET @xml = '
&lt;row KullaniciId="1" KullaniciAdi="Yavuz" Sifre="1234" /&gt;
&lt;row KullaniciId="2" KullaniciAdi="Selim" Sifre="5678" /&gt;'

INSERT INTO @TempTablo
	SELECT
		Tbl.Col.value('@KullaniciId', 'INT')
	   ,Tbl.Col.value('@KullaniciAdi', 'NVARCHAR(50)')
	   ,Tbl.Col.value('@Sifre', 'NVARCHAR(50)')
	FROM @xml.nodes('//row') Tbl (Col)

SELECT
	*
FROM @TempTablo</code></pre>
<p>When you run the above code, you will see a result similar to the one below.</p>
<div id="attachment_1637" style="width: 710px" class="wp-caption alignnone"><img aria-describedby="caption-attachment-1637" decoding="async" class="size-full wp-image-1637" src="https://mssqlquery.com/wp-content/uploads/2023/06/converting-xml-to-table-in-sql-server-1.jpg" alt="" width="700" height="422" srcset="https://mssqlquery.com/wp-content/uploads/2023/06/converting-xml-to-table-in-sql-server-1.jpg 700w, https://mssqlquery.com/wp-content/uploads/2023/06/converting-xml-to-table-in-sql-server-1-300x181.jpg 300w" sizes="(max-width: 700px) 100vw, 700px" /><p id="caption-attachment-1637" class="wp-caption-text"><span style="font-size: 16px;">As you can see, we have converted the XML into a table.</span></p></div>
<p>Good luck to everyone in business and life.</p>
<p>&nbsp;</p>
<div class='epvc-post-count'><span class='epvc-eye'></span>  <span class="epvc-count"> 30</span><span class='epvc-label'> Views</span></div><p>The post <a href="https://mssqlquery.com/converting-xml-to-table-in-sql-server">Converting XML to Table in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How to Clear Backup History and Job History in SQL Server?</title>
		<link>https://mssqlquery.com/how-to-clear-backup-history-and-job-history-in-sql-server</link>
		
		<dc:creator><![CDATA[Yavuz Selim Kart]]></dc:creator>
		<pubDate>Thu, 22 Jun 2023 06:44:45 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Clear Backup History]]></category>
		<category><![CDATA[Job History in SQL Server]]></category>
		<category><![CDATA[SQL Backup History]]></category>
		<guid isPermaLink="false">https://mssqlquery.com/?p=1632</guid>

					<description><![CDATA[<p>Hello everyone. In this article, I will try to give information about how to clear backup history and job history in SQL Server. In SQL Server, in some cases, you may want to clear the backup history and job history. SQL Server keeps track of every backup and restore it&#46;&#46;&#46;</p>
<p>The post <a href="https://mssqlquery.com/how-to-clear-backup-history-and-job-history-in-sql-server">How to Clear Backup History and Job History in SQL Server?</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Hello everyone. In this article, I will try to give information about how to clear backup history and job history in SQL Server.</p>
<p>In SQL Server, in some cases, you may want to clear the backup history and job history.</p>
<p>SQL Server keeps track of every backup and restore it performs. This information is recorded in the tables below.</p>
<pre class="line-numbers"><code class="language-sql">msdb.dbo.backupfile
msdb.dbo.backupfilegroup
msdb.dbo.backupmediafamily
msdb.dbo.backupmediaset
msdb.dbo.backupset
msdb.dbo.restorefile
msdb.dbo.restorefilegroup
msdb.dbo.restorehistory</code></pre>
<p>There is no automatic cleanup of backup and restore records in SQL Server, but cleaning these records can become important over time.</p>
<p>Before upgrading SQL Server, if any of these tables exceed <strong>10,000</strong> rows this may cause the upgrade to hang, so it is recommended to delete old records.</p>
<p>You can run the procedure called <strong>sp_delete_backuphistory</strong> to reduce the rows in all these tables. This procedure deletes all backup and restore history information for backup sets older than a specified date. An example is shown below.</p>
<pre class="line-numbers"><code class="language-sql">EXEC msdb..sp_delete_backuphistory '20231201 12:00:00.000'</code></pre>
<p>Similarly, you can revise the example below for yourself to remove records from msdb.dbo.sysjobhistory.</p>
<pre class="line-numbers"><code class="language-sql">EXEC msdb..sp_purge_jobhistory @oldest_date = '20231201 12:00:00.000'</code></pre>
<p>You can set up a Job to clear these tables, but you&#8217;ll have to put some thought into how often you need to clear them.</p>
<p>Good luck to everyone in business and life.</p>
<div class='epvc-post-count'><span class='epvc-eye'></span>  <span class="epvc-count"> 23</span><span class='epvc-label'> Views</span></div><p>The post <a href="https://mssqlquery.com/how-to-clear-backup-history-and-job-history-in-sql-server">How to Clear Backup History and Job History in SQL Server?</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Using CREATE or ALTER View in SQL Server</title>
		<link>https://mssqlquery.com/using-create-or-alter-view-in-sql-server</link>
		
		<dc:creator><![CDATA[Yavuz Selim Kart]]></dc:creator>
		<pubDate>Mon, 19 Jun 2023 19:21:49 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[CREATE or ALTER View]]></category>
		<category><![CDATA[SQL CREATE or ALTER View]]></category>
		<category><![CDATA[SQL Server CREATE or ALTER View]]></category>
		<guid isPermaLink="false">https://mssqlquery.com/?p=1629</guid>

					<description><![CDATA[<p>Hello everyone. In this article, I will try to give information about Using CREATE or ALTER View in SQL Server. There is a small but useful new feature for creating Views with SQL Server 2016 SP1. OR ALTER statement. If you add this statement to your code to create the&#46;&#46;&#46;</p>
<p>The post <a href="https://mssqlquery.com/using-create-or-alter-view-in-sql-server">Using CREATE or ALTER View in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Hello everyone. In this article, I will try to give information about Using CREATE or ALTER View in SQL Server.</p>
<p>There is a small but useful new feature for creating Views with SQL Server 2016 SP1. <strong>OR ALTER</strong> statement. If you add this statement to your code to create the View, SQL Server will know whether it needs to run a CREATE or ALTER statement.</p>
<p>Below you can see a Vew example created for the Adventureworks database.</p>
<pre class="line-numbers"><code class="language-sql">CREATE OR ALTER VIEW EmployeeReport
AS
SELECT 
	p.FirstName, 
	p.MiddleName, 
	p.LastName, 
	e.HireDate, 
	e.JobTitle
FROM Person.Person p
JOIN HumanResources.Employee e 
	ON p.BusinessEntityID = e.BusinessEntityID</code></pre>
<p>This feature is especially useful when gradually developing your Views. You can focus on the change in View and no need to switch between CREATE and ALTER.</p>
<p>Good luck to everyone in business and life.</p>
<div class='epvc-post-count'><span class='epvc-eye'></span>  <span class="epvc-count"> 16</span><span class='epvc-label'> Views</span></div><p>The post <a href="https://mssqlquery.com/using-create-or-alter-view-in-sql-server">Using CREATE or ALTER View in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Using the Geography Data Type in SQL Server</title>
		<link>https://mssqlquery.com/using-the-geography-data-type-in-sql-server</link>
		
		<dc:creator><![CDATA[Yavuz Selim Kart]]></dc:creator>
		<pubDate>Sun, 18 Jun 2023 15:00:59 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Data Type]]></category>
		<category><![CDATA[Geography Data Type]]></category>
		<category><![CDATA[Geography SQL Server]]></category>
		<guid isPermaLink="false">https://mssqlquery.com/?p=1619</guid>

					<description><![CDATA[<p>Hello everyone. In this article, I will try to give information about using the Geography data type in SQL Server. In SQL Server, in some cases, you may have to use the Geography data type. The data type that allows us to keep the coordinates in SQL Server is the&#46;&#46;&#46;</p>
<p>The post <a href="https://mssqlquery.com/using-the-geography-data-type-in-sql-server">Using the Geography Data Type in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Hello everyone. In this article, I will try to give information about using the Geography data type in SQL Server.</p>
<p>In SQL Server, in some cases, you may have to use the Geography data type.</p>
<p>The data type that allows us to keep the coordinates in SQL Server is the Geography data type.</p>
<p>Besides this service, SQL Server also offers methods that allow us to perform some operations on these coordinates. You can see an example usage below.</p>
<pre class="line-numbers"><code class="language-sql">DECLARE @geo_istanbul geography
SET @geo_istanbul = geography::Point(41.010467,28.939179 , 4326)
SELECT @geo_istanbul</code></pre>
<p>Point(Latitude, Longitude, SRID) gives us the coordinates of a point on the earth. The variables inside the point are latitude, longitude and SRID, respectively.</p>
<p>SRID is a reference, it tells us whether the system is planar or like the shape of the earth. The 4326 we use is the one used in normal GPS systems. We will always use it.</p>
<p>You may think that we can&#8217;t write the <strong>&#8220;Point(41.010467,28.939179 , 4326)&#8221;</strong> part, which you see directly above, on the table, but unfortunately you need to make a definition as above. The reason for this is that this system stores data of the Geography type as binary in the database.</p>
<p>In the example below, you can see how to add a Geography data type to a table.</p>
<pre class="line-numbers"><code class="language-sql">IF OBJECT_ID('tempdb..#GeoTest') IS NOT NULL
    DROP TABLE #GeoTest
CREATE TABLE #GeoTest ([CoordinateLocation] [geography] NULL);
INSERT INTO #GeoTest (CoordinateLocation)
SELECT GEOGRAPHY::STPointFromText('POINT(41.010467 28.939179)', 4326);
SELECT *
FROM #GeoTest;</code></pre>
<p>The method we can use the most is the STDistance() method. This method calculates the distance between two points on the earth. You can see an example on this subject below.</p>
<pre class="line-numbers"><code class="language-sql">DECLARE @istanbul geography, @izmir geography
SET @istanbul = GEOGRAPHY::STPointFromText('POINT(41.00527 28.97696)', 4326)
SET @izmir = GEOGRAPHY::STPointFromText('POINT(38.41885  27.12872)', 4326)
SELECT @istanbul.STDistance (@izmir)/1000</code></pre>
<p>Those who wish</p>
<p>They can check it from <a href="https://www.meridianoutpost.com/resources/etools/calculators/calculator-latitude-longitude-distance.php">https://www.meridianoutpost.com/resources/etools/calculators/calculator-latitude-longitude-distance.php</a></p>
<p>Good luck to everyone in business and life.</p>
<div class='epvc-post-count'><span class='epvc-eye'></span>  <span class="epvc-count"> 29</span><span class='epvc-label'> Views</span></div><p>The post <a href="https://mssqlquery.com/using-the-geography-data-type-in-sql-server">Using the Geography Data Type in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Calculating Time Between Two Rows in SQL Server</title>
		<link>https://mssqlquery.com/calculating-time-between-two-rows-in-sql-server</link>
		
		<dc:creator><![CDATA[Yavuz Selim Kart]]></dc:creator>
		<pubDate>Sun, 18 Jun 2023 14:46:56 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server Time]]></category>
		<category><![CDATA[SQL Server Time Between]]></category>
		<category><![CDATA[SQL Time Between]]></category>
		<guid isPermaLink="false">https://mssqlquery.com/?p=1615</guid>

					<description><![CDATA[<p>Hello everyone. In this article, I will try to give information about calculating the time between two rows in SQL Server. In SQL Server you may want to calculate the time between two rows in some cases. You can easily do this by revising the sample code below. --Tablonun oluşturulması&#46;&#46;&#46;</p>
<p>The post <a href="https://mssqlquery.com/calculating-time-between-two-rows-in-sql-server">Calculating Time Between Two Rows in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Hello everyone. In this article, I will try to give information about calculating the time between two rows in SQL Server.</p>
<p>In SQL Server you may want to calculate the time between two rows in some cases.</p>
<p>You can easily do this by revising the sample code below.</p>
<pre class="line-numbers"><code class="language-sql">--Tablonun oluşturulması

CREATE TABLE TestTable(
Id INT PRIMARY KEY IDENTITY(1,1),
IslemId INT,
Durum NVARCHAR(20),
Tarih DATETIME
)


--Tabloya veri eklenmesi

INSERT INTO TestTable (IslemId, Durum, Tarih)
	VALUES (100, N'Başladı', '2022-08-01 08:00:00.000'),
	(101, N'Başladı', '2022-08-02 07:30:00.000'),
	(100, N'Durdu', '2022-08-03 17:00:00.000'),
	(102, N'Başladı', '2022-08-04 08:00:00.000'),
	(102, N'Durdu', '2022-08-04 17:00:00.000'),
	(101, N'Durdu', '2022-08-05 18:00:00.000')

-- Tablonun kontrol edilmesi

SELECT * FROM TestTable

--İki satır arasındaki süreyi hesaplamak

SELECT
	IslemId, 
	CONVERT(DATE, testTablosu.Tarih) AS 'Başlama Zamanı',
	CONVERT(DATE, taskEnd.Tarih) AS 'Bitiş Zamanı',
	DATEDIFF(day, testTablosu.Tarih, taskEnd.Tarih) as 'Gün Farkı'
FROM TestTable testTablosu
OUTER APPLY (
	SELECT TOP(1)
		taskEnd.Tarih
	FROM TestTable taskEnd
	WHERE 
		taskEnd.Tarih &gt; testTablosu.Tarih
		AND taskEnd.Durum = 'Durdu'
		AND taskEnd.IslemId = testTablosu.IslemId
	ORDER BY Tarih
	) taskEnd
WHERE
	testTablosu.Durum = 'Başladı'</code></pre>
<p>When you create the table above and run the relevant codes, you will get a result as follows.</p>
<p><img decoding="async" class="size-full wp-image-1617" src="https://mssqlquery.com/wp-content/uploads/2023/06/calculating-time-between-two-rows-in-sql-server-1.jpg" alt="" width="700" height="416" srcset="https://mssqlquery.com/wp-content/uploads/2023/06/calculating-time-between-two-rows-in-sql-server-1.jpg 700w, https://mssqlquery.com/wp-content/uploads/2023/06/calculating-time-between-two-rows-in-sql-server-1-300x178.jpg 300w" sizes="(max-width: 700px) 100vw, 700px" /></p>
<p>As you can see, we have calculated the time between two lines.</p>
<p>Good luck to everyone in business and life.</p>
<div class='epvc-post-count'><span class='epvc-eye'></span>  <span class="epvc-count"> 25</span><span class='epvc-label'> Views</span></div><p>The post <a href="https://mssqlquery.com/calculating-time-between-two-rows-in-sql-server">Calculating Time Between Two Rows in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Listing Largest Tables and Indexes in Database in SQL Server</title>
		<link>https://mssqlquery.com/listing-largest-tables-and-indexes-in-database-in-sql-server</link>
		
		<dc:creator><![CDATA[Yavuz Selim Kart]]></dc:creator>
		<pubDate>Mon, 12 Jun 2023 21:51:13 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Listing Largest Index]]></category>
		<category><![CDATA[Listing Largest Tables]]></category>
		<category><![CDATA[Listing Largest Tables and Indexes in Database]]></category>
		<guid isPermaLink="false">https://mssqlquery.com/?p=1611</guid>

					<description><![CDATA[<p>Hello everyone. In this article, I will try to give information about listing the largest tables and indexes in the database in SQL Server. In SQL Server, in some cases you may want to list the largest tables and Indexes in the database. You can easily do this using the&#46;&#46;&#46;</p>
<p>The post <a href="https://mssqlquery.com/listing-largest-tables-and-indexes-in-database-in-sql-server">Listing Largest Tables and Indexes in Database in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Hello everyone. In this article, I will try to give information about listing the largest tables and indexes in the database in SQL Server.</p>
<p>In SQL Server, in some cases you may want to list the largest tables and Indexes in the database.</p>
<p>You can easily do this using the code below.</p>
<p><strong>Listing the Largest Tables</strong></p>
<pre class="line-numbers"><code class="language-sql">SELECT TOP 100
	s.[name] AS [schema]
   ,t.[name] AS [table_name]
   ,p.[rows] AS [row_count]
   ,CAST(ROUND(((SUM(a.total_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS [size_mb]
   ,CAST(ROUND(((SUM(a.used_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS [used_mb]
   ,CAST(ROUND(((SUM(a.total_pages) - SUM(a.used_pages)) * 8) / 1024.00, 2) AS NUMERIC(36, 2)) AS [unused_mb]
FROM sys.tables t
JOIN sys.indexes i
	ON t.[object_id] = i.[object_id]
JOIN sys.partitions p
	ON i.[object_id] = p.[object_id]
		AND i.index_id = p.index_id
JOIN sys.allocation_units a
	ON p.[partition_id] = a.container_id
LEFT JOIN sys.schemas s
	ON t.[schema_id] = s.[schema_id]
WHERE t.is_ms_shipped = 0
AND i.[object_id] &gt; 255
GROUP BY t.[name]
		,s.[name]
		,p.[rows]
ORDER BY [size_mb] DESC</code></pre>
<p><strong>Listing the Biggest Indexes</strong></p>
<pre class="line-numbers"><code class="language-sql">SELECT TOP(100)
    s.[name] AS [schema],
    t.[name] AS [table_name],
    i.[name] AS [index_name],
    i.[type_desc],
    p.[rows] AS [row_count],
    CAST(ROUND(((SUM(a.total_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS [size_mb],
    CAST(ROUND(((SUM(a.used_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS [used_mb], 
    CAST(ROUND(((SUM(a.total_pages) - SUM(a.used_pages)) * 8) / 1024.00, 2) AS NUMERIC(36, 2)) AS [unused_mb]
FROM
    sys.tables t
    JOIN sys.indexes i ON t.[object_id] = i.[object_id]
    JOIN sys.partitions p ON i.[object_id] = p.[object_id] AND i.index_id = p.index_id
    JOIN sys.allocation_units a ON p.[partition_id] = a.container_id
    LEFT JOIN sys.schemas s ON t.[schema_id] = s.[schema_id]
WHERE 
    t.is_ms_shipped = 0
    AND i.[object_id] &gt; 255 
GROUP BY
    t.[name], 
    s.[name],
    i.[name],
    i.[type_desc],
    p.[rows]
ORDER BY 
    [size_mb] DESC</code></pre>
<p>Good luck to everyone in business and life.</p>
<div class='epvc-post-count'><span class='epvc-eye'></span>  <span class="epvc-count"> 33</span><span class='epvc-label'> Views</span></div><p>The post <a href="https://mssqlquery.com/listing-largest-tables-and-indexes-in-database-in-sql-server">Listing Largest Tables and Indexes in Database in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Function to Print Words Side by Side in SQL Server</title>
		<link>https://mssqlquery.com/function-to-print-words-side-by-side-in-sql-server</link>
		
		<dc:creator><![CDATA[Yavuz Selim Kart]]></dc:creator>
		<pubDate>Wed, 07 Jun 2023 22:06:27 +0000</pubDate>
				<category><![CDATA[Functions]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Function to Print Words Side by Side]]></category>
		<category><![CDATA[print sql server]]></category>
		<category><![CDATA[sql print word]]></category>
		<guid isPermaLink="false">https://mssqlquery.com/?p=1606</guid>

					<description><![CDATA[<p>Hello everyone. In this article, I will try to give information about the function that prints the initials of words side by side in SQL Server. In SQL Server, in some cases, you may want to print the initials of the words of a given sentence side by side. You&#46;&#46;&#46;</p>
<p>The post <a href="https://mssqlquery.com/function-to-print-words-side-by-side-in-sql-server">Function to Print Words Side by Side in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Hello everyone. In this article, I will try to give information about the function that prints the initials of words side by side in SQL Server.</p>
<p>In SQL Server, in some cases, you may want to print the initials of the words of a given sentence side by side.</p>
<p>You can easily do this using the function below.</p>
<pre class="line-numbers"><code class="language-sql">CREATE FUNCTION [dbo].[fn_Cumle_Bas_Harfi_Alma] ( @cumle NVARCHAR(4000) )
RETURNS NVARCHAR(2000)
AS
BEGIN
    DECLARE @donensonuc NVARCHAR(2000);

    SET @cumle=RTRIM(LTRIM(@cumle));
    SET @donensonuc=LEFT(@cumle,1);

    WHILE CHARINDEX(' ',@cumle,1)&gt;0 BEGIN
        SET @cumle=LTRIM(RIGHT(@cumle,LEN(@cumle)-CHARINDEX(' ',@cumle,1)));
        SET @donensonuc+=LEFT(@cumle,1);
    END

    RETURN @donensonuc;
END
GO


--Using the Function

SELECT dbo.fn_Cumle_Bas_Harfi_Alma('Yavuz Selim Kart') AS SadeceBasHarfler</code></pre>
<p>When you create and run the above function, you will see a result similar to the one below.</p>
<p><img decoding="async" loading="lazy" class="alignnone wp-image-1608 size-full" src="https://mssqlquery.com/wp-content/uploads/2023/06/function-to-print-words-side-by-side-in-sql-server-1.jpg" alt="Function to Print Words Side by Side in SQL Server" width="700" height="375" srcset="https://mssqlquery.com/wp-content/uploads/2023/06/function-to-print-words-side-by-side-in-sql-server-1.jpg 700w, https://mssqlquery.com/wp-content/uploads/2023/06/function-to-print-words-side-by-side-in-sql-server-1-300x161.jpg 300w" sizes="(max-width: 700px) 100vw, 700px" /></p>
<p>As you can see, we have written the initials of the word.</p>
<p>Good luck to everyone in business and life.</p>
<div class='epvc-post-count'><span class='epvc-eye'></span>  <span class="epvc-count"> 26</span><span class='epvc-label'> Views</span></div><p>The post <a href="https://mssqlquery.com/function-to-print-words-side-by-side-in-sql-server">Function to Print Words Side by Side in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Getting Second Word in Given Sentence in SQL Server</title>
		<link>https://mssqlquery.com/getting-second-word-in-given-sentence-in-sql-server</link>
		
		<dc:creator><![CDATA[Yavuz Selim Kart]]></dc:creator>
		<pubDate>Wed, 07 Jun 2023 21:59:50 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Getting Second Word SQL Server]]></category>
		<category><![CDATA[SQL Server Get Second Word]]></category>
		<category><![CDATA[TSQL Second Word]]></category>
		<guid isPermaLink="false">https://mssqlquery.com/?p=1601</guid>

					<description><![CDATA[<p>Hello everyone. In this article, I will try to give information about getting the second word in the sentence given in SQL Server. In SQL Server you may want to get the second word in the given sentence in some cases. You can easily do this using the code below.&#46;&#46;&#46;</p>
<p>The post <a href="https://mssqlquery.com/getting-second-word-in-given-sentence-in-sql-server">Getting Second Word in Given Sentence in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Hello everyone. In this article, I will try to give information about getting the second word in the sentence given in SQL Server.</p>
<p>In SQL Server you may want to get the second word in the given sentence in some cases.</p>
<p>You can easily do this using the code below.</p>
<pre class="line-numbers"><code class="language-sql">DECLARE @Cumle NVARCHAR(264);

SET @Cumle = N'Yavuz Selim Kart';

SELECT LTRIM(SUBSTRING(
                          @Cumle,
                          CHARINDEX(' ', @Cumle),
                          CHARINDEX(
                                       ' ',
                                       LTRIM(SUBSTRING(
                                                          @Cumle,
                                                          CHARINDEX(' ', @Cumle),
                                                          LEN(@Cumle) - CHARINDEX(' ', @Cumle)
                                                      )
                                            )
                                   )
                      )
            );</code></pre>
<p>When you run the above code, you will see a result similar to the one below.</p>
<p><img decoding="async" loading="lazy" class="size-full wp-image-1604" src="https://mssqlquery.com/wp-content/uploads/2023/06/getting-second-word-in-given-sentence-in-sql-server-1.jpg" alt="" width="700" height="405" srcset="https://mssqlquery.com/wp-content/uploads/2023/06/getting-second-word-in-given-sentence-in-sql-server-1.jpg 700w, https://mssqlquery.com/wp-content/uploads/2023/06/getting-second-word-in-given-sentence-in-sql-server-1-300x174.jpg 300w" sizes="(max-width: 700px) 100vw, 700px" /></p>
<p>As you can see, we have taken the second word in the given sentence.</p>
<p>Good luck to everyone in business and life.</p>
<div class='epvc-post-count'><span class='epvc-eye'></span>  <span class="epvc-count"> 16</span><span class='epvc-label'> Views</span></div><p>The post <a href="https://mssqlquery.com/getting-second-word-in-given-sentence-in-sql-server">Getting Second Word in Given Sentence in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Converting VARCHAR Type Date to dd.mm.yyyy in SQL Server</title>
		<link>https://mssqlquery.com/converting-varchar-type-date-to-dd-mm-yyyy-in-sql-server</link>
		
		<dc:creator><![CDATA[Yavuz Selim Kart]]></dc:creator>
		<pubDate>Wed, 07 Jun 2023 21:54:51 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Convert Char to Datetime]]></category>
		<category><![CDATA[Convert Datetime]]></category>
		<category><![CDATA[Converting VARCHAR Type Date]]></category>
		<guid isPermaLink="false">https://mssqlquery.com/?p=1596</guid>

					<description><![CDATA[<p>Hello everyone. In this article, I will try to give information about converting VARCHAR type date as dd.mm.yyyy in SQL Server. In SQL Server, in some cases, you may want to convert the VARCHAR type date to dd.mm.yyyy. You can easily do this using the code below. DECLARE @tarih VARCHAR(30)&#46;&#46;&#46;</p>
<p>The post <a href="https://mssqlquery.com/converting-varchar-type-date-to-dd-mm-yyyy-in-sql-server">Converting VARCHAR Type Date to dd.mm.yyyy in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Hello everyone. In this article, I will try to give information about converting VARCHAR type date as dd.mm.yyyy in SQL Server.</p>
<p>In SQL Server, in some cases, you may want to convert the VARCHAR type date to dd.mm.yyyy.</p>
<p>You can easily do this using the code below.</p>
<pre class="line-numbers"><code class="language-sql">DECLARE @tarih VARCHAR(30) = '5.06.2023'
SELECT
	CONVERT(VARCHAR, CONVERT(DATE, @tarih, 104), 104)</code></pre>
<p>When you run the above code, you will see a result similar to the one below.</p>
<div id="attachment_1599" style="width: 710px" class="wp-caption alignnone"><img aria-describedby="caption-attachment-1599" decoding="async" loading="lazy" class="wp-image-1599 size-full" src="https://mssqlquery.com/wp-content/uploads/2023/06/converting-varchar-type-date-to-dd-mm-yyyy-in-sql-server-1.jpg" alt="Converting VARCHAR Type Date to dd.mm.yyyy in SQL Server" width="700" height="445" srcset="https://mssqlquery.com/wp-content/uploads/2023/06/converting-varchar-type-date-to-dd-mm-yyyy-in-sql-server-1.jpg 700w, https://mssqlquery.com/wp-content/uploads/2023/06/converting-varchar-type-date-to-dd-mm-yyyy-in-sql-server-1-300x191.jpg 300w" sizes="(max-width: 700px) 100vw, 700px" /><p id="caption-attachment-1599" class="wp-caption-text"> </p></div>
<p>As you can see, we have converted the VARCHAR type date as dd.mm.yyyy.</p>
<p>Good luck to everyone in business and life.</p>
<p>&nbsp;</p>
<div class='epvc-post-count'><span class='epvc-eye'></span>  <span class="epvc-count"> 14</span><span class='epvc-label'> Views</span></div><p>The post <a href="https://mssqlquery.com/converting-varchar-type-date-to-dd-mm-yyyy-in-sql-server">Converting VARCHAR Type Date to dd.mm.yyyy in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Procedure to Create Property from Tables and Views in SQL Server</title>
		<link>https://mssqlquery.com/procedure-to-create-property-from-tables-and-views-in-sql-server</link>
		
		<dc:creator><![CDATA[Yavuz Selim Kart]]></dc:creator>
		<pubDate>Thu, 01 Jun 2023 18:39:23 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Create Property from Tables]]></category>
		<category><![CDATA[csharp class generate sql]]></category>
		<category><![CDATA[sql server property generate]]></category>
		<guid isPermaLink="false">https://mssqlquery.com/?p=1593</guid>

					<description><![CDATA[<p>Hello everyone. In this article, I will try to give information about the procedure that creates Property from Tables and Views in SQL Server. If you&#8217;re programming on the CSharp side, you need Properties. It can take a lot of time to create a table or view and then create&#46;&#46;&#46;</p>
<p>The post <a href="https://mssqlquery.com/procedure-to-create-property-from-tables-and-views-in-sql-server">Procedure to Create Property from Tables and Views in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Hello everyone. In this article, I will try to give information about the procedure that creates Property from Tables and Views in SQL Server.</p>
<p>If you&#8217;re programming on the CSharp side, you need Properties.</p>
<p>It can take a lot of time to create a table or view and then create a Property in SQL Server.</p>
<p>Using the procedure below, you can easily create the class structure of your table.</p>
<pre class="line-numbers"><code class="language-markup">CREATE  PROCEDURE [dbo].[usp_GenerateClassFromViewAndTable]
  @TabloViewAdi NVARCHAR(100),
  @ClassAdi NVARCHAR(100)
AS
BEGIN
declare @Sonuc varchar(max) = 'public class ' + @ClassAdi + '
{'

select @Sonuc = @Sonuc + '
    public ' + ColumnType + NullableSign + ' ' + ColumnName + ' { get; set; }
'
from
(
    select 
        replace(col.name, ' ', '_') ColumnName,
        column_id ColumnId,
        case typ.name 
            when 'bigint' then 'long'
            when 'binary' then 'byte[]'
            when 'bit' then 'bool'
            when 'char' then 'string'
            when 'date' then 'DateTime'
            when 'datetime' then 'DateTime'
            when 'datetime2' then 'DateTime'
            when 'datetimeoffset' then 'DateTimeOffset'
            when 'decimal' then 'decimal'
            when 'float' then 'double'
            when 'image' then 'byte[]'
            when 'int' then 'int'
            when 'money' then 'decimal'
            when 'nchar' then 'string'
            when 'ntext' then 'string'
            when 'numeric' then 'decimal'
            when 'nvarchar' then 'string'
            when 'real' then 'float'
            when 'smalldatetime' then 'DateTime'
            when 'smallint' then 'short'
            when 'smallmoney' then 'decimal'
            when 'text' then 'string'
            when 'time' then 'TimeSpan'
            when 'timestamp' then 'long'
            when 'tinyint' then 'byte'
            when 'uniqueidentifier' then 'Guid'
            when 'varbinary' then 'byte[]'
            when 'varchar' then 'string'
            else 'UNKNOWN_' + typ.name
        end ColumnType,
        case 
            when col.is_nullable = 1 and typ.name in ('bigint', 'bit', 'date', 'datetime', 'datetime2', 'datetimeoffset', 'decimal', 'float', 'int', 'money', 'numeric', 'real', 'smalldatetime', 'smallint', 'smallmoney', 'time', 'tinyint', 'uniqueidentifier') 
            then '?' 
            else '' 
        end NullableSign
    from sys.columns col
        join sys.types typ on
            col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id
    where object_id = object_id(@TabloViewAdi)
) t
order by ColumnId

set @Sonuc = @Sonuc  + '
}'

PRINT  @Sonuc

END
GO

--Use of the procedure

EXEC usp_GenerateClassFromViewAndTable 
@TabloViewAdi = N'TableViewName' 
,@ClassAdi = N'ClassNameYouWanttoCreate'</code></pre>
<p>Good luck to everyone in business and life.</p>
<div class='epvc-post-count'><span class='epvc-eye'></span>  <span class="epvc-count"> 26</span><span class='epvc-label'> Views</span></div><p>The post <a href="https://mssqlquery.com/procedure-to-create-property-from-tables-and-views-in-sql-server">Procedure to Create Property from Tables and Views in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Viewing the Size of Log Files in SQL Server</title>
		<link>https://mssqlquery.com/viewing-the-size-of-log-files-in-sql-server</link>
		
		<dc:creator><![CDATA[Yavuz Selim Kart]]></dc:creator>
		<pubDate>Sun, 28 May 2023 10:24:29 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Log Files in SQL Server]]></category>
		<category><![CDATA[SQL Server Log Files]]></category>
		<category><![CDATA[TSQL Log Files]]></category>
		<guid isPermaLink="false">https://mssqlquery.com/?p=1587</guid>

					<description><![CDATA[<p>Hello everyone, In this article, I will provide information about viewing the size of log files in SQL Server. In SQL Server we may want to view the size of log files in some cases. You can easily do this with the help of the code below. SELECT DB_NAME(mf.database_id) AS&#46;&#46;&#46;</p>
<p>The post <a href="https://mssqlquery.com/viewing-the-size-of-log-files-in-sql-server">Viewing the Size of Log Files in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Hello everyone,</p>
<p>In this article, I will provide information about viewing the size of log files in SQL Server.</p>
<p>In SQL Server we may want to view the size of log files in some cases.</p>
<p>You can easily do this with the help of the code below.</p>
<pre class="line-numbers"><code class="language-sql">SELECT DB_NAME(mf.database_id) AS 'DB Name',
       name AS 'File Logical Name',
       'File Type' = CASE
                         WHEN type_desc = 'LOG' THEN
                             'Log File'
                         WHEN type_desc = 'ROWS' THEN
                             'Data File'
                         ELSE
                             type_desc
                     END,
       mf.physical_name AS 'File Physical Name',
       size_on_disk_bytes / 1024 AS 'Size(KB)',
       size_on_disk_bytes / 1024 / 1024 AS 'Size(MB)',
       size_on_disk_bytes / 1024 / 1024 / 1024 AS 'Size(GB)'
FROM sys.dm_io_virtual_file_stats(NULL, NULL) AS divfs
    JOIN sys.master_files AS mf
        ON mf.database_id = divfs.database_id
           AND mf.file_id = divfs.file_id
WHERE CASE
          WHEN type_desc = 'LOG' THEN
              'Log File'
          WHEN type_desc = 'ROWS' THEN
              'Data File'
          ELSE
              type_desc
      END = 'Log File'
ORDER BY DB_NAME(mf.database_id);</code></pre>
<p>When you run the code, you will get a result like the one below.</p>
<p><img decoding="async" loading="lazy" class="size-full wp-image-1589" src="https://mssqlquery.com/wp-content/uploads/2023/05/viewing-the-size-of-log-files-in-sql-server-1.jpg" alt="" width="700" height="481" srcset="https://mssqlquery.com/wp-content/uploads/2023/05/viewing-the-size-of-log-files-in-sql-server-1.jpg 700w, https://mssqlquery.com/wp-content/uploads/2023/05/viewing-the-size-of-log-files-in-sql-server-1-300x206.jpg 300w" sizes="(max-width: 700px) 100vw, 700px" /></p>
<p>As you can see, the size of the log files is displayed.</p>
<p>Good luck to everyone in business and life.</p>
<div class='epvc-post-count'><span class='epvc-eye'></span>  <span class="epvc-count"> 466</span><span class='epvc-label'> Views</span></div><p>The post <a href="https://mssqlquery.com/viewing-the-size-of-log-files-in-sql-server">Viewing the Size of Log Files in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Listing Tables by Creation Date in SQL Server</title>
		<link>https://mssqlquery.com/listing-tables-by-creation-date-in-sql-server</link>
		
		<dc:creator><![CDATA[Yavuz Selim Kart]]></dc:creator>
		<pubDate>Thu, 25 May 2023 19:54:35 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Listing Tables by Creation Date]]></category>
		<category><![CDATA[Listing Tables SQL]]></category>
		<category><![CDATA[SQL Server Listing Tables]]></category>
		<guid isPermaLink="false">https://mssqlquery.com/?p=1583</guid>

					<description><![CDATA[<p>Hello everyone. In this article, I will try to give information about listing tables by creation date in SQL Server. In SQL Server, in some cases you may want to list tables by creation date. You can easily do this using the code below. SELECT name ,create_date FROM sys.tables ORDER&#46;&#46;&#46;</p>
<p>The post <a href="https://mssqlquery.com/listing-tables-by-creation-date-in-sql-server">Listing Tables by Creation Date in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Hello everyone. In this article, I will try to give information about listing tables by creation date in SQL Server.</p>
<p>In SQL Server, in some cases you may want to list tables by creation date.</p>
<p>You can easily do this using the code below.</p>
<pre class="line-numbers"><code class="language-sql">SELECT
	name
   ,create_date
FROM sys.tables
ORDER BY create_date DESC</code></pre>
<p>Good luck to everyone in business and life.</p>
<div class='epvc-post-count'><span class='epvc-eye'></span>  <span class="epvc-count"> 21</span><span class='epvc-label'> Views</span></div><p>The post <a href="https://mssqlquery.com/listing-tables-by-creation-date-in-sql-server">Listing Tables by Creation Date in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Customized PRINT Procedure in SQL Server</title>
		<link>https://mssqlquery.com/customized-print-procedure-in-sql-server</link>
		
		<dc:creator><![CDATA[Yavuz Selim Kart]]></dc:creator>
		<pubDate>Thu, 25 May 2023 09:57:10 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Stored Procedures]]></category>
		<category><![CDATA[PRINT Procedure]]></category>
		<category><![CDATA[SQL PRINT 4000]]></category>
		<category><![CDATA[SQL Server PRINT]]></category>
		<guid isPermaLink="false">https://mssqlquery.com/?p=1578</guid>

					<description><![CDATA[<p>Hello everyone. In this article, I will try to give information about the customized PRINT procedure in SQL Server. In SQL Server, the PRINT statement prints up to 4000 characters. So what will you do if the content of the dynamic queries you write is longer than 4000 characters? In&#46;&#46;&#46;</p>
<p>The post <a href="https://mssqlquery.com/customized-print-procedure-in-sql-server">Customized PRINT Procedure in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Hello everyone. In this article, I will try to give information about the customized PRINT procedure in SQL Server.</p>
<p>In SQL Server, the PRINT statement prints up to 4000 characters. So what will you do if the content of the dynamic queries you write is longer than 4000 characters?</p>
<p>In SQL Server, in some cases, you may want to PRINT statements with more than 4000 characters.</p>
<p>You can easily do this using the procedure below.</p>
<pre class="line-numbers"><code class="language-sql">CREATE PROCEDURE LONGPRINT
    @String NVARCHAR(MAX)
AS
BEGIN
    BEGIN TRY

    DECLARE @CurrentEnd BIGINT;
    DECLARE @Offset TINYINT; 
    SET @String = replace(replace(@String, CHAR(13) + CHAR(10), CHAR(10)), CHAR(13), CHAR(10))

    WHILE LEN(@String) &gt; 1
    BEGIN
        IF CHARINDEX(CHAR(10), @String) BETWEEN 1 AND 4000
        BEGIN
            SET @CurrentEnd =  CHARINDEX(CHAR(10), @String) -1
            SET @Offset = 2
        END
        ELSE
        BEGIN
            SET @CurrentEnd = 4000
            SET @Offset = 1
        END   
        PRINT SUBSTRING(@String, 1, @CurrentEnd) 
        SET @String = SUBSTRING(@String, @CurrentEnd + @Offset, LEN(@String))   
    END 
    END TRY
    BEGIN CATCH
        DECLARE @ErrorMessage VARCHAR(4000)
        SELECT @ErrorMessage = ERROR_MESSAGE()    
        RAISERROR(@ErrorMessage,16,1)
    END CATCH
END

--Use of the Procedure

DECLARE @Degisken VARCHAR(MAX)
SET @Degisken = REPLICATE('x', 6000)
EXEC LONGPRINT @Degisken</code></pre>
<p>When you create the above procedure and run the code, you will see a result similar to the one below.</p>
<p><img decoding="async" loading="lazy" class="alignnone wp-image-1581 size-full" src="https://mssqlquery.com/wp-content/uploads/2023/05/customized-print-procedure-in-sql-server-1.jpg" alt="Customized PRINT Procedure in SQL Server" width="700" height="357" srcset="https://mssqlquery.com/wp-content/uploads/2023/05/customized-print-procedure-in-sql-server-1.jpg 700w, https://mssqlquery.com/wp-content/uploads/2023/05/customized-print-procedure-in-sql-server-1-300x153.jpg 300w" sizes="(max-width: 700px) 100vw, 700px" /></p>
<p>As you can see, we have printed 6000 characters with the LONGPRINT procedure.</p>
<p>Good luck to everyone in business and life.</p>
<div class='epvc-post-count'><span class='epvc-eye'></span>  <span class="epvc-count"> 29</span><span class='epvc-label'> Views</span></div><p>The post <a href="https://mssqlquery.com/customized-print-procedure-in-sql-server">Customized PRINT Procedure in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Why You Shouldn&#8217;t Prefix &#8220;sp_&#8221; Before Your Stored Procedures in SQL Server?</title>
		<link>https://mssqlquery.com/why-you-shouldnt-prefix-sp_-before-your-stored-procedures-in-sql-server</link>
		
		<dc:creator><![CDATA[Yavuz Selim Kart]]></dc:creator>
		<pubDate>Mon, 22 May 2023 19:51:23 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Stored Procedures]]></category>
		<category><![CDATA[sp_ prefix]]></category>
		<category><![CDATA[SQL Server sp_ prefix]]></category>
		<category><![CDATA[Stored Procedures in SQL Server]]></category>
		<guid isPermaLink="false">https://mssqlquery.com/?p=1575</guid>

					<description><![CDATA[<p>Hello everyone. In this article, I will try to give information about why you should not add &#8220;sp_&#8221; prefix in front of your Stored Procedures in SQL Server. Most people tend to prefix &#8220;sp_&#8221; to the name of the Stored Procedure when creating a Stored Procedure in SQL Server. There&#46;&#46;&#46;</p>
<p>The post <a href="https://mssqlquery.com/why-you-shouldnt-prefix-sp_-before-your-stored-procedures-in-sql-server">Why You Shouldn’t Prefix “sp_” Before Your Stored Procedures in SQL Server?</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Hello everyone. In this article, I will try to give information about why you should not add &#8220;sp_&#8221; prefix in front of your Stored Procedures in SQL Server.</p>
<p>Most people tend to prefix &#8220;sp_&#8221; to the name of the Stored Procedure when creating a Stored Procedure in SQL Server.</p>
<p>There are several reasons why this is considered bad practice.</p>
<p><strong>Microsoft says you shouldn&#8217;t do this.</strong></p>
<p>You can read Microsoft&#8217;s statement below.</p>
<p>Avoid using the sp_ prefix when naming Stored Procedures. This prefix is used by Microsoft SQL Server to identify system procedures. Using the prefix can break the application code if there is a system procedure with the same name.</p>
<p>This means that if you accidentally name your Stored Procedure with the same name as one of SQL Server&#8217;s Stored Procedure, your Stored Procedure will not be called. Instead, the SQL Server System Stored Procedure is called.</p>
<p>For example, if you have created a Stored Procedure named &#8220;sp_configure&#8221; in your database, it will never be executed when you call it because there is a system Stored Procedure named &#8220;sp_configure&#8221; in the master database and will be called instead.</p>
<p>Second, when SQL Server sees the prefix &#8220;sp_&#8221; at the beginning of a Stored Procedure, it first tries to find that procedure in the master database. As stated in the Microsoft documentation above, &#8221; This prefix is used by SQL Server to denote system procedures &#8220;, so when SQL Server sees the phrase &#8220;sp_&#8221; prefix, it starts searching for system procedures. But after searching all the procedures in the master database and determining that your procedure is not there, it will return to your database to try to find the stored procedure.</p>
<p>As noted in the above post, procedures named with the “sp_” prefix will run slower. Of course, this may not always be noticed.</p>
<p>There are times when the above situation is an exception.</p>
<pre class="line-numbers"><code class="language-markup">Use DatabaseOrnek 
EXEC dbo.sp_configure</code></pre>
<p>ile</p>
<pre class="line-numbers"><code class="language-markup">Use DatabaseOrnek 
EXEC DatabaseOrnek.dbo.sp_configure</code></pre>
<p>The second example will call your procedure correctly.</p>
<p>But for this to work, you have to call it like (databasename.semaname.procedure) every time.</p>
<p>Unless you are intentionally creating a Stored Procedure in the SQL Server system database, there is no valid reason to name the Stored Procedure with the &#8220;sp_&#8221; prefix. It is not very convenient to send SQL Server to wrong database to find Stored Procedure. It&#8217;s confusing, inefficient, and can cause performance issues. So why take the risk? To help maintain peak performance of your databases, avoid using the &#8220;sp_&#8221; prefix in your Stored Procedure names.</p>
<p>Good luck to everyone in business and life.</p>
<div class='epvc-post-count'><span class='epvc-eye'></span>  <span class="epvc-count"> 32</span><span class='epvc-label'> Views</span></div><p>The post <a href="https://mssqlquery.com/why-you-shouldnt-prefix-sp_-before-your-stored-procedures-in-sql-server">Why You Shouldn’t Prefix “sp_” Before Your Stored Procedures in SQL Server?</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Listing All Collations Supported in SQL Server</title>
		<link>https://mssqlquery.com/listing-all-collations-supported-in-sql-server</link>
		
		<dc:creator><![CDATA[Yavuz Selim Kart]]></dc:creator>
		<pubDate>Tue, 16 May 2023 05:22:58 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Collations]]></category>
		<category><![CDATA[sql server Collations]]></category>
		<category><![CDATA[tsql Collations]]></category>
		<guid isPermaLink="false">https://mssqlquery.com/?p=1571</guid>

					<description><![CDATA[<p>Hello everyone. In this article, I will try to give information about listing all collations supported in SQL Server. In SQL Server you may want to list all supported collations in some cases. You can easily do this using the query below. SELECT name, COLLATIONPROPERTY(name, 'CodePage') AS Code_Page, description FROM&#46;&#46;&#46;</p>
<p>The post <a href="https://mssqlquery.com/listing-all-collations-supported-in-sql-server">Listing All Collations Supported in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Hello everyone. In this article, I will try to give information about listing all collations supported in SQL Server.</p>
<p>In SQL Server you may want to list all supported collations in some cases.</p>
<p>You can easily do this using the query below.</p>
<pre class="line-numbers"><code class="language-sql">SELECT name,
       COLLATIONPROPERTY(name, 'CodePage') AS Code_Page,
       description
FROM sys.fn_HelpCollations();</code></pre>
<p>Good luck to everyone in business and life.</p>
<div class='epvc-post-count'><span class='epvc-eye'></span>  <span class="epvc-count"> 30</span><span class='epvc-label'> Views</span></div><p>The post <a href="https://mssqlquery.com/listing-all-collations-supported-in-sql-server">Listing All Collations Supported in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Changing Collation of All Columns in SQL Server</title>
		<link>https://mssqlquery.com/changing-collation-of-all-columns-in-sql-server</link>
		
		<dc:creator><![CDATA[Yavuz Selim Kart]]></dc:creator>
		<pubDate>Mon, 15 May 2023 17:26:59 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Changing Collation]]></category>
		<category><![CDATA[Changing Collation SQL]]></category>
		<category><![CDATA[Changing Collation SQL Server]]></category>
		<guid isPermaLink="false">https://mssqlquery.com/?p=1568</guid>

					<description><![CDATA[<p>Hello everyone. In this article, I will try to give information about changing the collation of all columns in SQL Server. In SQL Server, in some cases, when you change the Collation of the database, the Collation may not change in the columns and you may want to change the&#46;&#46;&#46;</p>
<p>The post <a href="https://mssqlquery.com/changing-collation-of-all-columns-in-sql-server">Changing Collation of All Columns in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Hello everyone. In this article, I will try to give information about changing the collation of all columns in SQL Server.</p>
<p>In SQL Server, in some cases, when you change the Collation of the database, the Collation may not change in the columns and you may want to change the Collation of all columns.</p>
<p>You can easily do this using the query below.</p>
<pre class="line-numbers"><code class="language-sql">DECLARE @collate NVARCHAR(100);
DECLARE @table NVARCHAR(255);
DECLARE @column_name NVARCHAR(255);
DECLARE @column_id INT;
DECLARE @data_type NVARCHAR(255);
DECLARE @max_length INT;
DECLARE @row_id INT;
DECLARE @sql NVARCHAR(MAX);
DECLARE @sql_column NVARCHAR(MAX);

SET @collate = N'Turkish_CI_AI';

DECLARE local_table_cursor CURSOR FOR
SELECT [name]
FROM sysobjects
WHERE OBJECTPROPERTY(id, N'IsUserTable') = 1;

OPEN local_table_cursor;
FETCH NEXT FROM local_table_cursor
INTO @table;

WHILE @@FETCH_STATUS = 0
BEGIN

    DECLARE local_change_cursor CURSOR FOR
    SELECT ROW_NUMBER() OVER (ORDER BY c.column_id) AS row_id,
           c.name column_name,
           t.Name data_type,
           c.max_length,
           c.column_id
    FROM sys.columns c
        JOIN sys.types t
            ON c.system_type_id = t.system_type_id
        LEFT OUTER JOIN sys.index_columns ic
            ON ic.object_id = c.object_id
               AND ic.column_id = c.column_id
        LEFT OUTER JOIN sys.indexes i
            ON ic.object_id = i.object_id
               AND ic.index_id = i.index_id
    WHERE c.object_id = OBJECT_ID(@table)
    ORDER BY c.column_id;

    OPEN local_change_cursor;
    FETCH NEXT FROM local_change_cursor
    INTO @row_id,
         @column_name,
         @data_type,
         @max_length,
         @column_id;

    WHILE @@FETCH_STATUS = 0
    BEGIN

        IF (@max_length = -1)
            SET @max_length = 4000;

        IF (@data_type LIKE '%char%')
        BEGIN TRY
            SET @sql
                = N'ALTER TABLE ' + @table + N' ALTER COLUMN ' + @column_name + N' ' + @data_type + N'('
                  + CAST(@max_length AS NVARCHAR(100)) + N') COLLATE ' + @collate;
            PRINT @sql;
            EXEC sp_executesql @sql;
        END TRY
        BEGIN CATCH
            PRINT 'ERROR: Some index or contraint rely on the column' + @column_name + '. No conversion possible.';
            PRINT @sql;
        END CATCH;

        FETCH NEXT FROM local_change_cursor
        INTO @row_id,
             @column_name,
             @data_type,
             @max_length,
             @column_id;

    END;

    CLOSE local_change_cursor;
    DEALLOCATE local_change_cursor;

    FETCH NEXT FROM local_table_cursor
    INTO @table;

END;

CLOSE local_table_cursor;
DEALLOCATE local_table_cursor;

GO</code></pre>
<p>Good luck to everyone in business and life.</p>
<div class='epvc-post-count'><span class='epvc-eye'></span>  <span class="epvc-count"> 27</span><span class='epvc-label'> Views</span></div><p>The post <a href="https://mssqlquery.com/changing-collation-of-all-columns-in-sql-server">Changing Collation of All Columns in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>All Uses of DROP IF EXISTS in SQL Server</title>
		<link>https://mssqlquery.com/all-uses-of-drop-if-exists-in-sql-server</link>
		
		<dc:creator><![CDATA[Yavuz Selim Kart]]></dc:creator>
		<pubDate>Mon, 15 May 2023 13:16:33 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[DROP IF EXISTS]]></category>
		<category><![CDATA[DROP IF EXISTS in SQL Server]]></category>
		<category><![CDATA[IF EXISTS in SQL Server]]></category>
		<guid isPermaLink="false">https://mssqlquery.com/?p=1562</guid>

					<description><![CDATA[<p>Hello everyone. In this article, I will give information about all DROP IF EXISTS usages in SQL Server. In SQL Server you may need to use DROP IF EXISTS in some cases. You can easily do this using the code below. Note: The commands below work on SQL Server 2016&#46;&#46;&#46;</p>
<p>The post <a href="https://mssqlquery.com/all-uses-of-drop-if-exists-in-sql-server">All Uses of DROP IF EXISTS in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Hello everyone. In this article, I will give information about all DROP IF EXISTS usages in SQL Server.</p>
<p>In SQL Server you may need to use DROP IF EXISTS in some cases.</p>
<p>You can easily do this using the code below.</p>
<p><span class="HwtZe" lang="en"><span class="jCAhz ChMk0b"><span class="ryNqvb"><span style="color: #ff0000;"><strong>Note:</strong></span> The commands below work on SQL Server 2016 and above.</span></span></span></p>
<p><strong>1 -) <span class="HwtZe" lang="en"><span class="jCAhz ChMk0b"><span class="ryNqvb">Drop a Table</span></span></span><br />
</strong></p>
<pre class="line-numbers"><code class="language-sql">DROP TABLE IF EXISTS dbo.TableName;</code></pre>
<p><strong>2 -) <span class="HwtZe" lang="en"><span class="jCAhz ChMk0b"><span class="ryNqvb">Drop a View</span></span></span></strong></p>
<pre class="line-numbers"><code class="language-sql">DROP VIEW IF EXISTS ViewName;</code></pre>
<p><strong>3 -) <span class="HwtZe" lang="en"><span class="jCAhz ChMk0b"><span class="ryNqvb">Drop a Index</span></span></span><br />
</strong></p>
<pre class="line-numbers"><code class="language-sql">DROP INDEX IF EXISTS my_index ON IndexName;</code></pre>
<p><strong>4 -) <span class="HwtZe" lang="en"><span class="jCAhz ChMk0b"><span class="ryNqvb">Drop a Procedure</span></span></span><br />
</strong></p>
<pre class="line-numbers"><code class="language-sql">DROP PROCEDURE IF EXISTS ProcedureName;</code></pre>
<p><strong>5 -) <span class="HwtZe" lang="en"><span class="jCAhz ChMk0b"><span class="ryNqvb">Drop a Function</span></span></span><br />
</strong></p>
<pre class="line-numbers"><code class="language-sql">DROP FUNCTION IF EXISTS FunctionName;</code></pre>
<p><strong>6 -) <span class="HwtZe" lang="en"><span class="jCAhz ChMk0b"><span class="ryNqvb">Drop a Schema</span></span></span><br />
</strong></p>
<pre class="line-numbers"><code class="language-sql">DROP SCHEMA IF EXISTS SchemaName;</code></pre>
<p><strong>7 -) <span class="HwtZe" lang="en"><span class="jCAhz ChMk0b"><span class="ryNqvb">Drop a Trigger</span></span></span><br />
</strong></p>
<pre class="line-numbers"><code class="language-sql">DROP TRIGGER IF EXISTS my_trigger ON TriggerName</code></pre>
<p><strong>8 -) <span class="HwtZe" lang="en"><span class="jCAhz ChMk0b"><span class="ryNqvb">Drop a </span></span></span>Foreign Key<br />
</strong></p>
<pre class="line-numbers"><code class="language-sql">ALTER TABLE TabloName
DROP CONSTRAINT IF EXISTS ForeignKeyName;</code></pre>
<p><strong>9 -) <span class="HwtZe" lang="en"><span class="jCAhz ChMk0b"><span class="ryNqvb">Drop a User</span></span></span><br />
</strong></p>
<pre class="line-numbers"><code class="language-sql">DROP USER IF EXISTS UserName;</code><code class="language-sql"></code></pre>
<p>The above examples show different usage scenarios of <strong>&#8220;DROP IF EXISTS&#8221;</strong> command in SQL Server. These commands are useful for checking objects for existence before deleting them and reduce the chance of errors when writing code.</p>
<p>Good luck to everyone in business and life.</p>
<div class='epvc-post-count'><span class='epvc-eye'></span>  <span class="epvc-count"> 31</span><span class='epvc-label'> Views</span></div><p>The post <a href="https://mssqlquery.com/all-uses-of-drop-if-exists-in-sql-server">All Uses of DROP IF EXISTS in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
