Skip to content

Instantly share code, notes, and snippets.

IF left ([Title],1) = '1'
or left ([Title],1) = '2'
or left ([Title],1) = '3'
or left ([Title],1) = '4'
or left ([Title],1) = '5'
or left ([Title],1) = '6'
or left ([Title],1) = '7'
or left ([Title],1) = '8'
or left ([Title],1) = '9' then 'starts with a number'
@ateneva
ateneva / SQL_string_analysis.sql
Created June 3, 2018 15:16
How do I quickly evaluate a text string in SQL?
------------------------------------MySQL-----------------------------------------------------------
select
title as Adtitle,
#--count the number of words in the title
length(title) - length(replace(title, ' ', '')) + 1 as AdTitleWordLength,
#--analyse the punctuation of a title
case
@ateneva
ateneva / SQL_replace_trim.sql
Created May 27, 2018 15:11
How do I replace a string in SQL and make sure it has no extra spaces?
-----------------------------------------------Vertica-------------------------------------------------------------
select
replace('We love you', 'We', 'I'), -- reolaces a string with another
' I love you ' as MyPhrase,
ltrim(' I love you '), --removes spaces on the left
rtrim(' I love you '), --removes spaces on the right
trim(' I love you ') --removes spaces on both sides
@ateneva
ateneva / SQL_length.sql
Last active May 27, 2018 15:15
How do I determine the length of a string with SQL?
---------------SQL Server---------------------------------------
select
'I love you' as phrase,
len('I love you') as phrase_length,
len(' I love you ') as phrase_length2,
datalength(' I love you ') as phrase_length3
phrase |phrase_length |phrase_length2 |phrase_length3 |
-----------|--------------|---------------|---------------|
@ateneva
ateneva / SQL_substring.sql
Last active May 27, 2018 16:34
How do I get part of a string with SQL? (part 2)
-----------------------SQL server-------------------------------
select
'I love you' as phrase,
substring('I love you', 3,4) as phrase2
phrase |phrase2 |
-----------|--------|
I love you |love |
@ateneva
ateneva / SQL_instr_functions.sql
Created May 27, 2018 12:53
How do I find the first position of a string within a string with SQL?
-------------------------------------MySQL------------------------------------------------------------------------
select
'The first agreement is to be impeccable with your word.
When you are impeccable, you take responsibility for your actions, but you do not judge or blame yourself!
Being impeccable with your word is not using the word against yourself.
Being impeccable with your word is the correct use of your energy' as phrase,
@ateneva
ateneva / SQL_left_mid_right.sql
Created May 27, 2018 10:47
How do I get a part of a string with SQL? (part 1)
---------------------------------MySQL-----------------------------------------------------
select
'I love you' as phrase,
left ('I love you', 1),
mid('I love you', 3, 4),
right('I love you', 3)
phrase |left ('I love you', 1) |mid('I love you', 3, 4) |right('I love you', 3) |
@ateneva
ateneva / ForEachPT_RefreshALLPTs.bas
Last active April 22, 2018 08:42
How do I refresh all Pivot Tables in my workbook?
Sub RefreshPTs()
Dim Wbk As Workbook
Dim Wks As Worksheet
Dim PT As PivotTable
Dim PC As PivotCache
'*****************************
'written by Angelina Teneva
'*****************************
@ateneva
ateneva / ForEachPF_ShowFieldinPT.bas
Last active April 21, 2018 17:24
How do I quickly re-arrange Pivot Table Layout in VBA?
Sub ShowFieldinPT()
Dim Wks As Worksheet
Dim PT As PivotTable
Dim PF As PivotField
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'written by Angelina Teneva, Sept 2016
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
For Each Wks In ActiveWorkbook.Worksheets
@ateneva
ateneva / ForEachPF_ChangePFCaptionOfCertainFields.bas
Last active April 21, 2018 14:48
Change the Pivot Field Name of Specific Fields Only
Sub ChangePFCaptionOfCertainFields()
Dim Wks As Worksheet
Dim PT As PivotTable
Dim PF As PivotField
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'written by Angelina Teneva, Oct 2016
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
For Each Wks In ActiveWorkbook.Worksheets