I'm trying to get a date value with:
- Current year
- Current month
- Day number from one select
the query
SELECT rat_data FROM rate_unita WHERE rateid = 1
as a result one INT value.
When I try to execute the following:
SELECT DATE(CONCAT(YEAR(NOW())),(MONTH(NOW())), (SELECT rat_data FROM rate_unita WHERE rateid = 1))
Mysql mark my syntax as incorrect near ' ( MONTH( NOW( ) ) ) , ( SELECT rat_data FROM rate_unita WHERE r
I think that there is something wrong with the way I'm calling the SELECT in the CONCAT, what is the correct query to reach my goal?
Thanks
L
You're closing the concat function too early. Remove some extra parentheis
SELECT DATE(CONCAT(YEAR(NOW()),
MONTH(NOW()),
(SELECT rat_data FROM rate_unita WHERE rateid = 1)
))
But you need to add some hyphens to make it a true date value
SELECT DATE(
CONCAT(
YEAR(NOW()),
'-',
MONTH(NOW()),
'-',
(SELECT rat_data FROM rate_unita WHERE rateid = 1)
)
)
This should result in a date of YEAR-MONTH-rat_data
Here's a working SQL Fiddle
Use DATE_FORMAT function. Here are a few examples:
http://www.w3schools.com/sql/func_date_format.asp
http://www.mysqltutorial.org/mysql-date_format/
Related
The below mentioned query is returning null but my inner query is fetching values like
SELECT CONCAT(
FLOOR(HOUR(time_milis) / 24), ' ',
MOD(HOUR(time_milis), 24), ':',
MINUTE(time_milis),':',
SECOND(time_milis))
FROM (
SELECT SUM(TIMEDIFF(logout_timestamp,login_timestamp)) AS time_milis FROM t_access_log WHERE logout_timestamp!='0000-00-00 00:00:00' GROUP BY login_id
)se
what am i missing here
Why not using DATE_FORMAT() for this? So my guess would be something like:
SELECT login_id ,
DATE_FORMAT(
SUM(
TIMEDIFF(
logout_timestamp,login_timestamp)
)
) as some_time
FROM t_access_log
WHERE logout_timestamp!='0000-00-00 00:00:00'
GROUP BY login_id;
Your time_milis are all seconds, but hour need a time like '10:05:03', you can just use sec_to_time. Like this:
SELECT concat(hour(sec_to_time(time_milis)), ' ', sec_to_time(time_milis))
FROM (
SELECT SUM(TIMEDIFF(logout_timestamp,login_timestamp)) AS time_milis FROM t_access_log WHERE logout_timestamp!='0000-00-00 00:00:00' GROUP BY login_id
)se
I am getting output in different format, here is my query:
SELECT
COUNT(lead_id) AS `leads`,
Month(FROM_UNIXTIME(`created_at`)) AS `Month`
FROM `lead`
WHERE YEAR(FROM_UNIXTIME(`created_at`)) = YEAR(CURDATE())
GROUP BY Month(FROM_UNIXTIME(`created_at`));
Output:
Month
312c31
322c31
332c31
342c31
352c31
362c31
372c31
382c31
392c31
31302c31
31312c31
31322c31
Required Output:
month
1,1
2,23
3,4
5,6
6,34
7,76
8,2
9,3
10,5
11,4
12,1
You can try this:
SELECT
CAST(
CONCAT(
MONTH(FROM_UNIXTIME(`created_at`)),
',',
COUNT(lead_ID)
) AS CHAR
) AS 'Month'
FROM lead
WHERE `created_at` BETWEEN UNIX_TIMESTAMP(DATE_FORMAT(NOW(), "%Y-01-01")) AND UNIX_TIMESTAMP(DATE_FORMAT(NOW(), "%Y-12-31"))
GROUP BY MONTH(FROM_UNIXTIME(`created_at`))
i got an answer thankyou for all you replies ur replies also an usefull information here is my query
SELECT
*,
concat(leads, ",", month)as leadss
FROM (
SELECT
COUNT(lead_id) as 'leads,
Month(FROM_UNIXTIME(`created_at`)) AS `Month`
FROM `lead`
WHERE YEAR(FROM_UNIXTIME(`created_at`)) = YEAR(CURDATE())
GROUP BY Month(FROM_UNIXTIME(`created_at`))
)AS ab
I am working ona small project for a client, and the data in the database is causing me a bit of bother. I am completing a search form for paid installments from the database. I can get the data fine, its the date fields which are giving me bother.
There are separate fields for the date, month and year for each installments. Is there a way I can use CONCAT or DATE or maybe even use CAST on the combined fields to search for the data.
I have tried the following
SELECT DATE_FORMAT( CONCAT( received_date, received_month, received_year ) , '%d/%m/%Y' ) FROM policy_installments
AND
SELECT DATE_FORMAT(STR_TO_DATE(CONCAT('pit.received_date', '-', 'pit.received_month','-', 'pit.received_year'), '%d-%m-%Y') AS mydate, '%d-%m-%Y') AS modate FROM policy_installments pit ORDER BY mydate
AND
SELECT * FROM policy_installments WHERE CONCAT(CAST(received_date AS CHAR),"-",CAST(received_month AS CHAR),"-",CAST(received_year AS CHAR)) >= '1-1-2014' AND CONCAT(CAST(received_date AS CHAR),"-",CAST(received_month AS CHAR),"-",CAST(received_year AS CHAR)) <= '31-3-2014'
When I use the following I get some unexpected data:
SELECT CONCAT_WS( received_date, "-", received_month, "-", received_year ) FROM policy_installments
RESULT
2d313130312d3132303039
2d3237313032372d323732303039 etc etc
Each of the 3 fields are type INT(11).
However, none of these give me the correct data. Is there a way i can convert all 3 fields and use a date to pass into the query to enable me to correctly grab the correct data?
Any help would be much appreciated!
To convert a concatenated string to date, string should be in the same format as what you are using to convert.
For a date-string in dd/mm/yyyy format, should be converted to sql date using %d/%m%Y format.
Example:
SELECT STR_TO_DATE(
CONCAT( received_date, '/', received_month, '/', received_year ),
'%d/%m/%Y'
) as rcvd_date
FROM policy_installments
And to compare with other dates, you can use BETWEEN clause.
Example:
SELECT * FROM policy_installments
WHERE
STR_TO_DATE(
CONCAT( received_date, '-', received_month, '-', received_year ),
'%d-%m-%Y'
) BETWEEN STR_TO_DATE( '1-1-2014', '%d-%m-%Y' )
AND STR_TO_DATE( '31-3-2014', '%d-%m-%Y' )
Refer to:
MySQL: STR_TO_DATE(str,format): Convert a string to a date
Generally, you want to avoid performing the function on the column. Instead, perform the function on the literal, so that you can utilize indexes on the columns:
SELECT *
FROM policy_installments
WHERE received_year = YEAR('2014-03-31')
AND received_month = MONTH('2014-03-31')
AND received_date = DAY('2014-03-31')
Add a multi-column index on received_year, received_month, and received_date, you'll be doing pretty good.
Obviously, this method will only work for = comparisons. For <=, it becomes a bit more complicated:
SELECT *
FROM policy_installments
WHERE
received_year < YEAR('2014-03-31')
UNION
SELECT *
FROM policy_installments
WHERE
received_year = YEAR('2014-03-31')
AND received_month < MONTH('2014-03-31')
UNION
SELECT *
FROM policy_installments
WHERE
received_year = YEAR('2014-03-31')
AND received_month = MONTH('2014-03-31')
AND received_date <= DAY('2014-03-31')
But this longer query is still able to utilize the index.
To simplify things, consider creating an actual date column and keep it updated with the proper value.
I want to select last information about client's balance from MySQL's database. I wrote next script:
SELECT *
FROM
(SELECT
contract_balance.cid,
/*contract_balance.yy,
contract_balance.mm,*/
contract_balance.expenses,
contract_balance.revenues,
contract_balance.expenses + contract_balance.revenues AS total,
(CAST(CAST(CONCAT(contract_balance.yy,'-',contract_balance.mm,'-01')AS CHAR) AS DATE)) AS dt
FROM contract_balance
/*WHERE
CAST(CAST(CONCAT(contract_balance.yy,'-',contract_balance.mm,'-01')AS CHAR) AS DATE) < '2013-11-01'
LIMIT 100*/
) AS tmp
WHERE tmp.dt = (
SELECT MAX(b.dt)
FROM tmp AS b
WHERE tmp.cid = b.cid
)
But server return:
Table 'clientsdatabase.tmp' doesn't exist
How to change this code for get required data?
Try this one in your subquery you are trying to get the MAX of (CAST(CAST(CONCAT(contract_balance.yy,'-',contract_balance.mm,'-01')AS CHAR) AS DATE)) AS dt but in subquery your aliased table tmp doesn't exist so the simplest way you can do is to calculate the MAX of dt and use GROUP BY contract_balance.cid contractor id ,i guess it will fullfill your needs
SELECT
contract_balance.cid,
contract_balance.expenses,
contract_balance.revenues,
contract_balance.expenses + contract_balance.revenues AS total,
MAX((CAST(CAST(CONCAT(contract_balance.yy,'-',contract_balance.mm,'-01')AS CHAR) AS DATE))) AS dt
FROM contract_balance
GROUP BY contract_balance.cid
Try this:
SELECT *
FROM (SELECT cb.cid, cb.expenses, cb.revenues, cb.expenses + cb.revenues AS total,
(CAST(CAST(CONCAT(cb.yy,'-',cb.mm,'-01')AS CHAR) AS DATE)) AS dt
FROM contract_balance cb ORDER BY dt DESC
) AS A
GROUP BY A.cid
The following query is working for me and returns the data in the format '1755|1755.jpg' :
SELECT (CAST(GROUP_CONCAT(CONCAT(photoId, '|', photoFileName)) AS CHAR(10000) CHARACTER SET utf8)) AS recentPhotoList
FROM
(
SELECT photoId, photoFileName
FROM photo
WHERE photoAlbumId = _albumId
AND photoPublishDate >= DATE_ADD(tStartDate, INTERVAL -20 MINUTE)
AND photoPublishDate <= tStartDate
ORDER BY photoPublishDate DESC
LIMIT 0,1
) as subQuery
);
However, by simply appending another column ('photoCaption') to the query, it then returns nothing?!? Why is this? Why is adding an additional column causing it to do this?
Here is the modified query that causes issues:
SELECT (CAST(GROUP_CONCAT(CONCAT(photoId, '|', photoFileName, '|', photoCaption)) AS CHAR(10000) CHARACTER SET utf8)) AS recentPhotoList
FROM
(
SELECT photoId, photoFileName, photoCaption
FROM photo
WHERE photoAlbumId = _albumId
AND photoPublishDate >= DATE_ADD(tStartDate, INTERVAL -20 MINUTE)
AND photoPublishDate <= tStartDate
ORDER BY photoPublishDate DESC
LIMIT 0,1
) as subQuery
);
I think that if photoCaption is null then the concat returns null. Use coalesce around photoCaption to solve this.
photoCaption is NULL i think.
Maybe you can check the results of your subquery to see what is going on.
SELECT CONCAT('bla', '|','-' , '|','|', 'bla', NULL) as foobar;
returns NULL
SELECT CONCAT('bla', '|','-' , '|','|', 'bla') as foobar;
returns bla|-||bla
Good luck