-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1705 lines (1133 loc) · 34.4 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<title>Advanced Analysis with SQL - NYC Parks</title>
<meta charset="utf-8">
<link rel="stylesheet" href="slide.css"/>
</head>
<body>
<textarea id="source">
layout:true
<div class="header" style="display:block; text-align: left; color:gray; font-size:1em; position: fixed; top: 0px; left: 0px; height: 30px;vertical-align:middle;margin:7px 0 0 0;width:100%;background: #fcfcfc;
background: -moz-linear-gradient(top, #fcfcfc 0%, #d1d1d1 100%);
background: -webkit-linear-gradient(top, #fcfcfc 0%,#d1d1d1 100%);
background: linear-gradient(to bottom, #fcfcfc 0%,#d1d1d1 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fcfcfc', endColorstr='#d1d1d1',GradientType=0 );">
<img src="images/datapolitan_transparent_small.png" style="position: fixed;top: 2px; left: 5px; width: 14%;">
<p style="display:block;text-align:left;color:gray;font-size:1em; position: fixed; top: 0px; left: 155px; height: 30px;vertical-align:middle;margin:7px 0 0 0; ">Advanced Analysis with SQL - NYC Parks and Recreation</p>
</div>
<div class="footer" style="display:block;color:gray;position:fixed;bottom: 0px;left:0px;height:30px;vertical-align:middle;margin:0 0 0 0;width:100%;background: #fcfcfc;background: -moz-linear-gradient(top, #fcfcfc 0%, #d1d1d1 100%);background: -webkit-linear-gradient(top, #fcfcfc 0%,#d1d1d1 100%);background: linear-gradient(to bottom, #fcfcfc 0%,#d1d1d1 100%);sfilter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fcfcfc', endColorstr='#d1d1d1',GradientType=0 );">
<p class="footer">
<span xmlns:dct="http://purl.org/dc/terms/" property="dct:title">Advanced Analysis with SQL</span> by <a xmlns:cc="http://creativecommons.org/ns#" href="http://www.datapolitan.com" target="_blank" property="cc:attributionName" rel="cc:attributionURL">Richard Dunks</a> and <a xmlns:cc="http://creativecommons.org/ns#" href="http://ebrelsford.github.io" property="cc:attributionName" rel="cc:attributionURL">Eric Brelsford</a> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International License</a>.<a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/"><img alt="Creative-Commons-License" style="border-width:0" src="https://i.creativecommons.org/l/by-sa/4.0/80x15.png" /></a>
</p>
</div>
<!-- <p class="footer">
<span xmlns:dct="http://purl.org/dc/terms/" property="dct:title">Data Analytics for NYC Parks</span> by <a xmlns:cc="http://creativecommons.org/ns#" href="http://www.datapolitan.com" property="cc:attributionName" rel="cc:attributionURL">Richard Dunks</a> and <a xmlns:cc="http://creativecommons.org/ns#" href="http://www.tinypanther.pizza" property="cc:attributionName" rel="cc:attributionURL">Julia Marden</a> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International License</a>.<a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/"><img alt="Creative-Commons-License" style="border-width:0" src="https://i.creativecommons.org/l/by-sa/4.0/80x15.png" /></a>
</p> -->
--
class:center, middle
![img-center-50](images/datapolitan.png)
# Advanced Analysis with SQL<br>City of New York Parks and Recreation
- - -
## Instructors: Eric Brelsford and Richard Dunks
## Teaching Assistant: Sarah Kontos
### Follow along at: http://bit.ly/advanced-analysis-sql
#### See the code at: http://bit.ly/advanced-analysis-sql-code
---
class:center,middle
<!-- start of A block -->
# Welcome
???
+ Facilitators and TA introduce themselves, establishing mastery but also creating openings for vulnerability and awareness
---
# A Few Ground Rules
???
+ Facilitators establish the intention we have for the culture of the classroom
--
+ Step up, step back
--
+ One mic
--
+ Be curious and ask questions
--
+ Assume noble regard and positive intent
--
+ Respect multiple perspectives
--
+ Listen to comprehend, not to respond
--
+ Be present (phone, email, social media, etc.)
---
# Introduce Yourself to Your Neighbor
+ Who are you?
+ Where do you work?
+ What has been the proudest moment in your job?
+ What's your favorite national or state park?
???
+ An opportunity for participants to get to know each other and settle into the class
+ We bring them into a positive space related to their work
+ Facilitator reminds them the data isn't the end but the means to achieve the actions that make us feel good about our jobs
---
# Housekeeping
???
+ Facilitator sets expectations with the students
+ Establishes the "contract" for the class
--
+ We’ll have one 15 minute break in the morning
--
+ We’ll have an hour for lunch
--
+ We’ll have a 15 minute break in the afternoon
--
+ Class will start promptly after breaks
--
+ Feel free to use the bathroom if you need during class
--
+ Please take any phone conversations into the hall to not disrupt the class
---
# What to Expect Today
--
+ Introduction to databases
--
+ Introduction to SQL
--
+ Hands-on experience writing and using SQL
--
+ A chance to explore SQL and perform some analysis using it
---
class:center,middle
# Let's Get Started
---
class:center,middle
# A Quick Recap from <br>[Introduction to Data Analytics](http://training.datapolitan.com/parks-intro-analytics/#1)
???
+ Facilitator concludes the walk-through to shift to a discussion of data and analysis
---
# The Value of Data
???
+ Facilitator brings back discussion of the value of data, prompting participants to describe the value of data in their job
+ Optionally, facilitator can ask the value of the data just aggregated and how it might be useful to decisionmakers
--
+ Data tells a story about something that's happened
--
+ Can describe what happened directly or indirectly
--
![img-center-80](images/parks-inspections-visitors.png)
???
+ Facilitator helps participants see the direct measurements of service requests while indirectly measuring when Park incidents actually happen
---
class: center,middle
# What are some of the data challenges you face?
???
+ Facilitator holds discussion with participants about the data issues they face on a regular basis
+ Facilitator prompts participants to think about their data problems as we go through the data techniques today
---
exclude:true
class:middle
> Facts do not "speak for themselves." They speak for or against competing theories. Facts divorced from theory or visions are mere isolated curiosities.
## -Thomas Sowell *A Conflict of Visions*
---
# What is Analysis?
--
>“Analysis is simply the pursuit of understanding, usually through detailed inspection or comparison”
## - [Carter Hewgley](https://www.linkedin.com/in/carterhewgley), Senior Advisor for Family & Homeless Services, Department of Human Services, District of Columbia
???
+ Facilitator prompts participants to think about analysis in the context of their work and what it is they are trying to accomplish
---
class:center,middle
# A Quick Recap from <br>Data Analysis with Excel I and II
---
# 5 Data Analytics Tasks
???
+ Facilitator introduces the 5 main tasks of analysis
+ Facilitator reminds participants we've talked about these in previous courses
+ We're going to look at the first three in SQL
--
1. Filtering
--
1. Sorting
--
1. Aggregating
--
1. Transforming
--
1. Visualizing
---
class:center,middle
# Importing data into Excel with SQL
???
+ Walk through importing data with SQL into Excel with a simple query
---
# Importing data from a database
![img-center-100](images/power_query.png)
+ Click on the **Power Query** ribbon
---
# Importing data from a database
![img-center-40](images/power_query_sql_server.png)
+ Power Query > From Database > From SQL Server Database
---
# Importing data from a database
![img-center-100](images/server_connect.png)
+ Enter `data.nycdpr.parks.nycnet` in the **Server** field
+ Enter `DWH` in the **Database** field
---
# Encryption Warning
![img-center-100](images/encryp.jpg)
## Just select "OK"
### [For more information](https://docs.microsoft.com/en-us/sql/relational-databases/security/encryption/sql-server-encryption?view=sql-server-2017)
---
# Importing data from a database
![img-center-70](images/server_navigator.jpg)
+ View the accessible databases
---
# Importing data from a database
![img-center-70](images/server_navigator.jpg)
+ Find `tbl_ref_calendar` and click **Load** to load it
---
# Importing data from a database
![img-center-100](images/tbl_ref_calendar_load.jpg)
---
class:center,middle
# What did we just do?
---
class:center,middle
# What is a database?
---
# What is a database?
--
+ A software system that manages storing and retrieving data
--
+ The software for a database often runs on a server, not the computer you're using
--
+ There are many different database systems, today we'll be using Microsoft's SQL Server
--
+ A database stores data in *tables*
---
# Tables
--
+ How data is stored in a database
--
+ Similar to a worksheet in Excel
--
+ Organized in *rows* and *columns*
--
![img-left-50](images/table_row.png)
--
![img-right-50](images/table_column.png)
---
class:center,middle
# Let's do that again using SQL
---
# `SELECT`
--
+ How you specify which columns to get from a table
--
+ Start with `SELECT *`, which gets all the columns
--
+ Separate column names with commas (`,`)
---
# For example:
![img-right-30](images/cal_query_crop_box.png)
```sql
SELECT *
FROM tbl_ref_calendar
```
--
+ Get all of the columns from the `tbl_ref_calendar` table
---
# For example:
![img-right-30](images/cal_query2_crop.png)
```sql
SELECT ref_date, fiscal_year
FROM tbl_ref_calendar
```
--
+ Only get the `ref_date` and `fiscal_year` columns from the `tbl_ref_calendar` table
--
![img-center-45](images/cal_query2_result_crop.png)
---
# `FROM`
--
+ How you specify the table you are querying
--
```sql
FROM tbl_ref_calendar
```
---
class:center,middle
# Why use a database?
???
+ Facilitator walks through benefits of using a database: single source of truth, always the latest data
---
# Why use a database?
--
+ Single source of truth
--
+ Don't have to worry about moving and sharing files
--
+ Can be faster and more consistent than Excel files
---
class:center,middle
# Why use SQL?
???
+ Facilitator walks through benefits of using SQL: you need to in order to communicate with a database, but also it can be self documenting: "here's the query I used" rather than "I clicked here, then there, then picked this option..." in Excel.
---
# SQL
--
+ The language databases understand
--
+ If you want to use a database, you will need at least a little SQL
--
+ It makes queries replicable—you can run the query again and be sure you will get the same results
--
+ ...and someone can use your query and be sure they will get the same results, too!
---
# Editing a query
--
+ You don't have to create a new query each time you want to change the SQL
--
+ If you are experimenting with a query, we recommend *editing* the SQL instead
---
# Editing a query
![img-center-100](images/query-edit-1.jpg)
+ Double click the **Source**
---
# Editing a query
![img-center-60](images/query-edit-2.jpg)
+ Edit the SQL as usual
---
# Your Turn
--
+ We talked about using `SELECT` to pick which columns to pull from a table:
--
```sql
SELECT ref_date, fiscal_year
FROM tbl_ref_calendar
```
--
+ Write a SQL query to get only the `ref_date`, `fiscal_day`, `fiscal_week`, `fiscal_qtr` columns from `tbl_ref_calendar`
--
+ Run the query in Excel
---
# Data Analytics Tasks: Filtering
???
+ Facilitator leads brief discussion of filtering, soliciting examples from participants on applicability to their own work
--
+ Show a preview of rows
--
+ Or only show rows that contain some value
--
+ Can filter by multiple values
--
+ Can filter by values in multiple columns
---
# Daily Tasks
--
+ Labor booking for routine litter and cleaning
--
+ Approximately 78,000 tasks logged a month (~936,000 a year)
--
+ Nearly 3.9 million tasks logged as of Spring 2019
--
+ Can't work with all this data at once
---
# Previewing rows
--
+ You can use `SELECT TOP` to get the first set of rows
--
```sql
SELECT TOP 5 *
FROM tbl_dailytasks
```
--
+ Select the top 5 rows from `tbl_dailytasks`
--
+ Without *sorting* the data this could be any 5 rows—we'll talk about sorting soon
--
+ Can use this to view the column names and the values in the table
--
+ For more information on TOP, [check out this tutorial](http://www.sqlservertutorial.net/sql-server-basics/sql-server-select-top/)
---
# Filtering by column values
![img-center-100](images/filter_slide83.png)
???
+ Facilitator reminds students about filtering in Excel
---
# In SQL we use `WHERE`
--
+ Lets you limit which *rows* are selected from the database
--
+ You define a condition that is either true or false, and every row is evaluated using that
???
+ Look back at filters in Excel
+ Do something similar in a WHERE
+ `AND` and `OR`
+ Watch out for precedence with `AND` and `OR`
+ `NULL` vs empty strings
---
# `WHERE`
```sql
SELECT TOP 10 *
FROM tbl_dailytasks
WHERE graffiti = 1
```
--
+ Select the top 10 daily tasks rows where graffiti was present
--
+ Note that we are using `TOP` along with `WHERE` here so we don't load too much data
---
# But `graffiti` is `TRUE` or `FALSE`
![img-center-80](images/graffiti-bit.jpg)
--
+ We'll be talking about data types soon, but this column is stored as a `BIT`, which is `1` or `0`
--
+ This is a common way to store values that are true or false
--
+ In this case, Excel is displaying the values at `TRUE` and `FALSE`, but in the database they are `1` or `0`
--
+ Learn more about [BIT here](http://www.sqlservertutorial.net/sql-server-basics/sql-server-bit/)
---
# `WHERE`
```sql
SELECT TOP 10 *
FROM tbl_dailytasks
WHERE nhours > 1
```
--
+ Select the top 10 daily tasks rows where `nhours` is greater than 1
---
# `WHERE` operators
+ `=`
--
- equal to
--
+ `>`
--
- greater than
--
+ `>=`
--
- greater than or equal to
--
+ `<`
--
- less than
--
+ `<=`
--
- less than or equal to
--
+ `!=`
--
and `<>`
--
- not equal to
--
+ Learn more in [the SQL Server documentation](https://docs.microsoft.com/en-us/sql/t-sql/language-elements/comparison-operators-transact-sql?view=sql-server-2017)
---
# Your Turn
+ Create an SQL query with a `WHERE` clause
+ Continue using the `tbl_dailytasks` table
+ Try writing a condition on a column with numbers in it
+ Make sure you limit the results with `TOP`
+ If you have time, try writing a condition on a column with text in it
---
class:center,middle
# What if you want to combine conditions?
---
# `WHERE`
```sql
SELECT TOP 10 *
FROM tbl_dailytasks
WHERE graffiti = 1 AND nhours > 1
```
--
+ Select the daily tasks rows where graffiti was present *and* the number of hours taken was over 1
---
# `WHERE`
```sql
SELECT TOP 10 *
FROM tbl_dailytasks
WHERE omppropid = 'X045-01' OR omppropid = 'X045-02'
```
--
+ Select the daily tasks rows for `omppropid` equal to either `X045-01` or `X045-02`
---
# `WHERE`
```sql
SELECT TOP 10 *
FROM tbl_dailytasks
WHERE NOT (omppropid = 'X045-01' OR omppropid = 'X045-02')
```
--
+ What do you think this does?
--
+ Why would we use parentheses here?
--
+ Learn more about [SQL's order of evaluation](http://www.peachpit.com/articles/article.aspx?p=1276352&seqNum=6) and [propositional logic](https://www.tutorialspoint.com/discrete_mathematics/discrete_mathematics_propositional_logic.htm)
---
# Your Turn
+ Create an SQL query with a `WHERE` clause
+ Continue using the `tbl_dailytasks` table
+ Start with one condition, then try adding conditions using `AND` / `OR`
---
# Documentation
--
+ It never hurts to keep track of your work
--
+ Can help you remember the steps you took
--
+ Can help you find errors in your methods
--
+ In the future, can help you reconstruct your work and adapt it to solve different problems
--
+ Colleagues and your future self will find it helpful
---
class:center,middle
# How should we document SQL work?
---
# Documentation with SQL
--
+ If you're working in Excel, you could use a separate worksheet where you track the steps you have taken
--
![img-center-100](images/document_ex1_rev_box.png)
---
# Documentation with SQL
+ If you're not working in Excel, you could keep a simple text document in Notepad where you keep track of the queries you're using
--
+ Either way, be sure to track the queries you're using and the date that you accessed the data
--
+ And if you make changes to the data after getting it from the database, track those steps too
--
+ The queries we're talking about today are documented in your [today's handout](workbook.pdf)—you can take notes there, too
---
class:center,middle
# WRAP-UP
![img-center-100](images/implementation_guide.png)
---
class:center,middle
# 15 MIN BREAK
---
# Data types
--
+ Every column in a database table has a type
--
+ A column's type determines what you can do with that column
--
+ For example, you can do math on numbers but not on text
--
+ The database will refuse to store data that doesn't match the column's type
--
+ Unless you're an administrator, you most likely will not be changing the column's type
???
+ Take a look back at data types in Excel
+ Mention that data types are more rigid in a database
+ Integers, numeric, datetime, strings
---
# Common numeric data types
--
+ `int`
--
- a number with no digits after the decimal point, like `5` or `100349`
--
+ `numeric`
--
- a number with digits after the decimal point, like `5.01` or `100.349`
--
+ `bit`
--
- a number that can only be `0` or `1`; as we mentioned earlier, this is commonly used for boolean (true or false) values
---
# Common text data types
--
+ `varchar`
--
- text with a maximum length, like `Queens`
--
+ `text`
--
- text with no maximum length, like an open comment field
---
# Other common data types
--
+ `date`
--
- a specific date, like `2002-10-17`
--
+ `datetime`
--
- a specific date and time, like `2002-10-17 06:13:00`
--
+ Learn more in the [SQL Server documentation](https://docs.microsoft.com/en-us/sql/t-sql/data-types/data-types-transact-sql?view=sql-server-2017)
---
# Why do data types matter?
--
+ Databases and SQL are less flexible than Excel here
--
+ Knowing the data type of a column tells you what to expect in that column
--
+ For example: `date` columns will never include time
--
+ Some functions only work on certain data types
---
# What data type should zip codes be stored as?
--
+ `numeric`?
--
+ `varchar`?
--
+ How do you handle a zip code like **08904**?
--
+ or **11368-3398**?
---
# Working with Data Types
--
+ Can get the information on types from the `INFORMATION_SCHEMA` table:
--
```sql
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='tbl_dailytasks'
```
--
+ If you need a column to be a different data type, you might find [`CAST` or `CONVERT` useful](https://docs.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-2017)
---
# Your Turn
???
+ Quick demonstration of why data types will matter
+ Instructor could show how `YEAR()` does not work with just any column
--
+ There are functions that work only with specific data types
--
+ For example, `YEAR()` only works on columns that are `date`s
--
+ See other date and time functions in the [SQL Server documentation](https://docs.microsoft.com/en-us/sql/t-sql/functions/date-and-time-data-types-and-functions-transact-sql?view=sql-server-2017#DateandTimeFunctions)
--
+ Try selecting the top 10 rows from `tbl_dailytasks` with the columns `date_worked` and `YEAR(date_worked)`
---
# Formulas
--
+ You can do arithmetic and combine values in the ways you're used to in Excel
--
+ You can also get a modified version of a value
???
+ Take a look back at arithmetic in Excel
+ You can do arithmetic in SQL, too
+ Order of operations is similar, using parentheses as needed
+ Show equivalent in SQL to something relatively complicated in Excel
+ Introduce `AS` for setting the output column name
---
# Using formulas with numbers
--
+ You can use the usual mathematical operators here:
`+`, `-`, `/`, `*`
--
+ For example, you can multiply a column by a number
--
+ `nhours * 60`
--
would find the number of minutes
--
```sql
SELECT TOP 10 nhours * 60
FROM tbl_dailytasks
```
---
# Using functions with text
--
+ Remember the `LEFT` function in Excel?
--
![img-center-80](images/leftfunctionaction.png)
--
+ It extracts part of a cell's value
---
# Using functions with text
--
+ We can do the same thing with SQL
--
+ `LEFT` extracts part of a string
--
+ The format is `LEFT(column, length)`
--
+ `LEFT(omppropid, 1)` would select the first letter from the `omppropid` column
--
```sql
SELECT TOP 10 LEFT(omppropid, 1)
FROM tbl_dailytasks
```
--
+ Find more functions in the [SQL Server Documentation](https://docs.microsoft.com/en-us/sql/t-sql/functions/string-functions-transact-sql?view=sql-server-2017)
---
# Your Turn