#TSQL2sday #149: T-SQL Advice you’d give to your younger self

Hello! As I mentioned on my invitation post, this month I’m hosting the T-SQLTuesday and my theme was “T-SQL Advice you’d give to your younger self”. Here’s my contribution.

If I could go back in time, and maybe send myself an anonymous email (which would probably go to my spam box? hmmm), this is what I’d like to find:

  • It’s ok to use Google, like every day multiple times a day. Sometimes we face issues and we may tend to get super deep into it, but forget that we do not need to re-invent stuff. Others have likely been through what you are living and if they were great human beings they even talked about it on the internet. Isn’t that wild? We must learn to be humble. It’s OK to have your colleagues and managers see an open Google tab, as long as you are delivering the job you’ve been given, you’ll be just fine.
  • Comment your code! I’ll give you a week and you’ll forget what you were doing anyways so leave some useful comments. That will help you and others who will pick up on your work after you. I like to add the following to all my scripts: author, date, project name and a little description. Sometimes I even add the name of the person who requested the work, makes it easier to talk directly whit whom I need to.
  • Do you know those times when you find yourself repeating the same piece of code over and over? There are better ways to version your code, but as a starting point, you can create a folder on your documents and start adding sql files there that you can refer to later. Here’s what I have on my folder:
    • how to search for a column or table with a specific name;
    • how to use date functions with examples such as DATEADD, DATEDIFF;
    • how to check for temp tables column names;
    • how to format phones when they’re all messed up;
    • how to find databases, filenames and paths;
    • getting a path for sql server log error message;
    • check you database size;
    • how to check for used and used space on the database
    • and whatever else your heart desires! This is the type of work that I do and that later will save my time. I’ll probably make more posts to show my solutions to this handy scripts 🙂
  • Always ident your code and use Uppercase for all commands. You’ll notice that one day you won’t even trust people who don’t do that (oh, the drama).
  • If you have someone senior than you at work, ask them questions when you’re stuck, they probably will solve the problem faster, more elegantly and you’ll learn how to do it by watching them!
  • Make it a habit of NOT using “*” when you are doing a SELECT query. It’s ok to do it once so you can see the table, but then your real query should list the fields you want to see, by name. ✨ Marie Kondo your query and let go of what does not serve you anymore ✨
  • Don’t wait until you are an accidental DBA to learn the fundamentals. Even if you just want to be able to discuss problems with them.
  • Learn how to read the query execution plan. It will help with understanding bottlenecks and how to optimize your queries.
    • …also, when optimizing, don’t struggle about the milliseconds you might save. Nobody will notice. You want to focus on the changes that will give you the biggest differences on execution time. Think about how big the impact will be by projecting “if I don’t fix this will someone come to my table with a very angry face?”
  • Whenever you start writing an UPDATE or DELETE statement, immediately write FROM… WHERE… This will save you the headaches of forgetting to finish your command and updating or deleting everything at once.

That’s it for me. Hope this was helpful for some beginners out there. Now tell me…

What do you wish you knew earlier? 🙂

#TSQL2sday #149 Invitation: Blog about T-SQL Advice you’d give to your younger self

T-SQL Tuesday is a monthly blogothon where a host chooses one topic and bloggers around the world give their ideas about it. This month is my first time being the host and I’m so excited!

April’s theme: if you could give advice about T-SQL to your younger self, what would you say? I’m not defining any more specific sub-topics here, I’m opening the floor to hear ideas about your past experiences and what you wish you knew better back then.

Some rules:

  • You have to publish your post next Tuesday, on April 12th.
  • Include the T-SQL Tuesday logo, and if somebody clicks on the logo, it has to link back to this post.
  • Include a link to this blog post, or after you publish it, leave a comment here so I can track you down for the roundup blog post next week. If you want to share via Linkedin as well or other social media, please do and I’ll link you too.
  • Isn’t this idea awesome? Do you want to join? For future hosts, please reach out to Steve Jones via Twitter on @way0utwest. Just a heads up that there is a line of people who also want to host, so if you’re uncertain, do it anyway because you have time to prep!
  • Why host? Well, as a blogger, this is a wonderful way to ramp up your visualizations and to get another bloggers to know your work. Plus, it’s one way to give back to the community. Check out how great are the posts by looking into the previous topics.

