create an empty string in DATE format BigQuery SQL - sql

I need to create an empty string/null value. It has to be DATE type. I am UNIONing a couple of tables. Table 1 has date column, table 2 and 3 do not.
Below is the snippet of code for one table that doesn't have date column.
SELECT
CAST(TIMESTAMP('') AS DATE) AS date,
coalesce(id, 'unknown') AS id
FROM
`my_table`
I received the following error: Invalid timestamp: ''
Is there a way to create null value with DATE type?
Thank you in advance.

select cast(null as date) as date
with output

Related

Apply two query statments to a list of tables in sqlite3

In my sqlite database I dynamically generate several (~150) tables, each representing a dataset consisting of a time stamp and a floating point value. These tables can each be described or generated by the following statement:
create table "MY_FIRST_TABLE"
(
Date TIMESTAMP,
Value REAL
);
create index "ix_MY_FIRST_TABLE_Date"
on "MY_FIRST_TABLE" (Date);
Now I want to create a query to retrieve for every one of the tables the first and last date that has a valid value (not equal to None).
I have already found out that I can query a list of all tables in my database with the following statement:
SELECT
name
FROM
sqlite_schema
WHERE
type ='table' AND
name NOT LIKE 'sqlite_%';
In addition, I have the two commands to query for each of the first and last date that has a valid value (not equal to None):
SELECT Date
FROM "MY_FIRST_TABLE"
WHERE Value is not null
ORDER BY Date ASC
LIMIT 1;
SELECT Date
FROM "MY_FIRST_TABLE"
WHERE Value is not null
ORDER BY Date DESC
LIMIT 1;
Now my question is, how can I merge these three individual queries into a single statement to eventually get a table that contains the name of each table with their first and last date of valid values (not equal to None) as a result, like this:
Table
First
Last
FIRST_TABLE_NAME
01.01.2011
12.01.2021
SECOND_TABLE_NAME
05.01.2011
05.02.2021
THIRD_TABLE_NAME
01.06.2011
08.11.2021
You can merge the last 2 queries if you use aggregation:
SELECT 'MY_FIRST_TABLE' TableName,
MIN(Date) First,
MAX(Date) Last
FROM "MY_FIRST_TABLE"
WHERE Value IS NOT NULL;
Then use a programming language to construct the sql statement (with UNION ALL) with a loop over the tables that you get from the 1st query.
Or, with SQLite, you can get the sql statement with:
WITH tables AS (SELECT name FROM sqlite_master WHERE type = 'table')
SELECT GROUP_CONCAT(
'SELECT ''' || name ||
''' TableName, MIN(Date) First, MAX(Date) Last FROM "' || name || '" ' ||
'WHERE Value IS NOT NULL',
' UNION ALL '
) sql
FROM tables;
This returns a string like:
SELECT 'MY_FIRST_TABLE' TableName, MIN(Date) First, MAX(Date) Last FROM "MY_FIRST_TABLE" WHERE Value IS NOT NULL
UNION ALL
SELECT 'MY_SECOND_TABLE' TableName, MIN(Date) First, MAX(Date) Last FROM "MY_SECOND_TABLE" WHERE Value IS NOT NULL
UNION ALL
SELECT 'MY_THIRD_TABLE' TableName, MIN(Date) First, MAX(Date) Last FROM "MY_THIRD_TABLE" WHERE Value IS NOT NULL

How to aggregate rows into one month

I'm attempting to combine data that I am selecting from another table.
This table has a column named IdClient_PK which is a uniqueID and a column DateJoinKey which is the date this user viewed a page.
I would like to combine all DateJoinKey into one month. So For example:
IDClient_PK
DateJoinKey
Views
0E984725-C51C-4BF4-9960-E1C80E27ABA0
01-1-2021
2
0E984725-C51C-4BF4-9960-E1C80E27ABA0
01-3-2021
1
0E984725-C51C-4BF4-9960-E1C80E27ABA0
01-14-2021
3
0E984725-C51C-4BF4-9960-E1C80E27ABA0
01-21-2021
1
I'm attempting to get a result that looks like this:
IDClient_PK
DateJoinKey
Views
0E984725-C51C-4BF4-9960-E1C80E27ABA0
01-1-2021
7
How am I able to do this?
I attempted using the FORMAT() statement in SQL but I run into an error saying: Conversion failed when converting date and/or time from character string.
Here is an example of my query:
CREATE TABLE #tmpModule2(
[IdClient_PK] uniqueidentifier,
[DateJoinKey] DATETIME,
[Views] INT
)
INSERT INTO #tmpModule ([IdClient_PK],[DateJoinKey], [Views] )
SELECT a.[IdClient_PK],
FORMAT(a.DateJoinKey, 'yyyy-MM'),
SUM(a.ViewValue)
FROM [usage].[Fact_RegisteredUsers_UserReport] a
GROUP BY
a.IdClient_PK,
FORMAT(a.DateJoinKey, 'yyyy-MM'),
FORMAT() returns a NVARCHAR data type, but your temp table has that column as a DATETIME. You can either change your CREATE to use the proper data type or SELECT INTO like below. Or you could convert that returned column that you have above to a DATETIME before you insert into your temp table.
SELECT a.[IdClient_PK],
FORMAT(a.DateJoinKey, 'yyyy-MM') as [DateJoinKey],
SUM(a.ViewValue) AS [Views]
INTO #tmpModule
FROM [usage].[Fact_RegisteredUsers_UserReport] a
GROUP BY
a.IdClient_PK,
FORMAT(a.DateJoinKey, 'yyyy-MM')

Select query not records any data when date is used in where clause

I have a table as below
CREATE TABLE TEST
( TEST_ID NUMBER(9,0) DEFAULT NULL,
TEST_DESC VARCHAR2(30 BYTE),
TEST_DATE DATE);
I have inserted the data as follows
1 sample1 28-07-18
2 sample2 29-07-18
3 sample3 30-07-18
4 sample4 31-07-18
5 sample5 01-08-18
when I try to select the records from table TEST by using TEST_DATE as a where clause.
select * from TEST where TEST_DATE = '01-08-18'
select * from TEST where TEST_DATE = TO_DATE('2018-08-01','YYYY-MM-DD')
the above queries returning empty set as output.
Please help me out in this.
Thanks in advance
Date type in Oracle also stores the time. So probably your query doesn't work because of different time parts.
Use this query to get the full view of the Date values:
SELECT TEST_ID, TEST_DESC, TO_CHAR(TEST_DATE, 'yy.mm.dd HH24:MI:SS') FROM TEST;
could be due to the NLS settings in your database,
do a select * from test and see how the output is.
then either change your NLS settings for update your query accordingly to match that.
]2
try adding TRUNC:
select * from TEST where trunc(TEST_DATE) = trunc(TO_DATE('2018-08-01','YYYY-MM-DD'))
First, you can use date constants. So:
where TEST_DATE = DATE '2018-08-01'
If this doesn't work, then you probably inserted data from another century and you should fix the data. You can try this:
select extract(year from test_date), t.test_date
from t
where extract(year from test_date) < 2000
You can fix the data in place (by adding years) or by re-importing the data.
So, use four-digit years. That is what the year values are.

oracle regex_replace to keep digit and backslash

I have excel file that contains some junk data in date field.
the date format is in 1/2/2015 format. I'm trying to load that file into a stage table in varchar field and apply regex replace function to clean up the bad data before loading to main table
can i somebody provide me the suitable experssion for this
create table A
(
bad_date varchar2(4000)
);
insert into A
( bad_date)
values ('1/2/2005');
insert into A
( bad_date)
values ('indep');
insert into A
( bad_date)
values ('/');
commit;
create table B
(
good_date date
);
I want to use regex function to cleanse the data which is not in the date pattern. Thanks for your help!
Use ^[0-9]{1,2}/[0-9]{1,2}/[0-9]{4} pattern for regexp_like conforming your date format.
Use the following insert statement to get clean date data :
insert into B
select * from
( select to_date(bad_date,
case when
regexp_like(bad_date,'^[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}') then
'DD/MM/YYYY'
end) dateConverted
from A)
where dateConverted is not null;
SQL Fiddle Demo
You can come close with something like:
select (case when regexp(bad_date, '^[0-1]?[0-9]/[0-3]?[0-9]/[0-9]{4}$')
then to_date(bad_date, 'MM/DD/YYYY'
end) as converted_date
Use the following:
INSERT INTO B (GOOD_DATE)
SELECT TO_DATE(BAD_DATE, 'DD/MM/YYYY')
FROM A
WHERE REGEXP_LIKE(BAD_DATE, '[0-9]+/[0-9]+/[0-9]+')
SQLFiddle here
Best of luck.
I am inclined to contribute a more mature regex to match valid dates in m/d/yyyy format:
INSERT INTO B (GOOD_DATE)
SELECT TO_DATE(BAD_DATE, 'DD/MM/YYYY')
FROM A
WHERE REGEXP_LIKE(BAD_DATE,
'^(0?[1-9]|[12][0-9]|3[01])\/(0?[1-9]|1[012])\/(19|20)[0-9][0-9]$'
)
SQLFiddle
Inspired by
Regex to validate dates in this format d/m/yyyy
https://www.regular-expressions.info/dates.html

Check if field is numeric, then execute comparison on only those field in one statement?

This may be simple, but I am no SQL whiz so I am getting lost. I understand that sql takes your query and executes it in a certain order, which I believe is why this query does not work:
select * from purchaseorders
where IsNumeric(purchase_order_number) = 1
and cast(purchase_order_number as int) >= 7
MOST of the purchar_order_number fields are numeric, but we introduce alphanumeric ones recently. The data I am trying to get is to see if '7' is greater than the highest numeric purchase_order_number.
The Numeric() function filters out the alphanumeric fields fine, but doing the subsequent cast comparison throws this error:
Conversion failed when converting the nvarchar value '124-4356AB' to data type int.
I am not asking what the error means, that is obvious. I am asking if there is a way to accomplish what I want in a single query, preferably in the where clause due to ORM constraints.
does this work for you?
select * from purchaseorders
where (case when IsNumeric(purchase_order_number) = 1
then cast(purchase_order_number as int)
else 0 end) >= 7
You can do a select with a subselect
select * from (
select * from purchaseorders
where IsNumeric(purchase_order_number) = 1) as correct_orders
where cast(purchase_order_number as int) >= 7
try this:
select * from purchaseorders
where try_cast(purchase_order_number as int) >= 7
have to check which column has numeric values only.
Currently, in a table every field is setted with nvarchar(max) Like tableName (field1 nvarchar(max),field2 nvarchar(max),field3 nvarchar(3)) and tableName has 25lac Rows.
But on manually Check Field2 Contain the numeric Values Only... How to Check With t-sql that in the Complete Column (Field2) has numeric Value or not/null value with Longest Length in the Column!