Extract Calenderweek, year AND update the table with new values - google-bigquery

I am totally new to SQL & Bigquery even though I am trying to learn I habe an obvious lack of knowledge.
I have a table with a date as timestamp and try to extract calenderweek and year. I know how select it the right way but the results end up in a temp table.
My wish is to update the same table with the same query and add the two columns with the specific values.
I tried to use informations from here but I get stuck in the syntax: https://cloud.google.com/bigquery/docs/reference/standard-sql/dml-syntax#update_statement
I am open to any advices.
timestamp = Name of my column with the date
SELECT timestamp,
EXTRACT (ISOWEEK FROM timestamp) AS KW,
EXTRACT (YEAR FROM timestamp) AS Jahr,
FROM MyTable AS date;

Finally found the solution:
UPDATE
MyTable
SET
KW = EXTRACT (ISOWEEK FROM timestamp),
Year_2 = EXTRACT (YEAR FROM timestamp)
WHERE
Year is null;

Related

SQL Server - Looking for a way to shorten code

I'm basically very new to SQL Server, so please bare with me. Here is my problem:
I have a table with (let's say) 10 columns and 80k rows. I have 1 column called Date in the format of YYYY-MM-DD type varchar(50) (can't convert it to date or datetime type I tried, the initial source of data is not good).
**Example :
Table [dbo].[TestDates]
Code
SellDate
XS4158
2019-11-26
DE7845
2020-02-06
What I need to do is to turn the YYYY-MM-DD format to DD/MM/YYYY format. After a lot of tries (I tried the functions (DATE_FORMAT, CONVERT, TO_DATE etc) and this is solution :
1- I added a primary key for join purpose later (ID)
2- I split my date column in 3 columns in a whole new table
3- I merged the 3 columns in the order I need with the delimiter of my choice (/) in the same new table
4- I copied the good column to my initial table using the primary key ID I created before
alter table [dbo].[TestDates]
add ID int not null IDENTITY primary key;
SELECT ID,
FORMAT(DATEPART(month, [SellDate]),'00') AS Month,
FORMAT(DATEPART(day, [SellDate]),'00') AS Day,
FORMAT(DATEPART(year, [SellDate]),'0000') AS Year
INTO [dbo].[TestDates_SPLIT]
FROM [dbo].[TestDates]
GO
ALTER TABLE [dbo].[TestDates_SPLIT]
ADD SellDate_OK varchar(50)
UPDATE [dbo].[TestDates_SPLIT]
SET SellDate_OK = [Day] + '/' + [Month] + '/' + [Year]
ALTER TABLE [dbo].[TestDates_SPLIT]
DROP COLUMN Month, Day, Year
ALTER TABLE [dbo].[TestDates]
ADD SellDate_GOOD varchar(50)
UPDATE [dbo].[TestDates]
SET [TestDates].SellDate_GOOD = [TestDates_SPLIT].SellDate_OK
FROM [dbo].[TestDates]
INNER JOIN [dbo].[TestDates_SPLIT]
ON [TestDates].ID = [TestDates_SPLIT].ID
This code works but i find too heavy and long, considering I have 6 more dates columns to work on. Is there a way to make it shorter or more efficient? Maybe with SET SellDate = SELECT (some query of sorts that doesn't require to create and delete table)
Thank you for your help
I tried the usual SQL functions but since my column is a varchar type, the converting was impossible
You should not be storing dates as text. But, that being said, we can try doing a rountrip conversion from text YYYY-MM-DD to date to text DD/MM/YYYY:
WITH cte AS (
SELECT '2022-11-08' AS dt
)
SELECT dt, -- 2022-11-08
CONVERT(varchar(10), CONVERT(datetime, dt, 121), 103) -- 08/11/2022
FROM cte;
Demo

SQL - Oracle compare data in two columns of one table

Can we compare the columns of same table and get a result by selecting all the rows where both columns data does not match. Compare CREATE_DATE and UPDATE_DATE
Table with data
Expected output:
SELECT *
FROM Table
WHERE TIME_CREATED <> UPDATE_DATE
There should be no issue comparing date to timestamp, please update your OP if you have tried this already
If there is an issue, read this post and try something like this:
Oracle comparing timestamp with date (also shows use of truncate to disregard time of day)
SELECT *
FROM Table
WHERE to_timestamp(TIME_CREATED,'D/MM/YYYY HH:MM:SS') <> UPDATE_DATE
OR
SELECT *
FROM Table
WHERE TIME_CREATED <> TO_DATE(UPDATE_DATE,'DD.MM.YYYY:HH24:MI:SS')
You might have to play with the timestamp format a bit, see https://www.akadia.com/services/ora_date_time.html

How to convert two types of dates format into one date format in SQL?

I have three table in the database two of them contain dates however the dates are in two format, first 20-02-2011 and second is 25/09/2018. Let say each table have 10000 records and mixed with these two types of dates format. This why I why I create the column like --- (Transaction_Date, Varchar(10) Not Null)
I tried convert (Varchar(10),Transaction_Date,105)
and also tried replace(convert(varchar(10),Transaction_Date,105),'/','-')
However date and year functions are still not working.
Please suggest a possible way.
How about this?
select replace(date, replace(Transaction_Date, '/', '-'), 105)
That is: (1) convert to a date and (2) replace the slash before converting.
You need to remember about your culture. Saved format vs server culture. But this is very possible
select Cast('2-22-2011' as datetime) f1,
Cast('2/22/2011' as datetime) f2
I other words just use Cast
select cast(Transaction_Date as datetime) . . .
But you should as soon as possible get rid of columns that saves date as string and create new date/time column, and insert your date values there
alter table tbl add column temp datetime
update tbl set temp = Cast(Transaction_Date as datetime)
alter table tbl drop column Transaction_Date
alter table tbl add column Transaction_Date datetime
update tbl set Transaction_Date = temp
alter table tbl drop column temp

Oracle 12c - how to set 'date' column with year quarter?

I have a need to create a column in Oracle 12c that will be date column with values:
20163
20164
20171
20172
20173
20174
...
How to specify that for a column in a create table statement?
thanks.
You can model the column as containing dates, constrained to fall only on the first moment of each quarter.
CREATE TABLE TEMP
( QUARTER DATE
CONSTRAINT IS_QUARTER CHECK ( (QUARTER = TRUNC(QUARTER, 'Q') ) )
)
To put values into the table, you need to set the date to the start of the quarter:
INSERT INTO temp VALUES ( TO_DATE('2017-04', 'yyyy-mm') );
To read values from the table, you can format as you like:
SELECT TO_CHAR( quarter, 'YYYYQ') FROM temp;
Because the underlying column is a date, you can do things like compare it with other dates, etc.
SELECT TO_CHAR( ADD_MONTHS( quarter, 3 ), 'YYYYQ') FROM temp;
Joe!
Such fields as you described are frequently using for monthly periods.
Typicaly they are encoded in integer datatypes.
If you were not on Oracle, Int32 would be perfect.
In Oracle the decision is not so clear.
I would prefer NUMBER(6,0) and simple constraint that field most be less than any possible date in future.
CHAR(6) - is fine too but constraints will be more complex.

Display Now date and Time in SQl table column

I want to be able to have todays date and time now in a table column
If my table is say Table1, basically it should display the time and date when
SELECT * FROM Table1 is run.
I've tried the following but they just show the time from the moment in time I assign the value to column
ALTER TABLE Table1
ADD TodaysDate DateTime NOT NULL DEFAULT GETDATE()
and
ALTER TABLE Table1
ADD TodaysDate DateTime
UPDATE Table1
SET TodaysDate = GETDATE()
Hope this is clear. any help is appreciated.
Thanks
In SQL Server you can use a computed column:
alter table table1 add TodaysDate as (cast(getdate() as date));
(use just getdate() for the date and time)
This adds a "virtual" column that gets calculated every time it is referenced. The use of such a thing is unclear. Well, I could imagine that if you are exporting the data to a file or another application, then it could have some use for this to be built-in.
I hope this clarifies your requirement.
The SQL Server columns with default values stores the values inside the table. When you select the values from table, the stored date time will be displayed.
There are 2 options I see without adding the column to the table itself.
You can use SELECT *, GETDATE() as TodaysDate FROM Table1
You can create a view on top of Table 1 with additional column like
CREATE VIEW vw_Table1
AS
SELECT *, GETDATE() as TodaysDate FROM dbo.Table1
then you can query the view like you mentioned (without column list)
SELECT * FROM vw_Table1
This will give you the date and time from the moment of the execution of the query.