Convert Data from yyyy-dd-mm to mm/dd/yyyy Issue - sql

I have a column in my table with Dates in the format yyyy-mm-dd I want to convert all the dates in that column to the format mm/dd/yyyy
I am using the below query
UPDATE Test.dbo.Status
SET DateIn = CONVERT(DATE,DateIn ,101)
The DateIn column is defined as Date in my table (DateIn DATE NULL)
The query does no change to the data. am I doing some thing wrong here?

You can change the default format in which SQL Server displays a date, but you can't alter the way a DATE value is stored via CONVERT(). You can format a date however you want if you store it as a string, but you lose functionality when you do that and it's not advisable. If you are hell-bent on storing a formatted version, you might want to create a new VARCHAR() field so you can preserve your DATE version.
You're better off formatting the date at the application level.
The reason your query does nothing is that the actual DATE values are equivalent. Notice when you take any valid date format and CAST() it as DATE the resulting format is the same regardless of the input:
SELECT CAST('20040510' AS DATE)
SELECT CAST('2004-05-10' AS DATE)
SELECT CAST('May 10, 2004' AS DATE)
All return: 2004-05-10 on my instance of SQL Server.

Related

count records in specific date format with SQL

i'm trying to define a rule that count rows where date format is dd/mm/yyyy and greater than a specific date in sql but i couldn't find the right function to use.
The second part is working : SELECT COUNT (*) FROM CUSTOMER WHERE DAT_0 >= '01/01/1995' but how could i specify the format too
In Standard SQL, the WHERE clause would look like:
WHERE DAT_0 >= DATE '1995-01-01'
Many databases would simply accept this without the DATE as well:
WHERE DAT_0 >= '1995-01-01'
In your comment DATE_0 is a date column
Therefore we can change it to any format as below using to_char then to_date
to_date(to_char(DATE_0,'yyyymmdd'),'yyyy-mm-dd')
When comparing you can try this, converting the date to_number, i find it very easy
to_number(to_char(DATE_0,'yyyymmdd')) > 19950101
if datatype of "DAT_0" is date or datetime or timestamp , they are recorded as a specific format specified by default datetime setting in your database engine , therefore you can't have multiple format inside the column , so there is no concern there

Date format value not updating from previous format to new format after update

