in one of the table the date column values are like below
date
25052008112228
26052008062717
table name is transaction
i tried using the below query but its throwing error
select * from transaction where date between '2012-01-06' and '2012-06-30'
select * from transaction where date between '2012/01/06' and '2012/06/30'
give me a solution.
The problem is that the [date] column doesn't contain a date in a format that will be automatically converted to an appropriate datetime value - it doesn't even contain a supported format value. So you're left shredding the text using string operations:
declare #Transactions table (TDate char(14))
insert into #Transactions (TDate) values
('25052008112228'),
('26052008062717')
select CONVERT(datetime,
SUBSTRING(TDate,5,4) + '-' +
SUBSTRING(TDate,3,2) + '-' +
SUBSTRING(TDate,1,2) + 'T' +
SUBSTRING(TDate,9,2) + ':' +
SUBSTRING(TDate,11,2) + ':' +
SUBSTRING(TDate,13,2))
from
#Transactions
Results:
2008-05-25 11:22:28.000
2008-05-26 06:27:17.000
You could wrap the CONVERT/SUBSTRING operations into a UDF, if you need to perform this kind of conversion often. Of course, ideal would be to change the column definition to store a genuine datetime value - almost all datetime issues arise when people treat them as text.
(Note, I've renamed both the table and the column, since using reserved words is usually a bad idea)
Your query could be something like:
;with converted as (
select *,CONVERT(datetime,
SUBSTRING([Date],5,4) + '-' +
SUBSTRING([Date],3,2) + '-' +
SUBSTRING([Date],1,2) + 'T' +
SUBSTRING([Date],9,2) + ':' +
SUBSTRING([Date],11,2) + ':' +
SUBSTRING([Date],13,2)) as GenuineDate
from [Transaction]
)
select * from converted where GenuineDate between '20120106' and '20120630'
(Note that I've also changed the date literals in the final query to a safe format also)
-- asp time stamp
select * from [transaction] where
cast(SUBSTRING([date],5,4) + '-' + SUBSTRING([date],3,2) + '-' +
SUBSTRING([date],1,2) + ' ' + SUBSTRING([date],9,2) +
':' + SUBSTRING([date],11,2) + ':' +
SUBSTRING([date],13,2) as datetime)
between '2008-05-26' and '2012-01-06'
-- unix epoch time
select * from [transaction] where [date]
between DATEDIFF( SECOND, '01-01-1970 00:00:00', '2012-01-06' )
and DATEDIFF( SECOND, '01-01-1970 00:00:00', '2012-06-30')
Related
I have this query:
SELECT name, SUM(count_1 + count_2 + count_3 + count_4 + count_5 + count_6) AS Total
FROM my_table
Is there a way to add these values count_1 + count_2 + count_3 + count_4 + count_5 + count_6 and so on.. more efficiently? MySQL keeps crashing for me when I add huge numbers of fields.
Regardless of whether the db design is right or wrong if you use an aggregation function you should use group by
SELECT name, SUM(count_1 + count_2 + count_3 + count_4 + count_5 + count_6) AS Total
FROM my_table
GROUP BY name
I have a table with fields: country_code, short_name, currency_unit, a2010, a2011, a2012, a2013, a2014, a2015. a2010-a2015 fields are type of double.
How do I make a query which orders the results by average of fields a2010-a2015, keeping in mind that these fields might have NULL value?
I tried this code and it did not work (returns a mistake, which tells there is something wrong in ORDER BY part. mistake was saying something about coumn names and GROUP BY). The logic is: ORDER BY ((A)/(B)) where A - sum of not NULL fields and B - count of not NULL fields.
Any ideas?
(if important, the code is going to be used in BigInsights environment)
SELECT country_code, short_name, currency_unit, a2010, a2011, a2012,
a2013, a2014, a2015
FROM my_schema.my_table
WHERE Indicator_Code = 'SE.PRM.TENR'
ORDER BY
(
(
Coalesce(a2010,0) + Coalesce(a2011,0) + Coalesce(a2012,0)
+Coalesce(a2013,0) + Coalesce(a2014,0) + Coalesce(a2015,0)
)
/
(
COUNT(Coalesce(a2010)) + COUNT(Coalesce(a2011)) + COUNT(Coalesce(a2012))
+ COUNT(Coalesce(a2013)) + COUNT(Coalesce(a2014)) +
COUNT(Coalesce(a2015))
)
) DESC;
use MySQL ifnull
IFNULL(expression_1,expression_2)
in your query :-
IFNULL(
(
COUNT(Coalesce(a2010)) + COUNT(Coalesce(a2011)) + COUNT(Coalesce(a2012))
+ COUNT(Coalesce(a2013)) + COUNT(Coalesce(a2014)) +
COUNT(Coalesce(a2015))
),
1
)
I had string in the order of ddmmyy i.e '231013' which i need to convert it into dd-mm-yy format
I tried following but it is giving yyyy-dd-mm format
I know 105 will give dd-mm-yy format
DECLARE #ITEMS AS NVARCHAR(10)='231013'
SELECT CONVERT(DATE,#ITEMS,105)
but it is returning
2023-10-13
How to convert string in the order of ddmmyy to date dd-mm-yy
Convert string in the order of ddmmyy to date dd-mm-yy
DECLARE #ITEMS AS NVARCHAR(10)='231013'
SELECT CONVERT(varchar(25),
CONVERT(DATE, SUBSTRING(#ITEMS, 1, 2) + '-' + SUBSTRING(#ITEMS, 3, 2) + '-' + SUBSTRING(#ITEMS, 5, 2), 5),5)
result will be
It seems that the mmddyy style is not natively supported by CONVERT; to perform the conversion, I would just adapt the input string to one of the supported styles, like mm-dd-yy:
SELECT
CONVERT(DATE, SUBSTRING(#ITEMS, 1, 2) + '-' + SUBSTRING(#ITEMS, 3, 2) + '-' +
SUBSTRING(#ITEMS, 5, 2), 5)
If you just need to change the format (Not converting to a DateTime type), you could Stuff() it:
Declare #Items as nvarchar(10)='231013' --ddmmyy
Select Stuff(Stuff(#Items,3,0,'-'),6,0,'-') --dd-mm-yy
Fiddle demo
I have a column in one of my tables, which is TIME format (00:00:00). I am trying to sum the entire column and display it as same (00:00:00).
I have tried using the following but it is not giving me anywhere near the correct answer.It's giving me 22.12:44:00 and manual calcaulation tells me it should be close to 212:something:something
SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( vluchttijd ) ) ) AS totaltime FROM tbl_vluchtgegevens
Any recommendations?
You can try like this:-
SELECT SEC_TO_TIME(SUM(SECOND(vluchttijd ))) AS totaltime FROM tbl_vluchtgegevens;
or try this(althoug this is not a good approach):
SELECT concat(floor(SUM( TIME_TO_SEC( `vluchttijd ` ))/3600),":",floor(SUM( TIME_TO_SEC( `vluchttijd ` ))/60)%60,":",SUM( TIME_TO_SEC( `vluchttijd ` ))%60) AS total_time
FROM tbl_vluchtgegevens;
Edit:-
Try this:-
select cast(sum(datediff(second,0,dt))/3600 as varchar(12)) + ':' +
right('0' + cast(sum(datediff(second,0,dt))/60%60 as varchar(2)),2) +
':' + right('0' + cast(sum(datediff(second,0,dt))%60 as varchar(2)),2)
from TestTable
Working SQL Fidlle
In MySQL, the TIME type is rather limited in range. Moreover many time function do not accept values greater that 23:59:59, making it really usable only to represent the time of the day.
Given your needs, your best bet is probably to write a custom function that will mimic SEC_TO_TIME but allowing much greater range:
CREATE FUNCTION SEC_TO_BIGTIME(sec INT)
RETURNS CHAR(10) DETERMINISTIC
BEGIN
SET #h = sec DIV 3600;
SET #m = sec DIV 60 MOD 60;
SET #s = sec MOD 60;
RETURN CONCAT(
LPAD(#h, 4, '0'),
':',
LPAD(#m, 2, '0'),
':',
LPAD(#s, 2, '0')
);
END;
And here is how to use it:
create table tbl (dt time);
insert tbl values
('09:00:00'), ('01:00:00'), ('07:50:15'), ('12:00:00'),
('08:30:00'), ('00:45:00'), ('12:10:30');
select SEC_TO_BIGTIME(sum(time_to_sec(dt))) from tbl;
Producing:
+--------------------------------------+
| SEC_TO_BIGTIME(SUM(TIME_TO_SEC(DT))) |
+--------------------------------------+
| 0051:15:45 |
+--------------------------------------+
See http://sqlfiddle.com/#!8/aaab8/1
Please note the result is a CHAR(10) in order to overcome TIMEtype limitations. Depending how you plan to use that result, that means that you may have to convert from that string to the appropriate type in your host language.
This worked for me:
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(vluchttijd))) AS totaltime FROM tbl_vluchtgegevens;
I have a table(production) which is having a column with name TimeSpent, the datatype of this column is varchar and it stores data in HH:MM format example 10:23,14:59,11:00 etc.I want to write a query in sql which will sum this and give me a total in HH:MM Format only.
I tried some thing like this but getting error 'Conversion failed when converting date and/or time from character string.':-
select CAST
(
(SUM (datepart(hh, convert (varchar, timespent, 108))) +
(sum(datepart(mi, convert (varchar, timespent, 108)))/60) ) AS VARCHAR(2)
)
+ ':' +
CAST
(
sum(datepart(mi, convert (varchar, timespent, 108))) - 60 * (sum(datepart(mi, convert (varchar, timespent, 108)))/60)
as VARCHAR(2)) from production
Query worked for me, I had just updated my table and decreased the size of TimeSpent to Varchar(25) from varchar(60) and it is fixed.
select CAST
(
(SUM (datepart(hh, convert (varchar, timespent, 108))) +
(sum(datepart(mi, convert (varchar, timespent, 108)))/60) ) AS VARCHAR(2)
)
+ ':' +
CAST
(
sum(datepart(mi, convert (varchar, timespent, 108))) - 60 * (sum(datepart(mi, convert (varchar, timespent, 108)))/60)
as VARCHAR(2))from production