Tag Archives: sql

Order SQL query manually when numeric string representation is out of order

In some instances, doing a straight ORDER BY ASC (OR DESC) does not work as one might want: 0-50 101-200 201-300 301-400 401-500 501-600 51-100 601+ That is not how humans do it 😉 A way to get around this, is to add a new column and put in an ordering value, for instance: 1. 0-50 2. 51-100 3. 101-200 4. 201-300 5. 301-400 6. 401-500 7. 501-600 8. 601+ Now that is much better 🙂 Here is a real

SQL Server: list all user defined functions in a database

Recently I inherited a database project where many of the functions were not listed nor documented. Thankfully, with the help of the always useful stackoverflow, I was able to find the following snippet for displaying all user functions: select name, definition, type_desc FROM sys.sql_modules m INNER JOIN sys.objects o ON m.object_id=o.object_id where type_desc like ‘%function%’ via sql server – SQL list of all the user defined functions in a database – Stack Overflow. Very cool! 🙂

Format SQL code

I have been writing copious quantities of SQL lately. Some of the queries are getting really long. When I am working with Navicat it has a nice ‘SQL beautify’ feature that takes messy SQL and makes it nice and tidy. 🙂 While that features is great when using Navicat for SQL editing, sometimes I need to workdirectly with SQL Server 2008 there is no comparable feature built into Microsoft’s SQL pane. 🙁 Thankfully, with a quick copy/paste there is an online tool

Set your own date format in MySQL

Use DATE_FORMAT in MySQL to create your date strings formatted however you like: SELECT CONCAT(YEAR(res_hotel_booking_bookings.created),’-‘,DATE_FORMAT(res_hotel_booking_bookings.created,’%m’) ,’-‘,DATE_FORMAT(res_hotel_booking_bookings.created,’%d’)) AS creationDate, Count(res_hotel_booking_bookings.id) As reservationsMade FROM res_hotel_booking_bookings WHERE res_hotel_booking_bookings.created >= ‘2013-10-15 00:00:00’ AND res_hotel_booking_bookings.created <= ‘2013-10-17 24:00:00’ GROUP BY YEAR(res_hotel_booking_bookings.created), MONTH(res_hotel_booking_bookings.created), DAY(res_hotel_booking_bookings.created) ORDER BY creationDate DESC There are more concise ways to do this, but this was adapted from an earlier method that used CONCAT so I stuck with that… 🙂

How to fix Customized Recent Comments duplicate comments

Customized Recent Comments is a great plugin for displaying recent comments on your WordPress blog. It has all the features I need and requires minimal install and setup. With most ‘recent comments’ plugins/widgets, if you (the blog owner) respond to a bunch of comments at once, you end up having your comments take up the entire recent comments listing 🙁 Thankfully, one of my favorite features of the plugin is its ability to exclude particular commenters from being listed. 🙂 However, the only issue I have