I want to format my date column in SQL Server like this Wed, 23 from given date format 4/23/2014.
Is there any way to do this...?
SQL Server version is 2008
Try like this
SELECT LEFT(DATENAME(dw, GETDATE()), 3) + ' , ' + CAST(Day(GetDate()) AS Varchar(10))
Fiddle Demo
Query would be like this
SELECT mydate,LEFT(DATENAME(dw, mydate), 3) + ' , ' + CAST(Day(mydate) AS Varchar(10)) As Date
From tbl
SQL FIDDLE
O/P
MYDATE DATE
2014-04-21 Mon ,21
2014-04-22 Tue ,22
2014-04-23 Wed ,23
2014-04-24 Thu ,24
Try this!
declare #a table(a date)
insert into #a values('4/21/2014'),('5/21/2014'),('6/21/2014')
select left(DATENAME(dw,a),3)+','+convert(varchar(10),datepart(day,a)) from #a
DEMO
select Substring(DATENAME(WEEKDAY, getdate()), 0, 4)+' '+ DATENAME(dd, getdate())
Related
Hi I am working with SQL 2014 and I need to convert the following 2 date formats:
12/31/18 - Varchar
12/31/2018 - Varchar
to this format final format
December 31,2018 Varchar
I know that use varchar is not the correct.
any suggestion?
Try to use:
DECLARE #f varchar(50) = '12/31/18'
SELECT FORMAT(CAST(#f AS DATETIME), N'MMMM dd, yyyy')
OUTPUT:
December 31, 2018
And your second variant:
DECLARE #f varchar(50) = '12/31/2018'
SELECT FORMAT(CAST(#f AS DATETIME), N'MMMM dd, yyyy')
OUTPUT:
December 31, 2018
You can try the following query also using
create table DateValue (DateVal varchar(10))
insert into DateValue values
('12/31/18'),
('12/31/2018'),
('01/31/18'),
('01/31/2018')
Select
DateName( month , DateAdd( month , DATEPART(MONTH, DateVal) , 0 ) - 1 ) + ' ' +
Cast(DATEPART(dd, DateVal) as Varchar) + ', ' +
+ Cast (DATEPART(YYYY, DateVal) as Varchar) as VarcharDate
from DateValue
The output will be as shown below.
VarcharDate
----------------
December 31, 2018
December 31, 2018
January 31, 2018
January 31, 2018
This query will also run in lower version of SQL Server where format() is not available.
Live Demo
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))
I have a problem in converting sql server 2008 datetime to varchar,
Select convert(varchar(20),convert(datetime, '2013-12-11 00:59:00.000'))
the result is Dec 11 2013 12:59AM but I need it to be actually Dec 11 2013 00:59AM as in the database dates are of 24-Hour format.
How can I correct the query?
There are two functions, cast and convert that you can use.
http://msdn.microsoft.com/en-us/library/ms187928.aspx
You forgot to put in the format style.
-- Using cast
Select cast('2013-12-11 00:59:00.000' as varchar(20)) as my_casted_date
-- Using convert
Select convert(varchar(24), '2013-12-11 00:59:00.000', 113) as my_converted_date
Use format with custom date time strings for utlitmate flexibility.
http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
-- Using format
DECLARE #my_date datetime2 = '2013-12-11 00:59:00.000';
SELECT format(#my_date, 'MMM dd yyyy HH:MM tt', 'en-us') as str_english_date
Solution that will work with SQL Server 2008.
-- Create a date time variable
DECLARE #my_date DATETIME2 = '2013-12-11 00:59:00.000';
-- Using convert
SELECT
CONVERT(varchar(24), #my_date, 113) +
CASE
WHEN DATEPART(HH, #my_date) < 12 THEN ' AM'
ELSE ' PM'
END
AS my_converted_date;
This will be perfect for you..
declare #dt datetime
set #dt='12-Jan-2014 23:59'
SELECT Right(CONVERT(VARCHAR, #dt, 100),7) AS DateTime_In_12h_Format
It's not nice but it does its job:
DECLARE #Var DATETIME;
SET #Var = '2013-12-11T00:59:00.000';
SELECT
#Var AS SourceValue,
CONVERT(VARCHAR(50), #Var, 100) AS FormatedValue1,
CASE
WHEN CONVERT(VARCHAR(50), #Var, 100) LIKE '%[ ]12:__AM' THEN REPLACE(CONVERT(VARCHAR(50), #Var, 100), ' 12:', ' 00:')
ELSE CONVERT(VARCHAR(50), #Var, 100)
END AS FormatedValue2
SourceValue FormatedValue1 FormatedValue2
----------------------- ------------------- -------------------
2013-12-11 00:59:00.000 Dec 11 2013 12:59AM Dec 11 2013 00:59AM
Please try this
Select convert(varchar(20),convert(datetime, '2013-12-11 00:59:00.000'),113) + ' ' +RIGHT(CONVERT(VARCHAR(26), Convert(datetime,'2013-12-11 00:59:00.000'), 109), 2)
Output
11 Dec 2013 00:59:00 AM
Fiddle Demo
How do I use sql to get the whole month name in sql server?
I did't find a way using DATEPART(mm, mydate) or CONVERT(VARCHAR(12), CreatedFor, 107).
Basically I need in the format: April 1 2009.
SELECT DATENAME(MONTH, GETDATE())
+ RIGHT(CONVERT(VARCHAR(12), GETDATE(), 107), 9) AS [Month DD, YYYY]
OR Date without Comma Between date and year, you can use the following
SELECT DATENAME(MONTH, GETDATE()) + ' ' + CAST(DAY(GETDATE()) AS VARCHAR(2))
+ ' ' + CAST(YEAR(GETDATE()) AS VARCHAR(4)) AS [Month DD YYYY]
If you are using SQL Server 2012 or later, you can use:
SELECT FORMAT(MyDate, 'MMMM dd yyyy')
You can view the documentation for more information on the format.
Most answers are a bit more complicated than necessary, or don't provide the exact format requested.
select Format(getdate(), 'MMMM dd yyyy') --returns 'October 01 2020', note the leading zero
select Format(getdate(), 'MMMM d yyyy') --returns the desired format with out the leading zero: 'October 1 2020'
If you want a comma, as you normally would, use:
select Format(getdate(), 'MMMM d, yyyy') --returns 'October 1, 2020'
Note: even though there is only one 'd' for the day, it will become a 2 digit day when needed.
109 - mon dd yyyy (In SQL conversion)
The required format is April 1 2009
so
SELECT DATENAME(MONTH, GETDATE()) + RIGHT(CONVERT(VARCHAR(12), GETDATE(), 109), 9)
Result is:
select datename(DAY,GETDATE()) +'-'+ datename(MONTH,GETDATE()) +'- '+
datename(YEAR,GETDATE()) as 'yourcolumnname'
In one of my db table, there is a column FinalDate which will store the date and the data type is not datetime but varchar. I would like to write a query that I can select distinct FinalDate and group/display like Jun 2012, Jul 2012.
Values for the FinalDate column would be something like below:
20120213
20120225
20120218
20120306
20120320
So, how I can write a query to select distinct of the FinalDate and display them in:
Feb 2012
Mar 2012
Declare #a table (d varchar(8))
insert into #a Values ('20120213'),('20120225'),('20120218'),('20120306'),('20120320');
Select FinalDate
from
(
select Distinct
--DateName(Month,d)+' '+CAST(Datepart(yy,d) as Varchar(4)) as FinalDate
SubString(DateName(Month,d),1,3)+' '+CAST(Datepart(yy,d) as Varchar(4)) as FinalDate
,Datepart(yy,d) as yy,Datepart(mm,d) as mm
from
(Select CAST(d as datetime) as d from #a) a
) b
Order by yy,mm
Try this one -
Query:
DECLARE #temp TABLE (t VARCHAR(8))
INSERT INTO #temp VALUES
('20120213'),
('20120225'),
('20120218'),
('20120306'),
('20120320')
SELECT LEFT(DATENAME(MONTH, t), 3) + ' ' + y
FROM (
SELECT DISTINCT
t = CAST(LEFT(t, 6) + '01' AS DATETIME)
, y = LEFT(t, 4)
FROM #temp
) t
ORDER BY t
Output:
Feb 2012
Mar 2012
try below query
SELECT
Count (SUBSTRING(CONVERT(char(12), CONVERT(date, StringDate,112),113),4,12)) as counts,
SUBSTRING(CONVERT(char(12), CONVERT(date, StringDate,112),113),4,12) from Employees
group by SUBSTRING(CONVERT(char(12), CONVERT(date, StringDate,112),113),4,12)
output
counts (No column name)
1 Apr 1992
1 Aug 1992
1 Jan 1994
1 Mar 1994
1 May 1992
1 May 1993
1 Nov 1994
2 Oct 1993
Here's a CTE based approach. Replace [[[TABLE_NAME]]] with the actual name of the table that contains the FinalDate field. I did the processing in the final select rather than inside of the CTE to prevent any chance of SQL Server performing the processing on filtered out (non-seq 1) rows.
WITH a AS (
SELECT ROW_NUMBER() OVER (PARTITION BY LEFT(FinalDate, 6) ORDER BY FinalDate) seq, FinalDate
FROM [[[TABLE_NAME]]]
) SELECT LEFT(DATENAME(mm, FinalDate), 3) + ' ' + LEFT(FinalDate, 4)
FROM a
WHERE seq = 1