SQL Server Format Date DD.MM.YYYY HH:MM:SS - sql-server

How can I get a date like 'dd.mm.yyyy HH:MM:SS'
I tried the following but it's not exactly what I'm looking for...
select convert(varchar(20),getdate(),113)
result: 14 Jul 2011 09:23:57
Thanks a lot
Largo

See http://msdn.microsoft.com/en-us/library/ms187928.aspx
You can concatenate it:
SELECT CONVERT(VARCHAR(10), GETDATE(), 104) + ' ' + CONVERT(VARCHAR(8), GETDATE(), 108)

A quick way to do it in sql server 2012 is as follows:
SELECT FORMAT(GETDATE() , 'dd/MM/yyyy HH:mm:ss')

You can learn datetime formatting in sql server here
http://www.sql-server-helper.com/tips/date-formats.aspx
http://yrbyogi.wordpress.com/2009/11/16/date-and-time-types-in-sql-server/

CONVERT(VARCHAR,GETDATE(),120)

Related

How to convert YYYY-MM-DD HH:MI:SS format to MM-DD-YYYY HH:MI:SS in MSSQL

How to convert getdate() function to MM-DD-YYYY HH:MI:SS format in MSSQL.
Use the FORMAT function:
SELECT FORMAT ( GETDATE(), 'MM-dd-yyyy HH:mm:ss')
FORMAT() function is available from SQL SERVER 2012 or Above Versions, if you want to use for older SQL SERVER Versions use this Query
SELECT convert(varchar,getdate(), 110) + ' ' + convert(varchar, getdate(), 108)
Use this link for reference on how to convert to other formats

Format date as 'DD Month YYYY'

Just have a mssql query that has a date in the format:
'2016-03-22 00:00:00.000'
I need to format it as:
'22 March 2016'
I'm using SQL Server 2012. I've tried googling and the usual 106, 112 codes don't seem to work.
Is there a specific code format I can use?
Try the FORMAT function:
SELECT FORMAT(GETDATE(), 'D', 'en-gb')
If your version does not support the FORMAT function, you can do it by concatenating the date parts:
SELECT
RIGHT('00' + CAST(DATEPART(DAY, GETDATE()) AS VARCHAR(2)), 2) + ' ' +
DATENAME(MONTH, GETDATE()) + ' ' +
CAST(DATEPART(YEAR, GETDATE()) AS VARCHAR(4))
Use Format Function in SQL Server 2012
SELECT FORMAT(GETDATE(),'dd-MMMM-yyyy')
See Image for your datatype ref
Try this query:
SELECT CONVERT(VARCHAR, CONVERT(DATETIME, '2016-03-22 00:00:00.000'), 106)
You need to convert into DATETIME then you will get your desired output
More Datetime Sql formate
SQLFiddle
This may help you
Try Convert function for SQL server:
select convert(varchar(11),cast('2016-03-22 00:00:00.000' as datetime), 106)
How to change day, month, year in PHP:
echo $purchase_date=date('F-Y',strtotime('2022-03-22 00:00:00.000'));
Answer:
March-2022

Date Format in SQL dd/mm/yyyy hr:mm:ss AM/PM

What is the format to be used in SQL to get date in format like
dd/mm/yyyy hr:mm:ss AM/PM
eg: 9/17/2013 4:55:43 PM
The link below gives an answer but i want value in seconds also
How to display Date in DD/MM/YYYY H:MM AM/PM format in SQL Server
Instead of 101 in below query.
convert(varchar, CONVERT(smalldatetime, PhaseStartDate), 101)
You can check both posts below...
They convert to mm/dd instead of dd/mm
but to get what you want, just change to 103 instead of 101 ;)
Link 1
Link 2
select convert(varchar(10), GETDATE(), 103 /*or 101*/) + ' ' +
ltrim(right(convert(varchar(20), GETDATE(), 22), 11))
Probably you meant 103 or mm/dd/yyyy instead. You could craft that yourself from the pieces or combine and craft from two or more styles.
IMHO, that should be something you do in your front end.
CONVERT(VARCHAR(10), PhaseStartDate, 101) + Substring((CONVERT(VARCHAR(20), PhaseStartDate, 22)),9,12) as PhaseStartDate
Useful Link is here
http://www.sql-server-helper.com/sql-server-2008/sql-server-2008-date-format.aspx
I assume that you're not using SQL Server 2005.
here is your query
SELECT
CONVERT(VARCHAR,GETDATE(),103) +' '+
CONVERT(VARCHAR(8),CAST( GETDATE() AS TIME ))+' '+
SUBSTRING(CONVERT(VARCHAR(30), GETDATE(), 9), 25, 2)

How can I convert SQL Server date to custom formats

SELECT GETDATE() AS CurrentDateTime
2015-08-30 19:22:24.830
I need this output (in SQL Server)
2015-08-30 19
2015-08
SELECT
CONVERT(VARCHAR(13), GETDATE(), 120) as "YYYY-MM-DD hh 1"
, CONVERT(VARCHAR(7), GETDATE(), 120) as "YYYY-MM 1"
, FORMAT(getdate(),'yyyy-MM-dd hh') as "YYYY-MM-DD hh 2"
, FORMAT(getdate(),'yyyy-MM') as "YYYY-MM 2"
;
For any version of SQL Server you can use CONVERT(VARCHAR(),datecol,styleno)
just learn the relevant style numbers and adjust the length of the varchar
For SQL Server 2012 onward it's easier with the FORMAT() function
Try this
SELECT CONVERT(VARCHAR(13), GETDATE(), 120)
SELECT CONVERT(VARCHAR(7), GETDATE(), 120)

Remove time from DateTime sql server 2005

I need date part from datetime. in format of "dd-mm-yyyy"
I have tried follwoing
Query:
select Convert(varchar(11), getdate(),101)
Output:
01/11/2011
Query
SELECT cast(floor(cast(GETDATE() as float)) as datetime)
Output
2011-01-11 00:00:00.000
Query:
SELECT
CONVERT(VARCHAR(MAX),DATENAME(DD,GETDATE())) + '-' +
CONVERT(VARCHAR(MAX),DATEPART(MONTH,GETDATE())) + '-' +
CONVERT(VARCHAR(MAX),DATENAME(YYYY,GETDATE())) `
Output:
11-1-2011 i.e. "d-m-yyyy"
I required output in "dd-mm-yyyy" format.
SELECT CONVERT(VARCHAR(10),GETDATE(),105)
Try:
SELECT convert(varchar, getdate(), 105)
More here.
Here you can find some examples how to do this: http://blog.pengoworks.com/index.cfm/2009/1/9/Useful-tips-and-tricks-for-dealing-with-datetime-in-SQL
Using the CONVERT function "works" but only if you're comparing strings with strings. To compare dates effectively, you really need to keep the SMALLDATETIME data type strongly typed on both side of the equation (ie "="). Therefore 'apros' comment above is really the best answer here because the blog mentioned has the right formulas to use to strip off the time component by "flattening" it to midnight (ie 12:00:00) via rounding and any date column in SQL Server 2005 will always default to 12:00:00 if the date is given without a time.
This worked for me ...
select dateadd(day, datediff(day, '20000101', #date), '20000101')

Resources