I'm currently performing a union across 4 different tables in which the dates are currently a combination of date and datetime fields.
If I use "Format" to change the dates, it'll turn the date column into an nvarchar data type which I do not want.
How can I then, format each of the dates in each of the tables for uniformity while maintaining the data type as date in the outputted view?
Below is the SQL script I used previously, which turned my dates into an nvarchar data type
FORMAT(Date,'MM/dd/yy') 'Date'
The final data type would be date
The correct query for my question is convert(date,yourfields) per George Menoutis
Related
I have two tables, one that was created using a connection to teradata and other one that was created importing an excel file.
I need to find the records that are on one table but not on the other given three of the fields. The first two are returning what i would expect but when i add the third field which is a date field then none of the records match. Wen i look into the tables i see that the two dates are identical but somehow SAS does not consider them to be identical.
My code looks like this:
PROC SQL;
CREATE TABLE fields_that_do_not_match AS
SELECT /*FIRST TWO FIELDS*/ date_a FROM table_a
EXCEPT
SELECT /*FIRST TWO FIELDS*/ date_b FROM table_b;
QUIT;
Is there something else i should be considering for comparing dates?
When i see the properties of the date fields both are on DATE9. format, are of numerical type and have 8 bytes in length. Both of the dates show 14FEB2022 when i query the table but i don't know if some of the tables have aditional information that is not being displayed due to the format.
Thanks in advance.
The reason why the dates did not match was because one of them had decimal values and i needed to round the values. I added INT(DATE_A) and it worked after that.
I have oracle query which should remove the duplicate records from a date column which consists of time as well. Because of time-stamp there are duplicate records are showing when include other columns along with the date columns. please see attached image from power bi. Is there any way I could be get rid of duplicacy of records.
Select distinct
to_date(to_char(JOB_CLOSED_DATE,'dd-mon-yy'))
From DWH_FACT_DISCRETE_JOB_WIP
First, implicit casting from char to date is very dangerous in oracle - it can result in hard to find bugs.
Second, try to use trunc() function instead of to_char() to get date without time.
Select distinct trunc(JOB_CLOSED_DATE)
From DWH_FACT_DISCRETE_JOB_WIP
I have a group of two SQL tables in postgres. A staging table and the main table. Among the variety of reasons for the staging table, the data i am uploading has irregular and different formats for all of the date columns. During the upload process these values go into staging table as varchars to be manipulated into usable formats.
In the main table the column type for the date fields is of type 'date' in the staging table they are of type varchar.
The question is, does postgres support a copy expression similar to
insert into production_t select *,textdate::date from staging_t
I need to change the format of a single field during the copy process. I know i can individually type out all of the column names during the insert and typecast the date columns there, but this table has over 200 columns and is one of 10 tables with similar issues. I want to accomplish this insert+typecast in one line that i can apply to all tables rather than having to type 2000+ lines of sql queries.
You have to write every column of such a query, there is no shorthand.
May I say that a design with 200 columns is questionable.
Im working on sql server 2005, i have a column 'Date' of datatype datetime and want to extract month and year separately and store it in two separate columns in the same table as 'Date'. How do i do this and make it happen automatically everytime a new entry is entered where i dont have to manually input month and date.
You can use computed columns or trigger.
I recommend to use computed columns. It's very easy to implement and maintenance.
To get year and month use YEAR and MONTH functions
Computed Columns in SQL Server
DateTime functions in SQL Server
I am working on an application the stores time values in a database e.g Expected time of Arrival and actual time of arrival. What is the best way of storing these fields in a database?
What SQL query can I use to obtain the time difference between the two fields?
If you're on SQL Server 2008, use the TIME datatype. Declare those two fields
ExpectedTimeOfArrival TIME(7),
ActualTimeOfArrival TIME(7)
Read up on how to manipulate the TIME datatype here.
Once you have those two fields in place, you could add a computed column to your table that calculates the difference between those two, e.g. in minutes:
ALTER TABLE dbo.YourTable
ADD TimeDifference AS DATEDIFF(MINUTE, ActualTimeOfArrival, ExpectedTimeOfArrival)
and then you could query o that new column TimeDifference as if it were a normal, regular table column.