I hope this is going to be fun and I’m definitely looking forward to reading your thoughts on my theme. Let’s get writing!

my very advanced drawing skills, as always

For girls struggling to find their way in tech

Hi! This post is kind of a letter to my past self. Things may get personal here but I’m being vulnerable with the hope that girls will see they are not alone and that things get better.

Why did I chose tech?

When I was finishing high school, my city had a project to include a technical to our last year and a half of school so that when we were done we could potentially be trainees somewhere. I liked the idea and I went to study Computer Technology. At the end of the course, we could choose between programming or databases. At that time I may not have realized, but my main database fundamentals teacher was a woman. She was very young and bright. I believe she inspired me to learn more, so I signed up for databases.

I then liked the classes and thought I was good at it. So, when I signed up for college my choice was Databases again. At the time we had a full course designed just for that. The director of our database groups was a teacher that lead the main DB classes. She was fierce. She was gorgeous. She would enter the room to teach, usually wearing high heels and skirts and dresses (which is not what you saw the other male teachers using). And she would kick ass. I immediately gravitated towards her, and then it was clear to me I looked up to her, and more interestingly: other people looked up to her, because she was intelligent and she knew what she was doing. Eventually, she was the one who mentored me on my final project, which I’m forever grateful for, I loved our on to one meetings and insights.

Work is not what they teach you at school

In my class at college I had few women colleagues, which lead me to think that at work it would be the same…and it was. It was hard to find women at the workplace (still is, but slightly better), specially in leadership roles. From my career experience, most of the time I was part of the very small group of women in the team, who also would take more project management than development or databases. Fun fact: the first time I joined a big company, there was only one other woman on the team of my hiring managers. She was the one that read my CV and made sure I was being sent to the tech team instead of the business one. I had no idea how important her braveness would be for me, I’m grateful for her actions, someone whom I learned a lot with. Something that I never asked and never gave much thought about is the fact that a group of men saw my resume, and still thought I was more suited to the business team (spoiler: I was not).

I believe it wasn’t until I got to Canada that I had my first woman manager. It was a nice experience to have someone who you can be open about misogyny at work, and not worry so much about what the throwback may be.

I wanted to tell you this part of my history to give an example of how much it means to me to have women who I can look up to and count with. Of course I also worked with wonderful men and learned a bunch with them, but the times I would believe myself the most was when I had other she to walk with me.

Having one woman in a class full of men does not mean she is exceptional, it means we screwed up something along the way and left many others out.

How can you help?

  • If you can and this is a topic that interests you, I’d suggest mentoring someone. You have no idea how much a little bit of guidance goes a long way. Be open to hear, see and take action.
  • When you see women struggling at work, speak up and also give them a hand and help them get up on their feet. If you are afraid of facing consequences (which I’m sorry if you do), find someone at work who can help you, someone you know that will back you up. You manager is not your only option. Be vulnerable and you’ll see right away who’s with you.
  • Lastly, consume female references. Be mindful of seeing and praising women’s work, for the sake of their brilliance and the stories you would not hear from others.

For the girls struggling

  • You are not alone. Seek for others in the same position as you are, do not take for granted the comfort and power of being in a group that supports you.
  • Find your anchor. I would always gravitate towards someone who I knew would back me up. Follow them, make them see you and you’ll be heard.
  • Don’t let the fears wear you down. Make yourself heard. And if you don’t have the strength to speak up when you need, that’s ok too, you don’t have to solve the worlds problems by yourself.
  • For all the girls out there struggling: I feel you, I hear you, I see you. Reach out if you need someone to talk to.

I hope we get better for our future generations.

Women in Technology

Hello! It’s been a while and I wanted to talk about women in tech, given that in March 8th we celebrate the International Women’s Day! Since this is a topic dear to my heart, I’m dividing my ideas into some posts which I’ll be sharing during this month.

Quick historical facts about women in tech

