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:
Date | Proper Date Value | Formatted Date Value 1 | Formatted Date Value 2 |
---|---|---|---|
10112021 | 2021-11-10 | 20211110 | 20211110 |
11112021 | 2021-11-11 | 20211111 | 20211111 |
12112021 | 2021-11-12 | 20211112 | 20211112 |
How can I set a custom date time format in Oracle SQL Developer?
You can change this in preferences:
- From Oracle SQL Developer's menu go to: Tools > Preferences.
- From the Preferences dialog, select Database > NLS from the left panel.
- From the list of NLS parameters, enter
DD-MON-RR HH24:MI:SS
into the Date Format field. - Save and close the dialog, done!
Here is a screenshot:
Related Topics
How to Find the Last Modified Date, Modified User of an Stored Procedure in SQL Server 2008
Temporary Table in SQL Server Causing ' There Is Already an Object Named' Error
Differencebetween a Candidate Key and a Primary Key
Oracle SQL:Timestamps in Where Clause
Improving Query Speed: Simple Select in Big Postgres Table
SQL Explain Plan: What Is Materialize
What Is Wrong with a Transitive Dependency
How to Describe Table in SQL Server 2008
Select All Records Don't Meet Certain Conditions in a Joined Table
Insert Default Value When Parameter Is Null
Pivot a Table on a Value But Group the Data on One Line by Another
Postgresql - Repeating Rows from Limit Offset
Postgresql Return 0 If Returned Value Is Null
A Simple Way to Sum a Result from Union in MySQL
Understanding the Differences Between Cube and Rollup
What Are the Reasons *Not* to Use a Guid for a Primary Key
Delete Top-N' Rows from a Table with Some Sorting(Order by 'Column')