SQL – Changing to British Datetime format

Error converting data type varchar to datetime
Are you getting the above error?

Run the following under your app user.

select @@Language, @@Langid

If it says us_english then you’re probably hitting errors when parsing dates as DD/MM/YYYY.
We can change the default language for a particular user. Login to your SQL server as an administrator account and run the following;

ALTER LOGIN yourapplogin WITH DEFAULT_LANGUAGE = British;
GO

Then login with the user “yourapplogin” and run

select @@Language, @@Langid

Hopefully you now see “British” and 23.

Selecting where row value is null

Don’t ask me why, but I ALWAYS forget how to select results from an mSQL database where the value is null. It’s just weird.

I always try

select * from table where colname = null
or
select * from table where colname = ”

But the correct way is to select using IS NULL!

select * from table where colname IS NULL

MySQL error #1135: Can’t create a new thread (errno 11).

This error was the bane of my life for a while, and it was very hard to get a definitive answer as to what was causing it, I hope this saves you some trouble.

My website occasionally got large traffic spikes, and at the top of these peaks, I would start to see errors like these:

MySQL error #1135: Can’t create a new thread (errno 11). If you are not out of available memory, you can consult the manual for a possible OS-dependent bug.

I looked in the my.cnf file on the db server and looked at the open files limit, because a process is counted as an open file, but it seemed fine:

[mysqld_safe]
open-files-limit=10240

I also checked that maximum connections was high enough, it was at 2048.

What the open-files-limit in my.cnf files does is it tells the init script to use ulimit to whatever number you put in there.

After a lot of digging around various places, and much frustration, I discovered that by default linux has a hard limit of 1024 open files for all non super-users, so even though I had set a high open-files-limit, it was capped at 1024 by the OS. I also discovered how to raise it;

/etc/security/limits.conf

This file is used by PAM to set things like maximum processes, max open files, memory usage etc and these limits can be set on a per-user basis, so I added these lines:

mysql soft nofile 4096
mysql hard nofile 4096


and restarted MySQL. BAM! That did the trick.
One more thing to make sure of is that the su pam config file (/etc/pam.d/su) is using pam_limits.so, as that is the pam module which enforces limits.

Good luck!
-Jonesy

Download counter (Stored Procedure)

A stored procedure I used as a download script. You pass this procedure a parameter (filename), it checks the database if a record for this exists. If so it increments the record by one. If the record doesnt exist it creates it for you.

For this to work I used a table with two columns.

filename varchar(255)
hits numeric (9)


IF EXISTS (
	SELECT name FROM dbo.sysobjects
	WHERE name = 'updateStats'
	AND type = 'P'
	)DROP PROCEDURE dbo.[updateStats]
GO
	CREATE PROCEDURE updateStats
	(
		@filename varchar(25)
	)
AS
IF EXISTS(SELECT 'filename' FROM kav_dlstats WHERE filename = @filename)
BEGIN
	--If it exists, update the record!
	UPDATE kav_dlstats SET hits=hits+1 WHERE filename = @filename
END
ELSE
	BEGIN
	--Else lets make a new record
	INSERT into kav_dlstats(filename, hits) VALUES(@filename, '1')
END