Did you know that…

  • the first person to write an algorithm was a woman? Her name was Ada Lovelace. She even has a language, Ada, named after her.
  • the term “bug” was invented by Grace Hopper when she literally removed a fly – bug from a computer.
  • when universities started to create computer science courses, they were majority filled by women? The reason behind that is because using a computer was seen as a job secretaries would do, hence something more women used to do than men.
  • by not introducing girls to science during their early ages we may actually discourage them to get into STEM? Seeing references which we can relate to are a big deal.

To finish, I wanted you to think about, and maybe share below, who are the women in tech that inspire you today? What are your female references? If your list is short, is that because you think they don’t exist (hint: they do!!) or because you haven’t exposed yourself to them yet? Take some time to reflect on that 🙂

See you soon!

Book review – “Storytelling with Data”, by Cole Nussbaumer Knaflic

Hello! As you may have noticed from my Reading List page here, I like to read. Recently, with the new job, I was looking for a book that talked about Data Visualization. While searching, I came across “Storytelling with Data”, and it was not the first time I saw it. After checking a few reviews, I decided to invest my time reading it. Turns out it was a great decision! I liked it so much that I wanted to talk about it here, so here it comes, grab your reading glasses.

Who should read this book?

I believe this book is great for beginners to BI and Data Visualization. However, it does not require any fancy tools. In fact, the author claims to have used Excel in all the examples. In short, if you work with and have to present data to others, this will be a valuable reading.

What’s covered in this book?

In some of the main chapters, you’ll find information about how to pick the right design for the story you are trying to tell. Bellow is a very brief summary for some chapters covered.

The importance of context

Sometimes we get so into the analytics part of the visualization, the we can easily forget the context behind it. Which is why it’s good t think about who’s this data for, what they are planning to do with it and which questions are trying to be answered by this viz. If you don’t considered this, then you’re just plotting numbers on a report that people will not know what to do with.

Key points: engage with the audience, leave some prompts with call to actions that will guide them to where they should look to understand what is being presented.

Effective visuals

Sometimes, the shine things are not the best. As a BI dev, you may want t go crazy and try something that is rarely used, because it’s cool. However, when you do this, you let go of the main objective of your work: you’re not doing this for yourself. Somebody or some team requested this, and people are more at ease with things they already know how to operate. If they only use excel and visualize data with tables, they may not be interested in seeing a area or stacked bar, because that translates to extra brain effort.

Key points: avoid pizza and donuts charts always, because our eyes can misinterpret them. When possible, stick to familiar forms for the audience (bars, line graphs etc).

Eliminating clutter

This is a hard one for me. I find it one of the most difficult things to do, which is eliminate what is unnecessary. If you want to tell too much at once, you end up not telling anything at all. It’s a good practice to keep it short, keep it simple. Answer what is being asked, and remember you do not need to make the audience take the analyzing journey with you. That was your job! Their precious time will be used to see the final results. You can brag about the hard work you do in a meeting with your managers, for example (performance reviews exists for this!)

Key points: Only show what’s important for the context of the story, remove repetitive things and summarize when possible. Even considere plain big old numbers to highlight very specific topics instead of showing a whole graph for it (for example, when showing a product price range through time, instead of using a graph, just say “Product X had an increase of XX% in x years.”)

Focusing attention

When looking at a data visualization, it’s easy to get lost. If it’s too colourful, too bright, or dull, you may not know what is the key point being presented to you. As mentioned, the audience is not there to go through the analysis with you. That part should be done and you can call out to the audience’s attention by using:

  • color: you could tone everything down, for example by making use of gray tones, and use 1 single color that would pick someone’s attention because it stands out so much.
  • bold letters: bold letters are good because they don’t make your viz look clutter as underline may do, and are more perceptive than italic.
  • sizing: you can make things bigger to call attention, just make sure it’s an appropriate size.

Think like a designer

Wearing your design hats! She mentions in the book that people tend to think that beautiful is in fact perceived as more efficient. This may be a neglected point when you don’t have much time to put into the design, but taking this extra step shows respect for your audience and your data (awn!).

