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
Related
I need to convert this string to yyyy-MM-dd date format:
December 31, 2014 to 2014-12-31
May 31 , 2018 to 2018-05-31
Any suggestion?
Regards!
You can just use convert():
select convert(date, 'December 31, 2014')
SQL Server is pretty good about doing such conversions.
So this works in both cases:
select convert(date, datestr)
from (values ('December 31, 2014'), ('May 31 , 2018')) v(datestr);
You can also use as shown Here
Select cast('December 31, 2014' as date) as [Date]
Select cast('December 31, 2014' as date) as [Date]
or in higher version of SQL Server provide format string value for different date format as you want.
SELECT FORMAT(cast('December 31, 2014' as Date),'yyyy-MM-dd','en-US') AS[DATE IN US FORMAT]
I have a varchar column which has values like "Aug 07 2017, 04:14 AM,EDT".
I need to convert this to a date column so that its possible to take the maximum value of the date.
I tried this:
select CONVERT(datetime, #dateField, 108)
from table
But I am getting the following error:
Conversion failed when converting date and/or time from character string.
You can just use left() and convert():
select convert(date, left('Aug 07 2017, 04:14 AM,EDT', 11))
If you want a datetime then convert the date and time separately, then:
select ( convert(datetime,
left('Aug 07 2017, 04:14 AM,EDT', 11)
) +
convert(datetime,
convert(time,
substring('Aug 07 2017, 04:14 AM,EDT', 14, 8)
)
)
)
Note: This is not taking the time zone into account.
Better Datetime solution
DECLARE #dateField AS NVARCHAR(50) = 'Aug 07 2017, 04:14,EDT'
-- Get index of last comma
DECLARE #validDateField AS NVARCHAR(20) = REPLACE(LEFT(#dateField, LEN(#dateField)- CHARINDEX(',', reverse(#dateField))), ',','')
SELECT CONVERT(DATETIME, #validDateField, 108)
Here is a solution:
select CONVERT(datetime, REPLACE(LEFT(#dateField, LEN(#dateField) - 3),',','') ,108 )
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())
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'
I'm trying to write a sql query to get last week's count in the format of mm/count/yyyy. In my situation, Wednesday starts a new week. So today March 8th, 2012 is the second week of March, and the last week count is 03/01/2012. Please note, the 01 means week count not date. If consider March 1st, 2012, the last week count should be the last week of Feburary which is 02/05/2012
Update: Thanks for Steve's comment, reminding me to clear my question here. Each month starts from it's first Wednesday. So the 03/01/2012 starts from March 7th, 2012 to March 13th, 2012
Any sql query solution?
I'm using sql-server
Here's some date logic to get you started. I can't comprehend your funky "last week count" definition by the two examples, so I can't code it.
DECLARE
#Now datetime,
#Today datetime,
#MonthStart datetime,
#WeekCount int,
#Result varchar(10)
SET #Now = GetDate()
SET #Today = DateAdd(dd, DateDiff(dd, 0, #Now), 0)
SET #MonthStart = DateAdd(mm, DateDiff(mm, 0, #Today), 0)
-- relative to the actual day the month started
SET #WeekCount = ((DatePart(dd, #Today)-1) / 7) + 1
-- relative to Sunday
--SET #WeekCount = (DatePart(week, #Today) - (DatePart(week, #MonthStart) + 1
SET #Result = convert(varchar(10), #Today, 101)
SET #Result =
LEFT(#Result, 3) -- month/
+ RIGHT('z00' + convert(varchar(2), #WeekCount), 2) -- week with zero pad
+ RIGHT(#Result, 5) -- /year
SELECT
#Now as TheNow,
#Today as Today,
#MonthStart as MonthStart,
#WeekCount as WeekCount,
#Result as Result
See also SET DATEFIRST
in oracle:
select to_char( sysdate, 'w' ) from dual
#Steven,
Sorry, this didn't fit as a comment.
Your description of "[nth] week in [month]" is not clear. Your week starts on Wednesday. February first was a Wednesday, so the weeks in February, 2012, if there are indeed 5 of them, are
February 1-7
February 8-14
February 15-21
February 22-28 and
February 29 - March 6
You describe March 8 as being in the "second week of March," so the first week of March must be the previous week, which is February 29 - March 6. Note that this is also what you call "the last (fifth) week in February."
By your description, then, the weeks you write as '02/05/2012' and '03/01/2012' are the same week with different names.
What is the rule that lets you know that
You want the same "last week" when you run the program March 1 as when you run it March 8
You want that week (which is the same) described differently in these two cases.
Or does your question have a mistake in it.
Once you can describe what you need, it will be easy to program. If you don't know what you want, it will be impossible to program.
In SQL Server these queries return 1,1,2,5
select ((DATEPART(dd, 'March 01, 2012')-1)/7) + 1
select ((DATEPART(dd, 'March 07, 2012')-1)/7) + 1
select ((DATEPART(dd, 'March 08, 2012')-1)/7) + 1
select ((DATEPART(dd, 'March 31, 2012')-1)/7) + 1
So this may be what you need
declare #dt datetime = 'March 31, 2012'
select
CONVERT(nvarchar(4), DATEPART(month,#dt)) + '/' +
CONVERT(nvarchar(4), ((DATEPART(dd, #dt)-1)/7) + 1) + '/' +
CONVERT(nvarchar(4), DATEPART(year,#dt))
which produces '3/5/2012'
SELECT
RIGHT(100 + MONTH(D) , 2) + '/' +
RIGHT(100 + (DAY(D) + 6) / 7, 2) + '/' +
DATENAME(YEAR, D)
FROM (
SELECT DATEADD(WEEK, -1, GETDATE()) AS D
) s
I think that should do it, I don't get the value for last week because it was confusing and the answer is cleaner that way, you can just add a DATEADD(day,-7,date) somewhere.
SET DATEFORMAT YDM
CREATE TABLE #Dates (Date DATETIME)
INSERT INTO #Dates VALUES ('2012-31-03');
INSERT INTO #Dates VALUES ('2012-15-02');
INSERT INTO #Dates VALUES ('2012-02-04');
INSERT INTO #Dates VALUES ('2012-02-05');
WITH Mondays as (
SELECT DATEADD(week, DATEDIFF(week,0, DATEADD(day,6-DATEPART(day,d.Date),d.Date) ), 0) FirstMonday,
DATEADD(week, DATEDIFF(week,0, DATEADD(day,6-DATEPART(day,DATEADD(month, -1, d.Date)),DATEADD(month, -1, d.Date)) ), 0) FirstMondayOfLastMonth,
d.Date
FROM #Dates d),
Wednesday as (
SELECT CASE WHEN DATEPART(day,m.FirstMonday) <= 5
THEN DATEADD(day,2,m.FirstMonday)
ELSE DATEADD(day,-5,m.FirstMonday) END FirstWednesday,
CASE WHEN DATEPART(day,m.FirstMondayOfLastMonth) <= 5
THEN DATEADD(day,2,m.FirstMondayOfLastMonth)
ELSE DATEADD(day,-5,m.FirstMondayOfLastMonth) END FirstWednesdayOfLastMonth,
m.Date
FROM Mondays m)
SELECT w.Date,
CASE WHEN w.Date >= w.FirstWednesday THEN
RIGHT('0' + CAST(DATEPART(month,w.Date) AS NVARCHAR(2)),2)
+ '/0' + CAST(DATEDIFF(week,w.FirstWednesday, w.Date) + 1 AS NVARCHAR(2))
+ '/' + CAST(DATEPART(year,w.Date) AS NVARCHAR(4))
ELSE
RIGHT('0' + CAST(DATEPART(month,w.FirstWednesdayOfLastMonth) AS NVARCHAR(2)),2)
+ '/0' + CAST(DATEDIFF(week,w.FirstWednesdayOfLastMonth, w.Date) + 1 AS NVARCHAR(2))
+ '/' + CAST(DATEPART(year,w.FirstWednesdayOfLastMonth) AS NVARCHAR(4))
END AS weekDate
FROM Wednesday w
DROP TABLE #Dates
The result is :
Date weekDate
2012-03-31 00:00:00.000 03/04/2012
2012-02-15 00:00:00.000 02/03/2012
2012-04-02 00:00:00.000 03/05/2012
2012-05-02 00:00:00.000 05/01/2012