Date Conversion in SQL Server - sql

I have a date in following format
'2013-09-22 00:00:00.000'
I need this in 22-09-2013, Sunday and 22-Sep-2013, Sunday formats. I dint find any convert function to get days along with.
Thanks

I think you need to use CONVERT() and DATENAME() functions like below :
1) select CONVERT(varchar(20), getdate(),105) + ',' + datename(DW, getdate())
2) select REPLACE(CONVERT(varchar(20), getdate(),106),' ','-') + ',' + datename(DW, getdate())
Result is
07-11-2013, Thursday and 07-Nov-2013, Thursday

SELECT REPLACE(REPLACE(CONVERT(VARCHAR(11), GETDATE(), 103), ' ', '-'), '/', '-') + ', ' + datename(dw,getdate()) AS [DD-Mon-YYYY];
And
SELECT REPLACE(REPLACE(CONVERT(VARCHAR(11), GETDATE(), 106), ' ', '-'), '/', '-') + ', ' + datename(dw,getdate()) AS [DD-Mon-YYYY];
Output:
07-11-2013, Thursday and 07-Nov-2013, Thursday
(You might not need the first REPLACE function. Without it, the second date for me showed up as 07/11/2013, Thursday. The replace simply replaces the '/' with '-' like you specified).
I agree with others suggesting that the conversion should be handled in the client application, though. Especially if the application will be used in different timezones. Hard-coding the date format in SQL then would possibly lead to confusion.

You can write as:
SELECT CONVERT(VARCHAR(10), GETDATE(), 105) + ', '+ DATENAME (weekday ,GETDATE()) AS [DD-MM-YYYY, DAY],
REPLACE(CONVERT(VARCHAR(11), GETDATE(), 106), ' ', '-') + ', '+ DATENAME (weekday ,GETDATE()) AS [DD-Mon-YYYY, DAY]

