I am trying to convert a string column of existing values from 'MM/DD/YY' to 'YYYY/MM/DD'.
Example Data:
Table: backlog_backlogData
[LoadDate]
06/29/18
06/29/18
06/28/18
07/24/18
07/24/18
I have tried this (in SQLite Manager FireFox extention):
UPDATE backlog_backlogData SET LoadDate = substr(LoadDate, 7, 2)||”-“||substr(LoadDate, 1,2)||”-“||substr(LoadDate, 4,2)
Unfortunately, the column is updated with only the first substr (results: 18). It seems like SQLite does not like concatenates ||?
I've searched for an answer and have not found anything that works.
Thanks for you time.
try replacing ” with "
you can check the query with a SELECT statement
SELECT substr(LoadDate, 7, 2)||"-"||
substr(LoadDate, 1,2)||"-"||
substr(LoadDate, 4,2)
from backlog_backlogData
example using python to convert date table column
I'm confident it was an issue with the SQLite Manager Firefox Extension. I ran the following code from cursor in python and it worked:
UPDATE backlog_backlogData SET LoadDate = substr(LoadDate, 7, 2) || '-' || substr(LoadDate, 1,2) || '-' || substr(LoadDate, 4,2)
I then reset my data and reran the code within the SQLite Manager Extension and it only processed the code up to the first concat.
EDIT: Thanks for the help below!
Related
I followed the steps in this question
Append text to column data based on the column in PostgreSQL
But I made a mistake in the SIMILAR TO clause and the text got added to fields it shouldn't have. How can I reverse it?
Te query I ran was:
update metadatavalue set text_value = 'Fil: ' || text_value where metadata_field_id = 136 and text_value not similar to 'Fill:%';
How can I remove extre characters from those fields?
Thanks a lot in advance.
You can trim the prepended string off and update the column with the result:
UPDATE metadatavalue
SET text_value = regexp_replace(text_value, '^Fil: ','');
As a start, I am extremely new at Python.
I am receiving an Excel file where the date field is incomplete. The value displays as "190808" (YYMMDD) instead of "2019-08-08".
Part of my automation attempt is to move the file to a different location, where the file is renamed. I want to use the date field to change the file name to the file description and date (e.g. "Sales figures 201908").
The code I have only works if the date format is
str(df['Bank date'][0].strftime("%Y%m"))
I have tried dateparser with the following:
dateparser.parse(df['Bank date'][0].strftime("%Y.%m"))
The error I am receiving is 'numpy.int64' object has no attribute 'strftime'
Any help will do.
Thanks.
I modified it slightly and built my own date-string using slicing.
vOldDate = str(df['Bank date'][0])
vNewDate = '20' + vOldDate[:2] + '.' + vOldDate[2:4]
Numpy is interpreting the date as an integer. To use dateparser, you need to convert that value into a string first, then parse that string, and then format the result:
dateparser.parse(str(df['Bank date'][0])).strftime("%Y.%m")
Since the input format is expected, you should specify it to ensure you get the right date:
>>> dateparser.parse(str(190808), date_formats=['%y%m%d']).strftime("%Y.%m")
'2019.08'
I have a text field in my SQLite database that stores a Time value, but for unrelated reasons I can't change the data type to TIME.
The values are stored in HH:MM format, and I'm having trouble trying to sort results by time because the values below '10:00' are missing a leading zero. I would prefer not to store the data with leading zero for the same unrelated reasons.
I'd like to add something to the Query that would pad the missing character if necessary, causing the results to read '08:30' when collected. I've been searching through the command and function lexicon though and I'm not finding what I need.
Is there a simple way to do this inside a query?
Thanks
I think this would work:
select your_col, case when length(your_col) < 5
then '0' || your_col else your_col end from your_table
Demo using Python
>>> conn.execute('''select c, case when length(c) < 5
then '0' || c else c end from t''').fetchall()
[(u'10:00', u'10:00'), (u'8:00', u'08:00')]
SELECT REPLACE(PRINTF('%5s', your_col), ' ', '0') FROM your_table
The PRINTF call pads the value with spaces until it's 5 characters, and the
REPLACE call replaces those spaces with zeros.
Please Help! This has been driving me nuts. YES I have looked at dozens of Stack Overflow answers for similar questions, and they all say "Your dates should be in a consistent accepted format".
All of my SQLite dates are stored in the following string format:
'yyyy-MM-dd HH:mm:ss'
I'm trying to do date comparison but NOTHING works. For instance:
SELECT transactiondate
FROM transactions
WHERE transactiondate >= '2010-02-19 00:00:00'
Doesn't work.. I get 0 records returned.
Even though when I run it without the WHERE clause I get returned date strings like:
'2017-02-25 00:00:00'
In fact, simply doing:
SELECT transactiondate
FROM transactions
WHERE '2017-02-25 00:00:00' >= '2010-02-19 00:00:00'
Does work, i.e. it compares the dates successfully in its raw form but not when referring to the table column value.
PLEASE.. WTF am I doing wrong?
Also, I've tried every permutation of datetime(...), date(...) in the SQL to no avail. I have also tried using ... BETWEEN ... AND ..., it doesn't help.
Thanks in advance if anyone can figure this out.
You got the ' in the front and end included in your data. In column transactiondate.
Then you also need to include ' chars in your WHERE filter too. Two ways to do that in SQLite:
select * from transactions where transactiondate >= "'2010-02-19 00:00:00'";
select * from transactions where transactiondate >= '''2010-02-19 00:00:00''';
However, I suggest you clean up your data instead:
update transactions
set transactiondate=substr(transactiondate,2,length(transactiondate)-2)
where transactiondate like "'%'";
Now you can use your original WHERE without the extra ' chars.
OK, This is not an answer. But in SO you can't do this kind of thing in a comment.
I can't replicate the condition you have. The code, in particular the SELECT, works well enough executed from Python.
>>> import sqlite3
>>> conn = sqlite3.connect(':memory:')
>>> cur = conn.cursor()
>>> cur.execute('CREATE TABLE transactions (transactionsdate string)')
<sqlite3.Cursor object at 0x000000000531CCE0>
>>> cur.execute('''INSERT INTO transactions VALUES ('2010-02-10 00:00:00')''')
<sqlite3.Cursor object at 0x000000000531CCE0>
>>> cur.execute('''INSERT INTO transactions VALUES ('2017-02-10 00:00:00')''')
<sqlite3.Cursor object at 0x000000000531CCE0>
>>> list(cur.execute("SELECT * from transactions where transactionsdate >= '2010-02-19 00:00:00'"))
[('2017-02-10 00:00:00',)]
>>> list(cur.execute("SELECT * from transactions"))
[('2010-02-10 00:00:00',), ('2017-02-10 00:00:00',)]
Stab in the dark: Can you dump the contents of the field into a text file and examine it using (say) Notepad++, which would enable to to see non-printing characters that are interfering?
I have 2 year-month prompts. If I don't select any year-month in 1st prompt, report should by default, run from January of the same year, selected in 2nd prompt. My prompts are value prompts and have string values. Please help me materialise the requirement. I have already tried # prompt macro, ?prompt?, case when etc. I am nto sure, If javascript would help.
I'm going to assume your underlying date fields are not stored as DATE value types since you're using strings. This may be easier split into 4 prompts: from month, from year, to month, to year.
The filter would then be an implied if:
(
(?FROM_YEAR? = '' or ?FROM_MONTH? = '') and
[database_from_month] = '01' and
[database_from_year] = ?TO_YEAR? and
[database_to_month] = ?TO_MONTH? and
[database_to_year] = ?TO_YEAR?
)
OR
(
(?FROM_YEAR? <> '' or ?FROM_MONTH? <> '') and
[database_from_month] = ?FROM_MONTH? and
[database_from_year] = ?FROM_YEAR? and
[database_to_month] = ?TO_MONTH? and
[database_to_year] = ?TO_YEAR?
)
The above style filter is superior for many reasons:
More likely to be sargeable
Easy to understand
Uses simple built-in Cognos functions; more likely to be cross-version compliant
No issues with cross-browser support you would get with Javascript
Code snippet would work in other Cognos studios (Business Insight, etc)
You've likely seen CASE statements in filters throws an error. The CASE statement is passed to SQL, not compiled into a SQL statement via Cognos. Hence it's not seen as proper syntax.