Change format of datetime in SQL - sql

I want format like
17-April-2011 9:05 PM
How to do this in sql query

Use the CONVERT or CAST function (see documentation) which offer a number of formats. But be careful with what you do with it. This is fine for displaying, but could result in inefficient queries if you use the result in a query expression.
And if you are using it for display, you will probably have better formatting options in the language you are using at the UI level.

Try CONVERT(varchar(50), datecol, 130) - format 130 seems closest and replace some spaces?

The closest you can get from the built-in CONVERT function is 17 Apr 2011 9:05:00:000PM:
SELECT convert(varchar, getdate(), 130)
See also:
How to format datetime & date in Sql Server 2005 by Anubhav Goyal
CAST and CONVERT (Transact-SQL) on MSDN

113 is also similar:
SELECT convert(varchar, getdate(), 113)

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.

How to get date from sql server in user specified format

I want get date from sql server in user specified format using GETDATE() function.
if i give this query
select GETDATE()
then it is displating output date in this format
2015-03-17 07:29:58.377
but i want output like this .
2015-03-17
what statement should be added with query to get the result.
help me from this problem.
Just use convert():
select convert(varchar(10), getdate(), 121)
Just have a look to the following link. which provides more conversion options.
https://msdn.microsoft.com/en-us/library/ms187928.aspx
In SQL Server 2012 onward you may use FORMAT() which is a bit more intuitive than recalling style numbers.
e.g.
select FORMAT( GETDATE() , 'yyyy-MM-dd' )
see: https://msdn.microsoft.com/en-AU/library/hh213505.aspx
however do note the second parameter is case sensitive
'yyyy-MM-dd' works
'YYYY-MM-DD' would only work for the MM

How to format dates in microsoft access queries

SELECT convert(varchar, getdate(), 101)
In Microsoft SQL Server, the above query returns the current date in the format
mm/dd/yyyy - 05/01/2014
What is its Microsoft Access query equivalent? I have tried CDate(MyDateValue) but this does not retain leading zeroes. In the example above, it returns
5/1/2014
but i need the leading zeroes.
try using
format
select Format (#05/01/2014#, "mm/dd/yyyy")
more info here
Try this
select convert(date,getdate(),101)