Selecting a Point datatype from MYSQL - mysql

Good Day,
I am trying to get a point from an SQL column. CUrrently it is encoded as a string of characters: Gþ`C#Ëóàî¬Ë]À
So is there any way to actually decode it to get the actual value?

Use MySQL's AsText() function:
SELECT AsText(myPoint) FROM myTable

Related

questions about the datetime type values

I have several things which I want to discuss with you guys.
Since, they were just simple questions, so no dataset here.
Suppose I have a datetime type column which called started_date, the value in it was like:
yyyy-mm-dd hh:mm:ss. So, if I want to select some IDs which were larger than one specified day (let's say June/01/2017), can I just using
select ID, started_date
from table1
where started_date>"2017-06-01";
Does this work?
I tried some samples, and it worked indeed in the mysql. However, someone told me that I cannot compare the datetime column with string values without converting their format. And it confused me. Because I thought the value "2017-06-01" here was date type value, so it does not need convert. Or am I thinking wrong?
Another thing was about the double quote and single quote, I understand that the single quote was used for string values. However, in this case, when I used double quote to quote "2017-06-01", it works. So, does it mean the double quote can quote date values?
I am just asking, so any response is welcome.
Thanks.
Your query is fine. You are using a safe date/time format for the string. In other words, if you have to store the value as a string, then use that format.
I would write the code as:
where started_date >= '2017-06-01'
I see no reason to exclude midnight on 2017-06-01 (although you might have a reason). Second, single quotes are the standard delimiter for strings.
That said, you can store the value as a string.
As a best practice, I stay away from comparing time-stamps to date-stamps. In this case you can be explicit and truncate the start date. And yes, use single quotes instead.
where SUBSTR(started_date, 1, 10) > '2017-06-01'
To make sure it works you could just convert the date time to a string first and compare the two strings:
to_char(started_date,'YYYY-MM-DD') >= '2017-06-01'
The Strings will compare just fine in that format.

Parse a string into a date in SQL

I have a string field with the format Jan/17 - is there a way to convert this to a date? Using a cast fails the conversion. All 12 months are in a 3 character format.
SELECT TRY_PARSE('Jan/17' AS DATETIME USING 'en-us')
even the simpler one would work:
SELECT TRY_PARSE('Mar/28' AS DATETIME)
Note that this function relies on the presence of the .NET Framework Common Language Runtime (CLR). So Gordon's solution is better in case you want a SQL-only way of doing it.
This appears to work:
select cast('01/' + col as date)

How do I convert binary data from MySQL to a string in ColdFusion 11?

I have a field in my MySQL database that is set to data type "BIT". The value in the field is 101101. I am trying to read this value using Coldfusion (version 11)
I simply use the following code:
<cfquery name=q1 datasource=#data_source#>
select * from mytable
</cfquery>
<cfoutput>
#q1.mybitfield#
</cfoutput>
I have tried using CAST and CONVERT on the MySQL side and I have tried CharSetEncodeing on the CF side along with every option of ToString, ToBase64, and ToBinary that I can think of.
I still can not get my output to look like it does in the database.
Actually, I was probably thinking of SQL Server's bit type. For MySQL, a simpler option might be to use the bin() function which:
Returns a string representation of the binary value of N, ...This is equivalent to CONV(N,10,2). Returns
NULL if N is NULL.
For example:
SELECT bin(YourBitColumn) AS YourBitColumn FROM YourTable
... or
SELECT bin(YourBitColumn+0) AS YourBitColumn FROM YourTable
NB: High-order 0 bits are not displayed in the converted value. That applies the CF function as well.
Thanks Leigh for your help. I was never able to get it to work with just one step so I used a two step solution (if you want to call it that). What I ended up doing was setting up my MySQL statement to convert the field to an Unsigned interger (although Decimal or a signed integer would have also worked), then once I read in the decimal value I was able to convert it to binary using your suggestion of FormatBaseN(q1.myfield,2). So thanks for the reminder of FormatBaseN. I had forgotten about that one.
My final code ended up looking like this:
The MySQL statement:
SELECT *, CONVERT(item , UNSIGNED) as di from mytable
And the Coldfusion looked like this:
<cfset d = FormatBaseN(q1.di, 2)>
Edit
After writing this, I decided to go with Leigh's answer above since it was a better solution.

How can I order a mySQL column by a number imbedded in a string?

I am parsing genomic positions from a MySQL field. The field is called "change" and the entries are of the form:
g.100214985T>C
g.100249769C>A
g.10185G>T
I am trying to order the field by the numerical portion of the string. I am trying to figure out what mySQL query I can use to accomplish this. I have tried using REGEXPs and SUBSTRING_INDEX but am still running into issues. Any help would be much appreciated!
Assuming you have always 2 characters in front of and 3 at the end you need to have removed:
SELECT CAST(SUBSTR(col from 3) AS UNSIGNED) AS value
FROM `my_table`
ORDER BY value
Watch this sql fiddle also: http://sqlfiddle.com/#!2/7bc0e/67
Thank you #MarcusAdams and #amoudhgz! The following code works:
CAST(SUBSTR(field, 3) AS UNSIGNED).
MySQL already stops the conversion at the first non-numerical character.

MYSQL Select Rows after specific Date (row type is varchar)

I'm trying to select all columns after a specific date but the trick is the "date" column is varchar
I have this
SELECT * FROM `users` WHERE STR_TO_DATE(birthday,'%m/%d/%Y') > '10-10-2000'
as an example and I want to select all users whos birthday is after the 10/10/2000 but this just returns all rows.
Anybody got a clue what's wrong?
You are comparing a data and a string. I would use CAST:
WHERE STR_TO_DATE(birthday,'%m/%d/%Y') > CAST('2000-10-10' AS date)
Also note that I changed the format of the second to match what mysql expects. You could also use STR_TO_DATE here.
I think mysql dates are usually yyyy-mm-dd which means you should change the string at the end to 2000-10-10.
Try that and let me know if that works.
We need to know what format the string birthday is in, now you're saying it is e.g. 12/31/2001. Is that right?
If you have multiple strings, consider using the same STR_TO_DATE-formatting:
WHERE STR_TO_DATE(birthday, '%m/%/d/%Y') BETWEEN STR_TO_DATE(some_date, '%m/%/d/%Y')
AND STR_TO_DATE(another_date, '%m/%/d/%Y')
More info:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date