I have a field name "date" datatype "char" and value in thid field is "04-08-2011 04:47:08 EDT"
but when i want to fetch the records from our data base basis on date like
select * from table_name where date="04-08-2011 04:47:08 EDT"
but i dont get any result for this query,any one can help me
Try using the LIKE statement, because if your field has X chars and you insert X-3 chars, the 3 left chars are filled with spaces, so modify your query to be like:
select * from table_name where date LIKE "04-08-2011 04:47:08 EDT%"
EDIT: date is a field type in MySQL, so this query isn't valid. If your field is 'date', you need to put it between ``, so the query will be like this:
select * from table_name where `date`="04-08-2011 04:47:08 EDT"
If you're using the datatype char then any characters that are under the set length of the column will be right padded with spaces.
e.g.
char(30) would pad like so:
04-08-2011 04:47:08 EDT <-- spaces up to arrow
Use a date datatype or varchar (if you must).
In response to comment:
Have you run this query from phpMyAdmin? If so, does it return any rows, if not, try.
Related
I have a table called "products" where one column is "SerialNumber". The problem is that this column is text latin1_swedish_ci and values are like "BC00001, BC0002, etc. i need to select all products where serial number is for example greater than some given serial number.
i tried selecting like this:
SELECT * FROM 'products' WHERE 'SerialNumber' >= 'BC00563'
But since this column is text, it' not working.
I can't modify the table structure because the this table laready has 20.000 + records.
Any help appreciated!
Assuming the value is always two characters followed by numbers, you can strip out the characters and convert the remaining values into number for the comparison, e.g.:
SELECT *
FROM `products`
WHERE `SerialNumber` >=
CASE WHEN 'BC00563' regexp '[A-Z]' THEN
CONVERT(SUBSTRING("BC00563", 3, LENGTH("BC00563")), UNSIGNED)
ELSE 'BC00563' END;
You can use the column name instead of constant value.
So I have a column in mysql db that has values such as '34343|dollar' and '2343|dollar'. I wish to get all the column values, then remove the '|dollar' string and get the average of the numbers. How can I achieve this?
Something like this:
select AVG(CONVERT(REPLACE(col,'|dollar',''), SIGNED INTEGER)) from my_table;
'col' is your column that has the values as you mentioned.
I have database in MYSQL with ID, Req_Order_No(Varchar) , Req_In_Time(DateTime), Req_Out_Time(DateTime)
The Sample row is like below:
1 W0CH546 2014-07-23 09:32:00 2014-07-24 01:42:00
The above Date and Time are in EST format. I want to convert both of them and store in IST format in other columns
I tried SELECT CONVERT_TZ('Req_In_Time','-05:00','+9:30');
But it returns NULL Values.
Please help. Do I need php also?
The quotes around Req_In_Time cause the error.
SELECT CONVERT_TZ(Req_In_Time,'-05:00','+9:30');
Also, you should never store time information in localtime.
Use UTC/GMT.
You can always convert it to the proper localtime when you display it.
Note: Of course you need to specify the table-name as well:
SELECT CONVERT_TZ(Req_In_Time,'-05:00','+9:30') FROM YOUR_TABLE_NAME;
So you add another column (e.g. column xxx) to YOUR_TABLE_NAME.
Then you update the values.
UPDATE YOUR_TABLE_NAME
SET xxx = CONVERT_TZ(Req_In_Time,'-05:00','+9:30')
BTW, to add the column:
ALTER TABLE YOUR_TABLE_NAME ADD COLUMN `xxx` datetime NULL ;
i have table and column id, and the values are 1, 2 ,3,...,10,11,12,13.
how to query get max id from varchar type ? i had try
select MAX(id) from table
but the result is 9, Please help ??
Looks like the values are strings and it selecting the maximum string. You have to cast them to numbers first if you want them to sort numerically. You can use CONVERT to do this:
SELECT MAX(CONVERT(id, SIGNED)) FROM table
You can also use CAST:
SELECT MAX(CAST(id AS SIGNED)) FROM table
They do nearly the same thing except CONVERT has some additional options if you need them.
You can cast the varchar to an integer - something like
SELECT MAX(CONVERT(id, SIGNED)) FROM table
http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html#function_convert
SELECT MAX(id+0) FROM table will do the trick
I have a date field on my Sql table that is actually a text field, thats because there sometimes I save dates like 10/28/2011 and sometimes strings like present.
It is possible without touching the table structure, and maybe just with the sql query having the result correct organanized by date? Where present is the max value and then the dates in decreasing order.
If you really can't sort the data type out on those string date columns then this might help:
select t.*,
case when date_end = 'present' then curdate()
else convert(concat(substr(date_end,7,4),'-',substr(date_end,1,2),'-' ,substr(date_end,4,2)),date) end as "realDate"
from myTable t
order by "realDate" desc;
Right. I'm off to go and say 100 Hail Marys to the MySQL database god now. Ugh.
Best way would be :
fix the db and add proper date fields (add proper date or datetime field and update values from text fields and format them to be dates on the fly)
keep your present status in a text field and you can use that as a extra condition
I know you asked for a way to sort it correctly as a text column, but I really don't think that's the right way to this.
I agree with #stivlo. It is very easy to update the database to deal with this is a better way. You can either:
Convert the column to a real date, and use NULL to represent "present" instead of the string "present".
Or, if you need NULL reserved for some other meaning, you can convert the column to a real date, and add additional boolean flag columns to specify when the dates are "present".
Either way, you should definitely convert those text fields to real dates.
select job_desc, data_begin, data_end from table where data_end = 'present'
union
select job_desc, data_begin, cast(data_end as date) data_end from table where data_end <> 'present' order by data_end desc
Not sure if syntax is completely correct, so you might want to check the mySql 'union' syntax
Another solution might be using coalesce for 'present':
select cast(coalesce(data_end,now()) as date) data_end from table order by data_end desc
this way the column gets interpreted as dates and 'present' gets replaced with the present date