I am having a problem with this date format change - teradata

I need help with this Teradata date format change
I have tried multiple queries and date formats and nothing seems to work
This was what I tried originally:
'''Teradata
select distinct concat(trim(EXTRACT(month FROM(rqst.admt_dt))), '/1/', trim(EXTRACT(year FROM(rqst.admt_dt)))) as MonYr
'''
When I create a volatile table with this it makes the datatype varchar(25) and I want the datatype as a date. I tried multiple cast date formats but none of the code was accurate. I decided to do this instead:
'''Teradata
select distinct rqst.admt_dt as MonYr
'''
After I run through all my scripts I end up with the final table and the MonYr is a date but I need to change any days that are not 01 to 01. Examples of the dates I end up with are:
6/02/2018
6/15/2018
6/22/2018
I tried this code to modify the DD to 01
'''Teradata
update dl_aa_tm_oprpt_s.TinaPAVDrop
set monyr = date format 'mm/01/yyyy'
'''
That does not work either. I am at a loss as to how to remedy this issue

If you want take a date and get the first day of that month:
SELECT
current_date - extract(day from current_date) + 1
Just replace current_date with your date column.

Related

Query between dates in SQLITE

I'm trying to query what happened between today and yesterday. To example on the 17th of June 2016 it would look like:
SELECT * FROM Inspection_Log WHERE date_time BETWEEN '2016-6-16' AND '2016-6-17'
But these days are relative, and this won't work say tomorrow, or really every again. So I've encountered this page where tells me now to use DATE as it's just a polite wrapper around strftime.
But here is my current issue:
This query works:
>SELECT COUNT(*) FROM Inspection_Log WHERE date_time BETWEEN '2016-6-16' AND '2016-6-17'
535
But when I use date('yada', '+1 day')
>SELECT COUNT(*) FROM Inspection_LOG WHERE date_time BETWEEN '2016-6-16' AND DATE('2016-6-16','+1 day')
0
So I try with strftime
>SELECT COUNT(*) FROM Inspection_LOG WHERE date_time BETWEEN '2016-6-16' AND strftime('%Y-%M-%D','2016-6-16','+1 day')
0
So I try with datetime
>SELECT COUNT(*) FROM Inspection_LOG WHERE date_time BETWEEN '2016-6-16' AND datetime('2016-6-16','+1 day')
0
Digging into this here is what i see
SELECT time('now')
'2016-06-24'
SELECT date('now')
'2016-06-24'
SELECT date('now','-1 day')
'2016-06-23'
SELECT date('2016-6-24','-1 day')
NONE
What am I doing wrong?
You need to change: AND strftime('%Y-%M-%D','2016-6-16','+1 day') for AND strftime('%Y-%m-%d','2016-06-16','+1 day').
1 - You should use '%Y-%m-%d' for the first parameter 'YYYY-MM-DD'.
The format string supports the most common substitutions found in the strftime() function from the standard C library plus two new substitutions, %f and %J. The complete list link
2 - A time string must be follow the format: YYYY-MM-DD, then you need to use '2016-06-16'.
There is a question and answer : SQL Select between dates
Okay so I was totally and completely wrong.
My scheme looks like this:
CREATE TABLE InspectionLog(
date_time DATE,
station_name TEXT,
inspection TEXT,
barcode_part_number TEXT,
bus_part_number TEXT,
barcode_serial_number TEXT,
bus_serial_number TEXT,
rework_operation TEXT,
status TEXT,
ng_description TEXT
)
DATE is not a valid data type. It is actually a high level wrapper around INTEGER and TEXT depending on the data placed into it. Sqlite3 defaults to TEXT.
What this means is when I perform an insert/update which does something similar to:
date_time = '2016-6-16'
This is valid as date_time is really TEXT not DATE. And when I preform a search that uses the DATE data type, it will skip any row which isn't a DATE.
The long version is. I inserted ~250MB incorrectly formatted into this table. After fixing my tests and functions so my inserts always have 2 day/month digits the majority of the OP's time queries work correctly.

