Showing entries with tag "MySQL".

Found 3 entries

MySQL: Using if statements for data conversion in a SELECT

I have an integer field in my database that I'd like to display as Yes/No in a report. You can do this conversion in your front end code, or you can use a MySQL if statement to do the conversion for you.

SELECT IF(CustPrimary = 1,'Yes','No') AS CustPrimary FROM Customer;
Leave A Reply

MySQL unix times

When dealing with dates and MySQL often you have to work data that's in unixtime. If you want to insert data into a MySQL table with a datetime field you can do this:

INSERT INTO MyTable VALUES (MyDateField) (FROM_UNIXTIME(1285695703));

If you have data in MySQL as a datetime and you want to extract it as unixtime do this:

SELECT UNIX_TIMESTAMP(MyDateField) AS MyUnixField FROM MyTable;
Leave A Reply

Random MySQL Dates

Here is a good way to generate a random date that you can use in MySQL.

SELECT FROM_UNIXTIME(RAND() * 2147483648) AS MyDate;

or

INSERT INTO TableName (DateField) VALUES (FROM_UNIXTIME(RAND() * 2147483648));
Leave A Reply