How to replace date value in SQL? - sql

I am using Oracle and trying to create a view in which I will replace one date value with another date value. I am trying to use case when statement, however, I need to use the same column name as an alias and I get the error that I have duplicate column names. Do you know how to fix it or propose the alternative for replacing date value in a column and creating view? I need to keep the original table unchanged.
Code:
create view name_of_view as
select t.*,
(case when birth_date = to_date('4.12.2015', 'DD.MM.YYYY')
then to_date('4.12.1950', 'DD.MM.YYYY')
else birth_date end) as birth_date
from table t;

As #Lukasz Szozda has suggested in the comments, when you try t.* it will retrieve all columns from your table, including birth_date.
So when you add another birth_date as part of your case when, you receive the duplicate column name error.
What you need to do is:
You either change the case when column name to something like: birth_date_new or whatever then you will have both of the columns.
You retrieve all columns by their names and when retrieving birth_date you apply case when.

Related

Using variable name the same as column name - Clickhouse

I have a table mytable and mydate is a column in it.
However, since the SELECT clause is dynamic, I need to pre-adjust the column with addMinutes.
So, the column is added to the variable with the function such that in the event the column turns up in SELECT the variable is taken over than the actual value in the table.
SELECT mydate FROM "mytable"
original value of mydate is returned
WITH addMinutes(mydate,300) AS mydate SELECT mydate FROM "mytable"
original value of mydate is returned, expected to return variable value
The exact opposite happens; Even if the variable is mentioned (same name as the column), the actual column value overrides the WITH clause.
Do we have a workaround to use WITH clause variables with the same name as the columns in the table?
SELECT
* REPLACE addMinutes(mydate, 300) AS mydate, mytable.mydate
FROM mytable
I'm not aware of any workaround. The real question is: Why do you need the WITH clause to use the same variable name?
Everything works as expected if you use a different name:
WITH
addMinutes(mydate, 300) AS mynewdate
SELECT
mynewdate,
mydate
FROM mytable;

How to Update a column value with a pseudo column in SQL

I have one table in oracle 12c db where one column is varchar2 data type but the values are in DATE format. and by some mistake there are two different type of format mixed.
Basically i have to change the format there and make all values similar.
right format is "2018-11-21-02.57.26.00" but some values are like this : "11/21/2018 20:03:10.066414"
We need all values should be like right format. Need to prepare one update statement for this.
I have prepared the select statement like this. Create_TS should be updated as Create_TS_NEW only for those rows where CREATE_TS has '%/%'
I need to know if i can update those rows in table with Pseudo column like Create_TS_NEW
First Try to find out value with slash (/) then convert it into date format.
SELECT
CREATE_TS,
CASE WHEN INSTR(CREATE_TS,'/')>0 THEN TO_DATE(SUBSTR(CREATE_TS,1,19),'MM/DD/YYYY HH24:MI:SS')
ELSE TO_DATE(SUBSTR(CREATE_TS,1,19),'YYYY-MM-DD-HH24-MI-SS') end default_date_format,
to_char(CASE WHEN INSTR(CREATE_TS,'/')>0 THEN TO_DATE(SUBSTR(CREATE_TS,1,19),'MM/DD/YYYY HH24:MI:SS')
ELSE TO_DATE(SUBSTR(CREATE_TS,1,19),'YYYY-MM-DD-HH24-MI-SS') end,'YYYY-MM-DD-HH24-MI-SS') CREATE_TS_new
FROM Table_
Just put derived column in update statement that will enough to update the required format.
Update Table_ set create_ts=
to_char(CASE WHEN INSTR(CREATE_TS,'/')>0 THEN TO_DATE(SUBSTR(CREATE_TS,1,19),'MM/DD/YYYY HH24:MI:SS')
ELSE TO_DATE(SUBSTR(CREATE_TS,1,19),'YYYY-MM-DD-HH24-MI-SS') end,'YYYY-MM-DD-HH24-MI-SS') ;

How does one select all columns but rename some of them in one statement?