Key points: who doesn’t like pretty things???

Storytelling

Storytelling is what ties it all together. You could have done a great analysis, found awesome stuff about the data, presented it beautifully, but if you’re missing the context because you can’t put it into a “story”, and just like that, you may have lost the audience.

Heard of Death by Powerpoint? Yeah. Creating a narrative that guides the people to your findings, using their language, showing the steps, involving them and asking them to follow along with you is important.

Key points: she really got me thinking about the prompts we can make to the audience. Using questions is an awesome way of inviting people to think about what they are seeing. And when that is narrated by a story, it’s more compelling. It’s hard to follow numbers on a page, it’s easy to see a story develop itself in front of you.

Would I recommend this book?

It’s clear by now that I loved it. It feels like I took a shortcut to avoiding some headaches that would have come with experience in BI. With that being said, don’t go in thinking you’ll see technical stuff about how to actually create vizes, specially with a tool like Tableau or Power BI, there is nothing in there about this.

Other resources

Cole has a blog called (brace yourselves) Storytelling with Data! It’s full of resources and has more info about the workshops and material they have.

That was it for me. I’m excited to check books that were mentioned on this one, and if they’re interesting I may write a review for them too. See you soon!

Work/Life Balance – #TSQL2sday

This post is a contribution to T-SQL Tuesday! I’m excited to be here once again 🙂

This month we’re talking about Work/Life balance and I’m contributing with some things that I’ve learned along the way.

When I start thinking about the topic, work/life balance sounds as something that may be separated in two big parts: your personal challenges, and the culture of the place you work at. It’s important to be aligned with your employers culture – but only up until a point that makes sense and it’s comfortable to you.

What I mean by comfort, it’s making sure that you’re able to have your professional life and still have time for your own stuff. After all, work is only part of your life.

I work since I was 15. Since I started, I have a better idea of how to take better care of my mental and physical health. These two topics are my pillars when thinking about the balance I need. I want to be able to make decisions on my professional life, while still taking care of me along the way.

Mental health it’s still health!

There’s a big stigma that surrounds this theme. Some people are afraid to talk about it. That’s why whenever I’m having a casual conversation at work, I’ll usually tell the person that I’m currently doing therapy. I like to talk about my hobbies and things outside work. I believe that sometimes we get stuck on day to day tasks, and it’s a nice reminder for others that hey – you are also human! Also, after I open up more, others seem more encouraged to talk about their own stuff.

We need to acknowledge and be constantly remembered that we’re all people with our own outside-of-work lives.

Things that help me mentally

  • making sure I take some time-off: who does not love vacations? They’re great, but lately I’ve been more open to paying attention to my body and mind working hours. During the day, I’ll make sure to block time for high focused work when I’m most alert. I know that I don’t function before a good breakfast, and that coffee (or just taking the break!) is a perfect mood booster for my afternoons, when I’m getting tired. This leads to…
  • taking smarter breaks: this one is easier said than done. I have a hard time not leaning into social media whenever I need a break. So sometimes I’ll try to do something small like picking up snack, filling my water bottle, stretching with some guided video, reading a book’s chapter, reading an article etc.
  • never work when I’m sleepy, thirsty or hungry: this is something that I internally always felt. Then, last year I joined a conference where one of at presenters said this when she was finishing her talk, and it was like my unconscious shouting to me “I’ve been trying to warn you about this, other people feel it too!”. This is why it’s so important to pay attention to your internal body clock, knowing when you’re likely to be hungry or tired can help you better plan your day. Pro-tips: don’t be the person who schedules meetings during lunch time, people will not love you.
  • having hobbies: besides making good conversations topics, I love doing manual work like drawing, cooking, and I’ve even learned some macrame. It’s amazing how into the flow my work gets when I’m doing an activity that requires my full attention like that. Doing something that relaxes you is like giving your brain a break from the work. So that next time you have a challenge in front of you, it may be easier to think in a different way and come up with fresh new ideas.
  • meditate: I’m anxious. Most people say I look super zen. Luck for them (and me) they can’t see what goes on inside an over thinker’s head. Most of the time, I get lost in thought. It happens. I try to deal with it by acknowledging the facts – this is just a thought, that I created, or, this is just a feeling, and everything is fine, nothing will blow up. To me, meditation is not relaxing, but awareness instead. It’s an anchor that provides tools for when I’m drifting off being concerned about things that will probably never happen.
  • be open to your loved ones: with most of us working from home, it’s likely that you need to share your space with others. I try to be honest about my work and I clearly say when I’d rather not be disturbed for a couple minutes, and that maybe it’s better to start lunch without me because I’ll be late.

