To Change Date Format in SQL

How to change datetime format in query?

This is what finally worked:
I Casted the datetime and used FORMAT() with a custom format. The other solutions I tried where a little bit slower.

WHERE s.zeitpunkt
BETWEEN
CAST(
FORMAT(CAST($__timeFrom() AS DATETIME),'yyyyMMddHHmmss')
AS VARCHAR)
AND CAST(
FORMAT(CAST($__timeTo() AS DATETIME),'yyyyMMddHHmmss')
AS VARCHAR)

Convert Date format into DD/MMM/YYYY format in SQL Server

I'm not sure there is an exact match for the format you want. But you can get close with convert() and style 106. Then, replace the spaces:

SELECT replace(convert(NVARCHAR, getdate(), 106), ' ', '/')

How to change sql date format of existing records in table?

I assume the column is really a string, not a datetime.

SQL Server has a pretty flexible conversion to date. First check to be sure that all the dates convert:

select col
from t
where try_convert(date, col) is null and col is not null;

If you are satisfied with the conversion, you can do:

update t
set col = try_convert(date, col);

alter table t alter col date;

Change date format for char data type

The key problem is that you are storing your date value as a string. You should never do that because it will almost always result in problems further down the line.

Therefore to change the formatting you first have to convert your current string into a valid date and then you use convert to format it as you desire.

SELECT [Date]
-- First convert to a valid date time - the current format needs to be modified
, CONVERT(DATE, SUBSTRING([DATE], 1, 2) + '-' + SUBSTRING([DATE], 3, 2) + '-' + SUBSTRING([DATE], 5, 4), 105) [Proper Date Value]
-- Then convert back to a string in the desired format
, CONVERT(VARCHAR(8), CONVERT(DATETIME, SUBSTRING([DATE], 1, 2) + '-' + SUBSTRING([DATE], 3, 2) + '-' + SUBSTRING([DATE], 5, 4), 105), 112) [Formatted Date Value]
-- In fact you can actually just use direct string manipulation in this case
, SUBSTRING([DATE], 5, 4) + SUBSTRING([DATE], 3, 2) + SUBSTRING([DATE], 1, 2)
FROM (
VALUES
('10112021'),
('11112021'),
('12112021')
) AS D ([Date]);

Returns:































DateProper Date ValueFormatted Date Value 1Formatted Date Value 2
101120212021-11-102021111020211110
111120212021-11-112021111120211111
121120212021-11-122021111220211112

How can I set a custom date time format in Oracle SQL Developer?

You can change this in preferences:

  1. From Oracle SQL Developer's menu go to: Tools > Preferences.
  2. From the Preferences dialog, select Database > NLS from the left panel.
  3. From the list of NLS parameters, enter DD-MON-RR HH24:MI:SS into the Date Format field.
  4. Save and close the dialog, done!

Here is a screenshot:

Changing Date Format preferences in Oracle SQL Developer



Related Topics



Leave a reply



Submit