Create a new date format in SQL Server - sql

I want my date to show as MM.dd.yyyy. But in SQL Server there is no such format to format your date in this format. What I can do is:
select replace(convert(varchar(10), getdate(), 101), '/', '.')
It will give me the expected output. But my question is: can we create a new format all together to display this kind of data without replace?
We have a dateformat options from 0 to 131 in SQL Server. Can we create new format like 132 in SQL Server?

You can use SQL Server format() function.
select format(getdate(), 'MM.dd.yyyy') as date

Related

SQL convert String date of style d-MMM-yyyy to DATE

I'm importing a CSV file and one of the fields is a non-standard date string with entries like 7-Dec-2021
Any ideas for to convert this into a DATETIME object that I can insert into my SQL table? Standard CAST/CONVERT didn't work.
Microsoft SQL Server 2012 - 11.0.2100.60
TRY_CONVERT seems to be working here:
SELECT TRY_CONVERT(datetime, '7-Dec-2021'); -- 2021-12-07 00:00:00.000
Edit:
Your input date almost matches format mask 106, once we replace the dashes with spaces. Consider this solution:
SELECT dt, CONVERT(datetime, REPLACE(dt, '-', ' '), 106) AS dt_out
FROM yourTable;
This outputs 2021-12-07 00:00:00.000 for dt_out on SQL Server 2014, and should behave the same way on SQL Server 2012.
I am guessing that you are using a LOGIN where its language isn't English based, and as a result 'Dec' (and/or other months) isn't recognised as a valid month name.
You can specify your language to be used for the batch and then CONVERT:
SET LANGUAGE BRITISH;
SELECT TRY_CONVERT(date,'7-Dec-2021',106);

Get DDMMYYYY format in SQL Server without periods or hyphens

I have a little task to do and I need to get ddmmyyyy format for my DateTime values in SQL Server.
I tried to do it with this code
SELECT convert(varchar, getdate(), 104)
and it works fine, but it returns dd.mm.yyyy or 01.11.2021. I need it to be 01112021 but I can't achieve this yet.
I am using this link for getting datetime format codes but I can't find any code that really helps me achieve my goal.
There's no exact match for the required format you need - you can either use
SELECT REPLACE(CONVERT(varchar(20), SYSDATETIME(), 104), '.', '')
(use what you had and just strip out the . after first step of formatting)
or you can use FORMAT which is available in SQL Server 2012 and newer:
SELECT FORMAT(SYSDATETIME(), 'ddMMyyyy')
There is no defined format that fulfills your request. However, you can use the 'Replace' function to strip your value from date separators.
select replace(convert(varchar, getdate(),104),'.','')
OR use the newer FORMAT function (SQL server 2012+)
SELECT FORMAT(getdate(), 'ddMMyyyy')
You can use replace function to complete it.
select replace(convert(varchar, getdate(), 104),'.','')

How to get datepart from datetime in a Column

I want to convert the column (EXECUTION_LOCAL_DATE_TIME) which has datetime format as (YYYY-MM-DD hh:mm:ss.nnnnnnn) to format (YYYY-MM-DD). Ho do i get this. I am working on SQL server management studio
If your intention is to just get the DATE part of a DATETIME then you can just convert the format to DATE (note that this will return a 'Date' datatype, not specifically formatted to a string 'YYYY-MM-DD'.)
eg:
DECLARE #Dt DATETIME = '2019-01-25T12:00:00'
SELECT CONVERT(DATE, #Dt)
Will return '2019-01-25'
You want the CAST() function:
https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-2017
Ideally you should return the datetime format from your query and let your presentation layer handle formatting.
However if you are using SQL server 2012 or higher then you can use the Format() function. See the below answer:
Convert Date format into DD/MMM/YYYY format in SQL Server
If you're using SQL Server 2012 or higher you can use the FORMAT() function.
In your case you'd need
SELECT FORMAT(EXECUTION_LOCAL_DATE_TIME, 'yyyy-MM-dd') FROM TABLE_NAME
You can find additional info here
https://www.mssqltips.com/sqlservertip/2655/format-sql-server-dates-with-format-function/
https://learn.microsoft.com/en-us/sql/t-sql/functions/format-transact-sql?view=sql-server-2017
Here Is the Code Worked For me
SELECT convert(varchar, EXECUTION_LOCAL_DATE_TIME, 111) from Tablename
The Execution_Local_Date_Time will be Converted to yyyy/mm/dd format.

Change Dateformat for nvarchar by sql script

I have to change the datetime format for a given string in a SQL query.
The database is SQL Server 2005, the column is of type nvarchar(1024).
Atm the data is like 2014-05-21, given the date from today as example.
I should write a script in which I can convert from 2014-05-21 to 21.05.2014.
What will be the fastest / easiest way to do this?
You can give a try as below :-
SELECT CONVERT(VARCHAR(10), GETDATE(), 104) AS [DD.MM.YYYY]
For more information :-
http://www.sql-server-helper.com/tips/date-formats.aspx

How to convert varchar to date

I am working in SQL Server 2008 R2
I need to convert a varchar(50) field in a view to a date format.
I have a view that uses the following to create the field delivered_date:
convert(varchar(50),[delvd_dt],110) as [delivered_date]
The formatted field looks like : 2012-03-11 16:24:42.0000000
I need the results to be a date so that I can check for dates within a range. I would prefer to do it within the view so that I can use SSRS to create the range for the report.
Have you tried cast()? The following returns the right date for me:
select CAST('2012-03-11 16:24:42.0000000' as DATE)
Perhaps you can simply truncate the millis and use CONVERT:
SELECT Convert(datetime, LEFT('2012-03-11 16:24:42.0000000', 19), 102) AS [Date]
Demo
This works even in MS SQL-Server 2005.
CAST and CONVERT (Transact-SQL)
Try this link website shows several formatting options.
Example:
SELECT CONVERT(VARCHAR(10), GETDATE(), 105)