Taking care of your body

I’m not going to lie and say I love exercising. I wanted to be the person that wakes up at 5AM, drinks some weird green juice, gets yoga done, takes a shower and it’s ready to work before everyone else is even up! I’m not that person and I probably won’t never be.

I have a tendency to visualize this perfect scenario and then if one tiny part does not go as planned, I’ll just throw it all away. Waking up at 7AM instead of 5? I guess I won’t have time to exercise today after all… Did not exercise when I was planning to? Oh I guess it’s irrelevant to have my healthy dinner than, I’ll just have pizza again.

Because of this behaviour of mine, I need to remind me that it’s ok if I could not wake up at the right time. Or if today I have a headache that won’t allow me to move as freely as I wanted to. Sometimes I’m just not in the mood.

The important thing is doing little things. Doing the best you can do. Each day. Don’t be too hard on yourself. But also make sure you’re not being too soft, like if you were your grandma when the kids wanted something.

Some things that help me take care of my body

  • exercising, and doing so because I love my body. Try different things, eventually you will find something that will help you relax, disconnect, feel stronger even! This leads to a more positive mindset for me. Like the day I did a plank for over a minute and felt like NOBODY MOVE, I’LL FINISH THAT PROJECT TODAY.
  • eating healthy, whole, foods: I come from Brazil, and we have one of the best cuisines – not because it’s fancy, but because it’s nutritiously rich. Most people will eat good proportioned food that is home made and that should be (nutritionally) enough for our bodies. Not to mention we actually leave our computer desks and take proper breaks to eat and refill on energy. I was not someone who cooked, but apparently I like it now and I’ve learned to value more the full process. At home, we make lists of what we’ll eat during the week, then grocery shopping and sometimes we even do a meal prep so that we can freeze things. Having my lunch be frozen so that all I need is to microwave it makes me instantly happier and gives me extra free time for my lunch break, yay! (Of course this is coming from someone with no kids and I have a partner that shares this work with me so I’m speaking from my experience 🙂 ).

This is part of what helps me maintain a good work/life balance. I’m also part of a group of people at work that encourages others to be more physically active. We promote stretching and meditation weekly sessions with the team. I think that even just talking about it at work and making people see our efforts and concerns reminds them of “hey – other people are feeling the same and they care!”. Thinking of our shared human connection can have a huge impact on our work/life balance.

Special thanks to TJay for hosting this month’s theme 🙂

Do you need SQL for a data related job?

Illustrations by Camila Henrique

I wanted to talk about this because I see a lot of doubts and lack of direction from people who are either beginning now in IT land or thinking about switching careers. The short answer to “do I need to know SQL for a data job?” , is yes. In the next few paragraphs I explain why I think so.

It’s easy to get caught up in all the fancy programming languages and methodologies for projects that sometimes the basics… are just not there. I believe having a good foundation opens paths to other doors that you could not see before. And I’m certain that SQL is one hell of a foundation to have in the data land.

Data jobs have been on a hype for the past few years. There’s a lot of speculating about what a data person job is (and actually it can vary a lot). However, there is one skill that seems to endlessly haunt job descriptions. Can you spot it below?

Taken from Linkedin, July 2021

Taken from Linkedin, July 2021

Taken from Linkedin, July 2021

SQL is always on high demand for any data job. Because it’s part of our basics. And knowing your basics can help you thrive. You can learn a lot of interesting stuff about data, but in the end, you’ll most likely need to get your hands on a database at some point, and SQL is how you talk to a database (and I think that’s beautiful. Stop judging me!).

