Can you GROUP BY with a CASE WHEN THEN alias name?

You need to use the whole CASE statement in the GROUP BY clause if you don’t wrapped it in a subquery. SELECT CASE WHEN DATEDIFF(o.EndDate, o.StartDate) < 30 THEN ‘<1 Month’ WHEN DATEDIFF(o.EndDate, o.StartDate) < 90 THEN ‘1 – 2 Months’ WHEN DATEDIFF(o.EndDate, o.StartDate) < 210 THEN ‘3 – 4 Months’ ELSE ‘>4 Months’ END … Read more

Extract numbers from a field in PostgreSQL

Simply: SELECT NULLIF(regexp_replace(po_number, ‘\D’,”,’g’), ”)::numeric AS result FROM tbl; \D being the class shorthand for “not a digit”. And you need the 4th parameter ‘g’ (for “globally”) to replace all occurrences. Details in the manual. For a known, limited set of characters to replace, plain string manipulation functions like replace() or translate() are substantially cheaper. … Read more

Case-insensitive PowerShell replacement

Call me pedantic but while nobody here was outright wrong, nobody provided the correct code for the final solution either. You need to change this line: $NullSessionPipes = $NullSessionPipes.replace(“browser”, “”) to this: $NullSessionPipes = $NullSessionPipes -ireplace [regex]::Escape(“browser”), “” The strange [regex] text isn’t strictly necessary as long as there are no regular expression characters (ex. … Read more

Using CASE in PostgreSQL to affect multiple columns at once

1. Standard-SQL: LEFT JOIN a single row of values You could LEFT JOIN a row of values using the condition (thereby evaluating it once). Then you can add fallback values per column with COALESCE(). This syntax variant is shorter and slightly faster with multiple values – especially interesting for an expensive / lengthy condition: SELECT … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)