Skip to content

Instantly share code, notes, and snippets.

@thorsman99
thorsman99 / SplitDelimited.sql
Created September 13, 2017 20:14
Split function SQL #SQL
CREATE FUNCTION dbo.split(
@delimited NVARCHAR(MAX),
@delimiter NVARCHAR(100)
) RETURNS @t TABLE (id INT IDENTITY(1,1), val NVARCHAR(MAX))
AS
BEGIN
DECLARE @xml XML
SET @xml = N'<t>' + REPLACE(@delimited,@delimiter,'</t><t>') + '</t>'
INSERT INTO @t(val)
SELECT r.value('.','varchar(MAX)') as item
@thorsman99
thorsman99 / rebuildallindexes.sql
Created August 30, 2017 14:45
Rebuild all indexes in a database #SQL
USE DatabaseName --Enter the name of the database you want to reindex
DECLARE @TableName varchar(255)
DECLARE TableCursor CURSOR FOR
@thorsman99
thorsman99 / RunningJobs.sql
Created August 10, 2017 19:04
Show running jobs on server #SQL
SELECT sqltext.TEXT,
req.session_id,
req.status,
req.command,
req.cpu_time,
req.total_elapsed_time
FROM sys.dm_exec_requests req
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS sqltext
@thorsman99
thorsman99 / FragLevel.sql
Last active April 7, 2017 13:05
Get fragmentation level of SQL Index #SQL
SELECT dbschemas.[name] as 'Schema',
dbtables.[name] as 'Table',
dbindexes.[name] as 'Index',
indexstats.alloc_unit_type_desc,
indexstats.avg_fragmentation_in_percent,
indexstats.page_count
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL) AS indexstats
INNER JOIN sys.tables dbtables on dbtables.[object_id] = indexstats.[object_id]
INNER JOIN sys.schemas dbschemas on dbtables.[schema_id] = dbschemas.[schema_id]
INNER JOIN sys.indexes AS dbindexes ON dbindexes.[object_id] = indexstats.[object_id]
@thorsman99
thorsman99 / FindString.sql
Created March 28, 2017 14:20
SQL to locate string in database
DECLARE @Search VARCHAR(100)
set @Search='CutoffDateTime'
SELECT o.name[Object Name],
CASE O.xtype WHEN 'P' THEN 'Stored procedure'
WHEN 'RF' THEN 'Replication filter stored procedure'
WHEN 'TR' THEN 'Trigger'
WHEN 'FN' THEN 'Scalar function'
WHEN 'IF' THEN 'In-lined table-function'
WHEN 'IT' THEN 'Internal table'
WHEN 'TF' THEN 'Table function'
@thorsman99
thorsman99 / Basic_Stored_Procedure_Structure.sql
Created March 8, 2017 15:38
SQL my preferred stored procedure base #sql
-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
@thorsman99
thorsman99 / Validation_with_Month_End_Example.js
Created March 8, 2017 15:34
Javascript validating entered dates with auto conversion to month end #javascript
<script type="text/javascript">
function Validate()
{
var element
element = document.getElementById("<%=ddlStartMonth.ClientID%>");
var StartMonth = element.options[element.selectedIndex].text;
element = document.getElementById("<%=ddlStartYear.ClientID%>");
var StartYear = element.options[element.selectedIndex].text;
element = document.getElementById("<%=ddlEndMonth.ClientID%>");
var EndMonth = element.options[element.selectedIndex].text;
@thorsman99
thorsman99 / Set_default_button.js
Created March 8, 2017 15:31
Javascript setting the default button to repond to enter key #javascript
document.onkeyup = CheckForSubmit;
function CheckForSubmit() {
if (document.getElementById('<%=submitControl.ClientID %>') != null && document.getElementById('<%=submitControl.ClientID %>').value != null && document.getElementById('<%=submitControl.ClientID %>').value.length > 0) {
var key;
var MyTag;
var EditTag;
//alert("here");
@thorsman99
thorsman99 / Listbox_dependent_on_other_listbox.js
Created March 8, 2017 15:28
Javascript to show/hide listbox options dependent on the chosen value of another listbox #javascript
function ManagerSetup() {
var lbDivisions = document.getElementById("< %=lstAMGRight.ClientID%>");
var lbManagersSelect = document.getElementById("< %=lstMGRRight.ClientID%>");
var lbManagersNotSelect = document.getElementById("< %=lstMGRLeft.ClientID%>");
//lbManagersNotSelect.options.length = 0;
lbManagersSelect.options.length = 0;
//Set all disabled, then enabled those in divisions
for (var j = 0; j < lbManagersNotSelect.options.length; j++) {
@thorsman99
thorsman99 / Insert_rows_in_table.js
Created March 8, 2017 15:14
Javascript insert rows at end of html table #javascript
function InsertRows() {
var intRowCount = Number(document.getElementById("RowCount").value);
var BeginRow = intRowCount+1;
var EndRow = intRowCount + 5;
for (var x = BeginRow; x <= EndRow; x++) {
var table = document.getElementById("TableBody");
var LastRow = table.rows(x - 1);
var row = table.insertRow(x);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);