I’m working on a series of posts dedicated to people who would like to learn SQL from zero. I’ll focus on the Microsoft product, MS SQL Server. They have a version of SQL just for it, it’s called T-SQL. I hope to share my knowledge with you and perhaps help someone along the way. This is my way of giving back to the community 🙂

Any questions or suggestions, my comments are always there for you.

If you want to check out this same post in Portuguese, click here.

Event invitation – Women Techmakers Montreal

Invitation to join online event: Women Techmakers Montreal!

Hello everyone! Today’s post is an invitation to the Women Techmakers Montreal event.

Women Techmakers is a program created by Google to celebrate International Women’s Day and to highlight the talent of women in technology. This program has been in over 200 global events and seen across 52 countries. The next event will be on Saturday, March 20, 2021.

Last year, I remember being really excited to go to the event in Montreal, when COVID hit and events all over were cancelled. Luckily, the organization switched to an online event instead. It was an amazing day! Full of such inspiring women, and such a beautiful community.

This year, I wanted to: 1- let more people know about the event, and 2- get people to join the event!

Who can join?

All genders are welcome! If you’re into tech, I’m positive you’ll find a topic that will get your attention. Sign up and enjoy (:

The event is totally free and lasts the whole day (from 9AM to 6PM EST). Check out their amazing agenda.

If you’re wondering about what happened last year, check this playlist with their sessions.

Find more info on their site.

I hope to see you there!

T-SQL Tuesday – My (least) favorite SQL data type: DATE

Hello! This post is a contribution to T-SQL Tuesday. T-SQL Tuesday is a monthly blogothon where we get together and write about a different topic. March’s topic was to blog about you favorite data type, hosted by Brent Ozar.

I wrote two versions of this post. This one you’re reading in English, and this one in Portuguese.

If you work with data, you probably do not have control over all the data sources you need. What I mean, is that for example, you may receive data from different places. Perhaps it’s your job to centralize and standardize it the best you can, so that it makes sense to your team. Once you understand the data, value can be extracted from it.

When your data comes from different systems, it’s likely you will not have control over the data’s validation. For example, one of you vendors may be very specific about the data types allowed into their systems, which means when the data gets to you, you’ll see something (ideally) more structured. However, your data may also be handled by a group of fed up developers who decided they will allow everything the user wants (special attention to the the verb there being want instead of need, big difference).

Date data types are really important and used in SQL. But for humans, dates can be formatted in some ways… For example, I’m from Brazil, and the way we write our dates is different from the US.

  • Brazil: DD/MM/YYYY
  • US: MM/DD/YYYY

In SQL, your date type stores data like this: YYYY-MM-DD. No room for mistakes, right? Bam, Wrong.

Remember when I said you may have different data sources and you can’t control their data type validations? Let’s think of the following example:

  • Your clients use a 3rd part system
  • The 3rd party uses the data to do whatever it is their system does
  • They send you that data with the results of your project
  • You, a smart data person, tries to load the data into your system. Most importantly, your system is formatted with the data types you expect to receive. So, for example, if you expect a field with a date value, you’ll format your table to have a date type column.
  • Let’s use the table “ThirdPartyInfo” as an example.

Now, as should have assumed by now, your third party did not applied any data validation to the date types. Hence, you may get some crazy “dates”, like this:

  • Jan/2021
  • 03-20-18
  • 01-02-03 (where to even begin with this one?!)
  • 2022/2
  • and many others….

Here’s what happens when you try to insert something that’s not a date, into any of the date columns:

It does not matter the method you’ll use to input data to your table, you’ll get an error if you’re not passing date values to your date types columns.

How can you avoid issues like this

  • Be open to your vendor about why this is important to your data, and explain to them your tables data type. How? Documenting, the thing IT people hate most.
  • If the vendor is pushing back, talk to your superiors, and show them a scenario in which you need to spend your precious (expensive) time to fix this mishap. Enforce this could be avoided if everyone were on the page about the data types for the data you share.
  • If nothing above works, or you need a temporary solution, you could validate the data on your end too. More work upfront, but your future self will thank you for putting this effort now.

Learning it the hard way

  • Real life example: I recently had an issue that cost me a lot of time. I had received a csv I needed for reporting, and the file had a few date fields. My table, was expecting date types to come in all the fields, but I was getting errors on my data loading job.
    • I had to take a step back and find where the issue was. I thought it would be easy to find, by checking the most recent info that got into the system (this was a daily file we received), and so I started looking for the issue. My main mistake, was that I did not isolate the date column that was giving me an error. In the SQL Server, the error message was really vague, I could only tell there was a string trying to be converted to date and I had several different date fields in my table.
    • My second big mistake was fixing everything I found on the source file, and I thought was wrong and causing the issues. Spoiler alert: it wasn’t.
    • It took me some time to realize that “ISDATE” was an easy way to search for a column that is expecting a date type, but received something else instead:
In desperate times, we may be blind by stress and not think about simple things, like this.

You see that the return to that query has the string value. Now, you could apply this kind of check as a validation before you load data into your production table, and also use it to troubleshoot issues like mine.

With all that being said, I actually like the DATE data type in SQL. It works great, the real issue is that we as humanity never agreed on a single date format. Sigh. I hope this helps!

If you want you can read more about the Date types here: Date types in SQL

Would you solve my problem in a different way? Tell me how below in the comments (:

Commenting and Naming practices with T-SQL scripts

Image: https://tinyurl.com/pt8ly0ix

Do you know that feeling when you’re starting another query and this time you’re thinking “I won’t re-use this… this is a one-time thing…”. We both know you’re lying.

I said that because most of the time:

  • you’re code is reusable
  • you’re human and you forget stuff, so perhaps tomorrow you forgot something you realized how to do today
  • I’ve never seen where you keep your scripts but I bet that needs a clean up

When you’re working on something, try to picture your future self. Your older self has probably no clue about what this *really_important_2018_script* project was. Is your older/wiser version happy while reading that? I don’t think so.

If you make an effort now to be more “tidy” with your code, here’s who will be happy:

  • your future self
  • your colleagues who read your code
  • people who will take your job in the future and think “hey! this person don’t suck as much as I thought… just a little!”

How to add comments in SQL

Basically, there are two ways to add comments in SQL:

1- add “–” and start typing!

2- add “/**/” and write in between the stars.

No one is better than the other. The first option is usually for in-line smaller comment. The second it’s more for commenting out blocks of code or writing bigger comments in a way they will not be on one same line.

Initial comments

Personally, I believe comments are a good thing. It’s not something I do with every line, however, when I open a new query, I automatically add my comments.

Here’s how I like to start my scripts:

Important note: if you’re writing a stored procedure, remember to add this block of comments after your “CREATE PROCEDURE” statement. When you do so, the comment will show up when you right-click on the stored procedure and chose “Modify”. Otherwise it’s gone and saved only on your main script file.

Naming your file

Try to be descriptive when naming your file, and don’t abbreviate too much. The name you chose may seem relevant now, but again, ask yourself “is my future version hating me now?”. If the answer is yes, than chose something else.

If the script you are creating is part of a bigger project that involves other scripts, then try to start every file name with something that refers to the project itself.

For example: sp_AutomatedFinancialReports_CalculatingEmployeeSalary

Here’s what we know based on the file name:

  • sp: this is a stored procedure
  • AutomatedFinancialReports: project name that’s descriptive
  • CalculatingEmployeeSalary: this is what your specific script will do as part of the project

Avoid at all costs using “new”, “old”, “final” and relative names on your file. This is not useful long-term. If you’re creating many drafts, I’d go with versions like “v1”, “v2”, or just add the plain date to your file name. But remember this is a temporary name, and that once you figure your “v23_final_new” file, you’ll change the name accordingly. Another trick is to move your thousand drafts to a, guess what, drafts folder. The idea is that your main project folder keeps your already tested and ready to go script.

This is some basic stuff I do everyday and I hope you find it useful. Do you add something else that you’d like to share? Leave a comment (:

Want to read this post in Portuguese? Check out this link.

See you later!