Normally I would write a statement like:
SELECT * FROM my_table;
But I have two columns, (both of date type), called 'created' and 'edited'. If I do select *, then the date in each of these columns will appear as:
2017-11-04T18:30:00.000Z
I would rather the date appear in DD/MON/YYYY.
To do that, I currently modify my SQL statement to:
SELECT column_name1,column_name2,column_name3,to_char(created, 'DD-MON-YYYY') as created,column_name4.... FROM my_table;
The problem is that although I can format the date, I have the problem of having to specify each column name in the statement. Is there some way I can select all the columns (but rename one or more columns using the method above), without having to specify each column name ?
You can try something like this:
select to_char(t.created,'DD-MON-YYYY') as created,to_char(t.edited,'DD-MON-YYYY') as edited, t.* from my_table t;

SQL: What does NULL as ColumnName imply

I understand that AS is used to create an alias. Therefore, it makes sense to have one long name aliased as a shorter one. However, I am seeing a SQL query NULL as ColumnName
What does this imply?
SELECT *, NULL as aColumn
Aliasing can be used in a number of ways, not just to shorten a long column name.
In this case, your example means you're returning a column that always contains NULL, and it's alias/column name is aColumn.
Aliasing can also be used when you're using computed values, such as Column1 + Column2 AS Column3.
When unioning or joining datasets using a 'Null AS [ColumnA] is a quick way to make sure create a complete dataset that can then be updated later and a new column does not need to be created in any of the source tables.
In the statement result we have a column that has all NULL values. We can refer to that column using alias.
In your case the query selects all records from table, and each result record has additional column containing only NULL values. If we want to refer to this result set and to additional column in other place in the future, we should use alias.
It means that "aColumn" has only Null values. This column could be updated with actual values later but it's an empty one when selected.
---I'm not sure if you know about SSIS, but this mechanism is useful with SSIS to add variable value to the "empty" column.
When using SELECT you can pass a value to the column directly.
So something like :
SELECT ID, Name, 'None' AS Hobbies, 0 AS NumberOfPets, NULL AS Picture, '' AS Adress
Is valid.
It can be used to format nicely a query output when using UNION/UNION ALL.
Query result can have a new column that has all NULL values. In SQL Server we can do it like this
SELECT *, CAST(NULL AS <data-type>) AS as aColumn
e.g.
SELECT *, CAST(NULL AS BIGINT) AS as aColumn
How about without using the the as
SELECT ID
, Name
, 'None' AS Hobbies
, 0 AS NumberOfPets
, NULL Picture
Usually adding NULL as [Column] name at the end of a select all is used when inserting into another table a calculated column based on the table you have just selected.
UPDATE #TempTable SET aColumn = Column1 + Column2 WHERE ...
Then exporting or saving the results to another table.

SQL replacing a field with specific text

I have the following fields for my sql view:
name | description | date
where I have to query for the max and min dates and print the new view as
status | name | description | starting
where in the status field is a text field will show longest time and shortest time. This field is completely new, and isn't built into any of the tables, how would I go in creating this field in the view?
EDIT:
I want to add the extra field status to my view, so make an extra column. As such, I have
create or replace view one (name, description, starting) as
-- SQL STUFF HERE...
from this view, I need to grab the max and min from it and union those two selections together, but add an extra column which describes whether that row has the longest time or the shortest time. To get
create or replace view two (status, name, description, starting) as
at the moment I've written up
select name, longname, max(starting) from one
union
select name, longname, min(starting) from one;
and this prints out the three columns, but I need to add the extra fourth column status but I don't know how to do this.
Just use a simple string:
select 'longest time' AS status, name, longname, max(starting) from one
union
select 'shortest time' AS status, name, longname, min(starting) from one;
Not sure what you are wanting Starting to show as you don't describe it but group by is what you are looking for and you can run aggregating functions over your date to get the information you desire. You may want a different date format so you might want to use Convert rather than cast to get the dates out in the format you want.
SELECT
CAST(MAX(Date) AS NVARCHAR(20)) + ' ,'
+ CAST(MIN(Date) AS NVARCHAR(20)) AS Status
Name
Description
FROM tblMyTables
GROUP BY
Name,
Description