MySQL - my months are current stored 0-11 - mysql

I thought I ran into a bug with MySQL 5.1, but the bug was in the perl code that's creating the timestamps. perl's localtime uses 0-11 for months, but MySQL's datetime uses 1-12. So, I've got all these malformed timestamps that I need to update.
2012-00-19 09:03:30
This should be:
2012-01-19 09:03:30
The problem is that the date functions for MySQL return NULL on a 00 month. Is there a way to do this in MySQL?
EDIT: Solution =
UPDATE test_stats
SET start_time = CAST(CONCAT(SUBSTRING(start_time, 1, 5),
CAST((CAST(SUBSTRING(start_time, 6, 2) AS UNSIGNED) + 1) AS CHAR(2)),
SUBSTRING(start_time, 8, 12)) AS DATETIME);
By the way, I was using MySQL 5.1

This should work:
UPDATE MyTable
SET DateTimeField =
CAST (
SUBSTRING(DateTimeString, 1, 5) -- '2012-'
+ CAST((CAST(SUBSTRING(DateTimeString, 6, 2) AS INT) + 1) AS VARCHAR) -- '00' => '1'
+ SUBSTRING(DateTimeString, 8, 12) -- '-19 09:03:30'
AS DATETIME)
Test with this select
DECLARE #x VARCHAR(50) = '2012-00-19 09:03:30'
SELECT CAST(SUBSTRING(#x, 1, 5)
+ CAST((CAST(SUBSTRING(#x, 6, 2) AS INT) + 1) AS VARCHAR)
+ SUBSTRING(#x, 8, 12) AS DATETIME)

Related

How to convert datetime.datetime to datetime.date?

From my sql query I'm getting output as datetime.datetime(2020, 9, 22, 0, 0)
query = '''SELECT checkin_date FROM `table1`
WHERE checkin_date BETWEEN %s AND %s'''
cursor.execute(query,(startDate, endDate)
results = cursor.fetchall()
#results:
#[(datetime.datetime(2020, 9, 22, 0, 0), datetime.datetime(2020, 9, 24, 0, 0))]
for res in results:
## When I print type I get correct result
print(type(res[0]) ## <type 'datetime.datetime'>
##when I compare with another datetime.date (currentDate variable)
if res[0] < currentDate:
## I get error `TypeError: can't compare datetime.datetime to datetime.date` *which is expected*
## But when I use .date()
if res[0].date() < currentDate:
## I get `TypeError: can't compare datetime.date to unicode`
I tried converting currentDate to datetime.datetime, but still doesn't work. Can't seem to figure out what's the issue here.
To force your query to spit out the date format you want, change it to this:
SELECT DATE_FORMAT(checkin_date, '%Y-%c-%d')
FROM table1
WHERE DATE(checkin_date) BETWEEN %s AND %s
To make it able to use an index on your checkin_date column, change it to this.
SELECT DATE_FORMAT(checkin_date, '%Y-%c-%d')
FROM table1
WHERE checkin_date >= DATE(%s)
AND checkin_date < DATE(%s) + INTERVAL 1 DAY
Try this
splitting a datetime column into year, month and week
SELECT Year(checkin_date), Month(Checkin_date), Day(Checkin_date),
FORMAT(GETDATE(),'HH'), FORMAT(GETDATE(),'mm')
FROM table1
WHERE (CAST(checkin_date AS DATE) BETWEEN '2018-01-01' AND '2020-01-01')
Note: Use 'HH' for 24 hours format and 'hh' for 12.

How to auto increment a string with sql query

I am stuck at a point where i have to increment a string, and my strings are of type C001,SC001,B001
in my data base they are defined like
what i am trying to do do is write a query which check the previous highest code present into my db and the incriment it to +1
for example C001 -> C002,C009->C010,C099`->C100 and so on
Similarly for SC001->SC002,SC009->SC010,SC099->SC100 and so on
Similarly fro B001 -> B002,B009->B010,B099`->B100 and so on
I have a query which my friend has suggested me to use but that query only incriminating AAAA->AAAA01 , AAAA09->AAAA10
query is
SELECT id AS PrevID, CONCAT(
SUBSTRING(id, 1, 4),
IF(CAST(SUBSTRING(id, 5) AS UNSIGNED) <= 9, '0', ''),
CAST(SUBSTRING(id, 5) AS UNSIGNED) + 1
) AS NextID
FROM (
-- since you allow strings such as AAAA20 and AAAA100 you can no longer use MAX
SELECT id
FROM t
ORDER BY SUBSTRING(id, 1, 4) DESC, CAST(SUBSTRING(id, 5) AS UNSIGNED) DESC
LIMIT 1
) x
when i am replacing ID with CategoryCode it is giving me PrevID-C004 NextID-C00401 which is not my requirement i want PrevID-C004 and NextID->C005
NOTE i am using my sqlServer 5.1
Just try this one ,
SELECT
CategoryCode,CAST(CONCAT(LPAD(CategoryCode,1,0),LPAD(MAX(RIGHT(CategoryCode,
3)) + 1, 3, 0) ) AS CHAR),
FROM test
SELECT
SubCategoryCode,CAST(CONCAT(LPAD(SubCategoryCode,2,0),
LPAD(MAX(RIGHT(CategoryCode, 3)) + 1, 3, 0) ) AS CHAR),
FROM test
SELECT
BrandCode,CAST(CONCAT(LPAD(BrandCode,1,0), LPAD(MAX(RIGHT(BrandCode, 3)) +
1, 3, 0)) AS CHAR) FROM test

Error select from sqlserver - 2008

I have a problem with date conversion and the displayed error. I think the block below is the problem.
I need to bring the difference between the dates and calculate them to know if the invoice is open "A" or downloaded "B", but it is returning the error below.
Conversion failed when converting the varchar value '30/12/' to datatype int.
<pre><code>
SELECT [vl_recebido]
,[vl_pago]
,[vl_original]
,s.[dt_baixa]
,[dt_vencto]
,[dt_rp]
,S.[id_divisao]
,[nro_baixa]
,S.[nm_cliente]
,ISNULL(C.id_cliente, -1) AS id_cliente
,[nro_documento]
,[ds_moeda]
,[cd_cc]
,[cd_t_cta]
,[cd_port]
,[cd_oper]
,[ds_observacao]
,[ds_complemento]
,[nm_resp_baixa]
,[qtd_recibo]
,[qtd_pagto]
,t.[qtd_colaborador]
,t.[vl_meta]
,[flag]
,[bco_sacado]
,[cheque_numero]
,[in_processo]
,isnull(af.id_aging_fatura, 5) as id_aging_fatura
,af.dsc_faixa_aging
,af.qtd_aging
,datediff(d, s.dt_vencto, s.dt_baixa) as qtd_aging_calc
,case when s.dt_baixa is null then 'A' else 'B' end as status_da_fatura
FROM stg_recibo_baixas S
LEFT JOIN dim_cliente C ON C.cod_cliente = S.cod_cliente
AND C.id_divisao = S.id_divisao
INNER JOIN tab_colaborador_baixa t ON t.id_mes = CONVERT(INT, CONVERT(VARCHAR(6), s.dt_baixa, 112))
left join dim_aging_faturas af on af.qtd_aging = (select min(qtd_aging) from dim_aging_faturas a
where a.qtd_aging >= case when datediff(d, s.dt_vencto, case when s.dt_baixa is null then getdate() else convert(datetime, substring(s.dt_baixa, 8, 4) + '-' + substring(s.dt_baixa, 4, 2) +'-'+ substring(s.dt_baixa, 0, 2), 103) end) < 0 then 0 else datediff(d, s.dt_vencto, convert(datetime, substring(s.dt_baixa, 8, 4) + '-' + substring(s.dt_baixa, 4, 2) +'-'+ substring(s.dt_baixa, 0, 2), 103)) end)
</code></pre>

Add 28 to last 2 digit of date and replace the order

I have a number such as this : 840106
I need to do the following :
Change the number to date add - and flip the number : 06-01-84
add 28 to the last 2 digit that the date will be : 06-01-12
84 + 16 = 00 + 12 = 12
number is always changing sometimes it cab be 850617 , but format is always same add - and add 28 last 2 digit.
any ideas how to help me here ?
Here is a sqlite solution:
create table t( c text);
insert into t (c) values(990831);
insert into t (c) values(840106);
insert into t (c) values(800315);
insert into t (c) values(750527);
insert into t (c) values(700923);
insert into t (c) values(620308);
select c, substr(c,5,2) || '-' || substr(c,3,2) || '-' ||
case when (substr(c,1,2) + 28) < 100 then (substr(c,1,2) + 28)
else case when ((substr(c,1,2) + 28) - 100) < 10 then '0' || ((substr(c,1,2) + 28) - 100)
else ((substr(c,1,2) + 28) - 100)
end
end
from t;
For formatting you can use
http://www.w3schools.com/sql/func_date_format.asp
For adding days to the date you should take a look at date_add() function
mysql> SELECT DATE_ADD('1998-01-02', INTERVAL 31 DAY);
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
Assuming date is the name of the column containing your date:
DATE_FORMAT(DATE_ADD(STR_TO_DATE(date, %y%m%d), INTERVAL 28 YEAR), %d-%m-%y);
What this does is first formats the string into a date, then adds 28 years, then converts back to string with the new format.
SQLite is a lot tricker with this, you'll need to use substrings.
substr(date,5) || "-" || substr(date,3,4) || "-" || CAST(CAST(substr(date,1,2) as integer) + 28 - 100) as text
I'm not too experienced with SQLite so the casting may be a bit weird.
Here is a t-sql solution that you can use and migrate to mysql.
declare #myDate as char(8) = '840106';
declare #y as char(2), #m as char(2), #d as char(2);
set #y = LEFT (#myDate, 2);
declare #yi as int = Convert (int, #y);
IF #y between 30 and 99 ----------- pick a cut-off year
SET #yi = (28 - (100-#yi));
SET #y = CONVERT(char, #yi)
set #m = SUBSTRING(#myDate, 3, 2);
set #d = SUBSTRING(#myDate, 5, 2);
SET #myDate = #d + '-' + #m + '-' + #y;
PRINT #myDate;

MySQL Calculations using variables

Why is this not working? I would like to get the result of:
(#sum_hours * #rate_hours) + (#sum_travel * #rate_travel) + (#sum_miles * #rate_miles)
The results of my sub queries are as follows:
#sum_hours = 5.00000, #sum_travel = 2.00000, #sum_miles = 0.00000
#rate_hours = 35.00000, #rate_travel = 35.00000, #rate_miles = NULL
#_rate_hours = CAST(IF(#rate_hours, #rate_hours, 0) AS DECIMAL(10, 5)) = 35.00000
#_rate_travel = CAST(IF(#rate_travel, #rate_travel, 0) AS DECIMAL(10, 5)) = 35.00000
#_rate_miles = CAST(IF(#rate_miles, #rate_miles, 0) AS DECIMAL(10, 5)) = 0.00000
So now that I have some values, I'm doing:
#total:= (#sum_hours * #_rate_hours)
I expect this to be 175, but it returns NULL. If I directly declare #sum_hours as 5
#sum_hours:= 5
I get 175, as expected. Am I doing something wrong? How can I get this to return 175, assuming I don't know that the values are supposed to be 5, 35 and 175?
#sum_hours is being cast as decimal, but with and without the cast it does not work.
Edit:
This is a stripped back version of the query, as the JOINs and subqueries aren't relevant, just the return details. All numeric fields are stored as FLOAT in the database. The values are confirmed in the result, but the calculation for #total does not work (returns null).
I have worked around this using COALESCE and some other bits, but I'm still curious as to why it would not calculate properly - or what I'm doing wrong.
SELECT
#sum_hours:= CAST(hours AS DECIMAL(10, 5)),
#sum_travel:= CAST(travel AS DECIMAL(10, 5)),
#sum_miles:= CAST(miles AS DECIMAL(10, 5)),
#rate_hours:= (SELECT ... returns 35),
#rate_travel:= (SELECT ... returns 35),
#rate_miles:= (SELECT ... returns NULL),
#_rate_hours = CAST(IF(#rate_hours, #rate_hours, 0) AS DECIMAL(10, 5)),
#_rate_travel = CAST(IF(#rate_travel, #rate_travel, 0) AS DECIMAL(10, 5)),
#_rate_miles = CAST(IF(#rate_miles, #rate_miles, 0) AS DECIMAL(10, 5)),
#total:= (#sum_hours * #_rate_hours) + (#sum_travel * #_rate_travel) + (#sum_miles * #_rate_miles)
FROM table
WHERE
id = '...'
GROUP BY id