Checking if a Date falls within a Range of Dates in SQLITE

I have an sqlite table with with 2 date columns (beginning & end). I am looking for a query that returns the date range a date falls in. e.g Checking range where 10-02-2016 falls given 2 records as follows
(01-02-2016 - 12-02-2016) & (13-02-2016 - 20-02-2016)
Clearly it falls in the first range but how do I do it in SQLITE. Please help?
I think the problem is next SQLite requires dates to be in format YYYY-MM-DD. With that format you could write a simple query:
select * from table where yourdate between begin and end
If you can't change your format you should look at next sqlite functions https://sqlite.org/lang_datefunc.html
Also you can try substr function to cotact date in needed format.

I need a sqlite equivalent of the folling msaccess query

Select distinct Format(DateAdd(""s""," & columnname & ",""1/1/1980 12:00:00 AM""), 'dd-MMM-yyyy') as A
I have assumed that the seconds to add and the original date are hard coded values below whilst awaiting clarifications requested in the comments.
To add a number of seconds to a date you can use:
select datetime('1980-01-01 00:00:00', "345000 seconds");
This gives the result: 1980-01-04 23:50:00
The example above is just under 4 days in seconds, if you want to truncate the result to just the date as implied by the query in your questions then you can wrap this inside a date function. However, this would give the result in the format "YYYY-MM-DD" rather than "DD-MMM-YYYY" as your access query does.
Unfortunately I cannot find any native SQLite function to convert a numeric month value to mmm format. You can do this manually with replace (similar to the answer to this question), but this is a bit messy.
If you are happy to live with the numeric months then you can simply use:
select strftime('%d-%m-%Y', '1980-01-01 00:00:00', "345000 seconds");
This gives the result: 04-01-1980
More information on the SQLite date / time functions can be found here.

SQLite3 Date Ranges not working

I cannot get date ranges to work properly in a query using SQLite3
Create And Populate Table:
create table dates(myDate DATE);
insert into dates values('2012-10-1');
insert into dates values('2012-10-2');
insert into dates values('2012-10-3');
insert into dates values('2012-10-4');
insert into dates values('2012-10-5');
insert into dates values('2012-10-6');
insert into dates values('2012-10-7');
insert into dates values('2012-10-8');
insert into dates values('2012-10-9');
insert into dates values('2012-10-10');
Query:
select * from dates where myDate >= '2012-10-1' and myDate < '2012-10-31';
Results:
2012-10-1
2012-10-2
2012-10-3
2012-10-10
Where are 10-4 - 10-9?
I get the same results if I use between:
select * from dates where myDate BETWEEN '2012-10-1' AND '2012-10-30';
If I change either of the queries to use '2012-11-1' as the end date, they work properly.
Any help would be greatly appreciated!
Date values must be in the format yyyy-mm-dd with fixed-width fields.
This is required because SQLite has no native date format; to compare date strings correctly, fields with the same meaning must always be at the same position in the string.

SQLite Extract Month From Column

I have a column of data with this date format mm/dd/yyyy. I would like to covert to this to the format yyyy-mm-dd for (SQLite usage). How could I achieve this?
UPDATE Table_Name
SET Column_Name = 'yyyy-mm-dd' format
I have tried substr, ltrim, rtrim (None of this work for my case).
My data are dynamic.
Sample
Column_Name
6/1/2004
06/25/2004
7/1/2003
6/1/2004
6/1/2004
09/19/2003
09/30/2003
09/30/2003
09/30/2003
09/30/2003
The Goal: Extract only month from this Column (Without displaying unnecessary stuff)
Thank you.
The syntax for extracting a specific date element from a date field is to use strftime.
SELECT strftime('%m', Column_Name) AS month which is your second question. For reconstituting the date data you could use (at least in JavaScript) substr to split the date out then format it as you require. Also see this post:Get month from DATETIME in sqlite

Resources