Showing posts with label sql. Show all posts
Showing posts with label sql. Show all posts

Thursday, July 22, 2021

Oracle TO_DATE( ) this afternoon

Just being asked to convert string "1/1/2020 0:00' to date field in Oracle, and come up with this:

select to_date('1/1/2020 0:00', 'dd/mm/yyyy hh24:mi') from dual

Sunday, December 1, 2019

Handling table names with underscore ("_") in flask_sqlalchemy / Python

When you create table class using flask_sqlalchemy (the Flask extension for SQLAlchemy), it can maps the modal class to a proper table automatically. This is cool but if you have a table name with underscore (e.g. MY_SAMPLE_TABLE) and you name your model class with the underscore ('_') as usual, you will find SQLAlchemy will map your table name as something the following:

"MY__SAMPLE__TABLE"

Each single underscore (_) with becomes a double underscores (__). This is annoying and you might believe this is a bug, but in fact you can resolve this issues by naming your table class using the PEP8 naming convention, and named your class as:

class MySampleTable(db.Model):
     field_id = db.Column(db.Integer, primary_key = True)
     field_data = db.Column(db.String(200))

The the SQLAlchemy will map the MySampleTable class to table name as "MY_SAMPLE_TABLE" correctly.

For more information about PEP8, click here for details.

Tuesday, January 10, 2012

Column aggregation in Oracle

Suppose we have a table (TBL) in Oracle with columns (CATEGORY, ATTR). Consider the following:

CATEGORYATTR
1A
1B
1C
2D
2E
2F
3X
3Y
3Z

We want to convert the table as follows:

CATEGORYATTR
1A,B,C
2D,E,F
3X,Y,Z

This can be done via the SQL below:

select CATEGORY, 
       xmlagg(xmlelement(c, ATTR || ',')).extract('//text()') 
from TBL 
group by CATEGORY

CSP on Apache

To add CSP to root if sort of funny. The following will NOT work for most cases !!     <LocationMatch "^/$">        Header s...