Dates stored in SQL-Server without any format. It is client application (e.g. ssms) formats a date.
If you need to format date on server side you can convert it into varchar using CAST or CONVERT functions.
SELECT CONVERT(varchar(50),#date,110)+', '+ DATENAME(weekday, GETDATE())

Related

SQL Server - converting date to D-MMM-YYYY format

How can I get the string from GETDATE() in D-MMM-YYYY format, e.g 3 May 2016
If I use CONVERT(VARCHAR, GETDATE(), 106), I would get a leading zero on day which is not what I want.
If you are on SQL Server 2012 or later, use FORMAT:
SELECT FORMAT(GETDATE(), 'd MMM yyyy')
Edit: some of the answers below are just flat-out wrong so I'm adding a solution for older versions of SQL Server. 2005 is the earliest that I can get my hands on:
SELECT CASE
WHEN CONVERT(varchar(20), GETDATE(), 106) LIKE '0%'
THEN SUBSTRING(CONVERT(varchar(20), GETDATE(), 106), 2, 20)
ELSE CONVERT(varchar(20), GETDATE(), 106)
END
SELECT case when left(convert(varchar(20),[DateColumn],106),1) ='0'
then right(convert(varchar(20),[DateColumn],106),len(convert(varchar(20),[DateColumn],106))-1)
else convert(varchar(20),[DateColumn],106)
end
FROM [DB].[dbo].[Table]
Some sample output :
29 Apr 2016
2 Apr 2016
If you cannot use FORMAT (Below SQL Server 2012)
DECLARE #date DATE = '20160503'
SELECT REPLACE(DATEPART(DAY, #date),' 0','') + ' ' +
CONVERT(CHAR(3), #date, 0) + ' ' +
CAST(DATEPART(YEAR, #date) AS CHAR(4))

Manipulating the GETDATE function to display DD MM YY

I have been trying to manipluate the getDatefunction in SQL but I want the date to come out as todays date and look exactly like this 07-Jan-15 but I cant't get it to work and I have tried everything!
What I have at the minute is:
convert(varchar(104), GETDATE()) as [Date]
but that is giving me Jan 7 2015 1:52PM and thats not what I'm looking for! I have tried all the convert options! So any help with this matter would be appreciated
Try
SELECT replace(convert(NVARCHAR, GETDATE(), 106), ' ', '-') as [Date]
Output:- 07-Jan-2015
OR
SELECT replace(LEFT(convert(NVARCHAR, getdate(), 106),6) + '-' +
RIGHT(year(convert(NVARCHAR, getdate(), 106)),2), ' ', '-') as [Date]
Output:- 07-Jan-15

How do I get the month and day with leading 0's in SQL? (e.g. 9 => 09)

DECLARE #day CHAR(2)
SET #day = DATEPART(DAY, GETDATE())
PRINT #day
If today was the 9th of December, the above would print "9".
I want to print "09". How do I go about doing this?
Pad it with 00 and take the right 2:
DECLARE #day CHAR(2)
SET #day = RIGHT('00' + CONVERT(NVARCHAR(2), DATEPART(DAY, GETDATE())), 2)
print #day
For SQL Server 2012 and up , with leading zeroes:
SELECT FORMAT(GETDATE(),'MM')
without:
SELECT MONTH(GETDATE())
Use SQL Server's date styles to pre-format your date values.
SELECT
CONVERT(varchar(2), GETDATE(), 101) AS monthLeadingZero -- Date Style 101 = mm/dd/yyyy
,CONVERT(varchar(2), GETDATE(), 103) AS dayLeadingZero -- Date Style 103 = dd/mm/yyyy
Try this :
SELECT CONVERT(varchar(2), GETDATE(), 101)
Leading 0 day
SELECT FORMAT(GetDate(), 'dd')
SQL Server 2012+ (for both month and day):
SELECT FORMAT(GetDate(),'MMdd')
If you decide you want the year too, use:
SELECT FORMAT(GetDate(),'yyyyMMdd')
Select Replicate('0',2 - DataLength(Convert(VarChar(2),DatePart(DAY, GetDate()))) + Convert(VarChar(2),DatePart(DAY, GetDate())
Far neater, he says after removing tongue from cheek.
Usually when you have to start doing this sort of thing in SQL, you need switch from can I, to should I.
SELECT RIGHT('0'
+ CONVERT(VARCHAR(2), Month( column_name )), 2)
FROM table
Might I suggest this user defined function if this what you are going for:
CREATE FUNCTION dbo.date_code (#my_date date) RETURNS INT
BEGIN;
DECLARE #retval int;
SELECT #retval = CAST(CAST(datepart(year,#my_date) AS nvarchar(4))
+ CONVERT(CHAR(2),#my_date, 101)
+ CONVERT(CHAR(2),#my_date, 103) AS int);
RETURN #retval;
END
go
To call it:
SELECT dbo.date_code(getdate())
It returns as of today
20211129
Roll your own method
This is a generic approach for left padding anything. The concept is to use REPLICATE to create a version which is nothing but the padded value. Then concatenate it with the actual value, using a isnull/coalesce call if the data is NULLable. You now have a string that is double the target size to exactly the target length or somewhere in between. Now simply sheer off the N right-most characters and you have a left padded string.
SELECT RIGHT(REPLICATE('0', 2) + CAST(DATEPART(DAY, '2012-12-09') AS varchar(2)), 2) AS leftpadded_day
Go native
The CONVERT function offers various methods for obtaining pre-formatted dates. Format 103 specifies dd which means leading zero preserved so all that one needs to do is slice out the first 2 characters.
SELECT CONVERT(char(2), CAST('2012-12-09' AS datetime), 103) AS convert_day
DECLARE #day CHAR(2)
SET #day = right('0'+ cast(day(getdate())as nvarchar(2)),2)
print #day
use
CONVERT(CHAR(2), DATE_COLUMN, 101)
to get the month part with 2 characters and
CONVERT(CHAR(2), DATE_COLUMN, 103)
for the day part.
Declare #dateToGet varchar(10)
Set #dateToGet = convert(varchar, getdate(), 112)
This works fine for the whole date with leading zeros in month and day
select
right('0000' + cast(datepart(year, GETDATE()) as varchar(4)), 4) + '-'+ +
right('00' + cast(datepart(month, GETDATE()) as varchar(2)), 2) + '-'+ +
right('00' + cast(datepart(day, getdate()) as varchar(2)), 2) as YearMonthDay

With SQL Server format date as '%Y-%m-%d %I:%M%P' / '2012-04-05 11:56am'

I've always found SQL Server date formatting to be counter intuitive. I am pretty sure I already know the only answer is to use a slew of convert and string functions but thought I would ask just in case.
How do you get SQL Server to format a datetime to look like:
%Y-%m-%d %I:%M%P or '2012-04-05 11:56am'
My current approach involves pulling back data with SELECT CONVERT(VARCHAR(19), GETDATE(), 120) and then looping server/client side in code. This I find inefficient in some scenarios when exporting data say to CSV.
DECLARE #d AS datetime;
SET #d = '2012-04-04 16:43:00'
SELECT LEFT(CONVERT(VARCHAR, #d, 120), 11)
+ RIGHT('0' + LTRIM(SUBSTRING(CONVERT(VARCHAR, #d, 131), 12, 5)), 5)
+ LOWER(RIGHT(CONVERT(VARCHAR, #d, 131), 2))
--2012-04-04 04:43pm
select
cast(datepart(YYYY, GETDATE()) as varchar) + '-' +
right('0' + cast(datepart(MM, GETDATE()) as varchar), 2) + '-' +
right('0' + cast(datepart(DD, GETDATE()) as varchar), 2) + ' ' +
LTRIM(RIGHT(cast(GETDATE() AS varchar), 8))
I think the real answer is "You don't", unless you upgrade your SQL Server instances to SQL Server 2012.
FORMAT (Transact-SQL)

Date Format conversion in SQL

I want to convert date format from 01/09 to January 2009 , 09/03 to September 2003 etc. Is this possible in SQL? Please let me know if there is a API for the same.
if you have a DateTime column in your table, it's possible
SELECT DATENAME(MM, YOUR_DATE_COLUMN) + ' ' + CAST(YEAR(YOUR_DATE_COLUMN) AS VARCHAR(4)) AS [Month YYYY]
http://www.sql-server-helper.com/tips/date-formats.aspx
You should look here.
It's rather simple.
You should first convert it to a datetime. Then you can easily apply any formating when you read it later.
declare #d varchar(10);
set #d = '01/09'
select
--cast(#d as datetime) as d1, --syntax error converting char string
cast('20' + right(#d, 2) + '-' + left(#d, 2) + '-01' as datetime) as d2
then convert it to mmm yyyy using rm's answer
select datename(month, GETDATE()) + ' '+ substring(convert(varchar, GETDATE(), 100),8,4)