When I update a column from a table with a date format of MMM DD,YYYY to a new format. The format doesn't change to the desired format which is YYYY-MM-DD HH:MM:SS. Even when update different value like GETDATE(). The date will change but the format will remain the same.
Current value & format from column the type is varchar
DueDate
Jun 27 2020 12:00AM
Desired format
DueDate
2020-06-27 00:00:00.000
Update statement
update TableName
set
DueDate = CAST([DueDate] AS smalldatetime),
LastSyncDateTime = GETDATE()
where CaseGuid = 'DA2CE6A1-0394-463E-8E8D-962F3A24ADC8'
There is a huge confusion between "Date displaying format" and "Date storing format". The VERY short explanation is that what you mentioned is only a client side displaying format, while SQL Server have specific format which is used for storing dates (remember that the server stores zero and one only).
You can insert dates to a table using different styles (the official name for the displaying format is STYLE), and you can present the dates in the client side using different style, but it will always be stored the same from the "SQL Server point of view" according to the DATE type which is used.
In order to solve your original needs, all that you needed to do is to provide the server the information about the style which you use in the client side (in the query). This is done by using explicit CONVERT with the third parameter, which is the STYLE.
For example if you use in the client side an Israeli format like dd/MM/yyyy, then you should use CONVERT(DATE, '27/02/2021', 103).
For more information on different STYLEs you can check this documentation.
Note: If you want to display the dates in specific format which is not covered by the existing STYLEs then you can use the function FORMAT() in your query. This function is fully flexible to return the data in your specific format. Remember that this function returns the data as string and it will not be date anymore.
For example, let's say that I want to use the format: "Day:dd,Month:MM,Year:yyyy". So if the date is '27/02/2021' then I expect to get "Day:27,Month:02,Year:2021". In this case use below:
DECLARE #D DATE
SET #D = CONVERT(DATE, '27/02/2021', 103) -- convert string to date for storing
select FORMAT(#D, 'Day:dd, Month:MM, Year:yyyy') -- convert date to string for displaying
Solution use the format function
update TableName
set
DueDate = FORMAT (CAST([DueDate] AS smalldatetime),'yyyy-MM-dd HH:mm:ss'),
LastSyncDateTime = GETDATE()
where CaseGuid = 'DA2CE6A1-0394-463E-8E8D-962F3A24ADC8'
https://www.sqlshack.com/a-comprehensive-guide-to-the-sql-format-function/

SQL query to bring back list of servers from specific date range

I'm trying to make a query with SQL Server Management Studio 2017 that brings back a count of all the servers with a projected migration date of this year. I have one query made now, but it's still bringing back some servers with dates from years before.
SELECT MONTH(Projected) as [Month], count(*) as [Total]
FROM dbo.tFake
WHERE Projected >='01/01/2019' AND Projected <='12/31/2019
GROUP BY Month(Projected)
ORDER BY [Month]
Date format is mm/dd/yyyy btw. How can I get this query to bring back just servers that are projected for the year 2019?
Going by the assumption that your data type is wrong, the first step is to fix that.
Note, I am assuming that your data only contains dates, and not time (which your query implies). Firstly, you'll need to change the value of all your rows to an convertible value, we'll go with the ISO format yyyyMMdd:
UPDATE dbo.tFake
SET Projected = CONVERT(varchar(8),CONVERT(date,Projected,101),112);
Now that all the rows are a literal string in the format yyyyMMdd we can alter the column:
ALTER TABLE dbo.tFake ALTER COLUMN Projected date;
Now, we can run your query again, but now your data type is correct, you won't have the problem:
SELECT MONTH(Projected) as [Month], count(*) as [Total]
FROM dbo.tFake
WHERE Projected >= '20190101' AND Project < '20200101' --I prefer the >= and < method. This is especially import with date & time data types
GROUP BY Month(Projected)
ORDER BY [Month];
Notice the literal strings I passed are also in the yyyyMMdd format. If you must pass a literal string in the format MMddyyyy you can wrap in a CONVERT with the style code 101: CONVERT(date,'12/31/2019',101). 101 means the US style date (CAST and CONVERT (Transact-SQL).
Remember, this solution assumes you have date only values, and not date and time values. If you do (have date and time values) you'll want to use an appropriate date and time data type and use the ISO8601 style, instead of the ISO style.

SQL Server clean up inconsistent dates

I have dates currently stored as varchar(1000) that are in inconsistent formats. I.e. yyyy-mm-dd and dd-mm-yyyy etc.
What query can I run to clean these up so they are in a consistent format, so I can convert the column datatype from varchar(1000) to date?
Thanks
This could get ugly if you have many different date formats, possibly including invalid data. One option would be to assign a new date column using a CASE expression to choose the correct format mask:
UPDATE yourTable
SET date_col = CASE WHEN date LIKE '[0-9]{2}-[0-9]{2}-[0-9]{4}'
THEN CONVERT(datetime, date, 105)
WHEN date LIKE '[0-9]{4}-[0-9]{2}-[0-9]{2}'
THEN CONVERT(datetime, date) END;
You may add conditions to the above CASE expression for other date formats.

creating table in Oracle with Date

I want to create a table in Oracle 10g and I want to specify the date format for my date column. If I use the below syntax:
create table datetest(
........
startdate date);
Then the date column will accept the date format DD-MON-YY which I dont want.
I want the syntax for my date column to be MM-DD-YYYY
Please let me know how to proceed with this.
Regards,
A DATE has no inherent format. It is not simply a string that happens to represent a date. Oracle has its own internal format for storing date values.
Formats come into play when actual date values need to be converted into strings or vice versa, which of course happens a lot since interactively we write dates out as strings.
The default date format for your database is determined by the settings NLS_DATE_FORMAT, which you probably have set to DD-MON-YYYY (which I believe is the default setting for American English locales). You can change this at the database level or for a single session for convenience, but in general it is safer programming practice to be explicit so that you don't get errors or, worse, wrong results if your code is run in a different environment.
The simplest way to specify a date value unambiguously is a date literal, which is the word 'date' followed by a string representing the date in YYYY-MM-DD format, e.g. date '2012-11-13'. The Oracle parser directly translates this into the corresponding internal date value.
If you want to use a different format, then I recommend explicitly using TO_CHAR/TO_DATE with your desired format model in your code. Examples:
INSERT INTO my_table (my_date) VALUES ( TO_DATE( '11-13-2012', 'MM-DD-YYYY' ) );
SELECT TO_CHAR( my_date, 'MM-DD-YYYY' ) FROM my_table;
dates rdo not have a format like you're suggesting. they are stored internally as a 7 byte number. to format the date when selecting, please use TO_CHAR(yourdatefield, 'format')
where formats are all shown here: http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm#i34924
eg to_char(startdate, 'mm-dd-yyyy')