Convert MonthName-Year to Year-MonthNumber - sql

I have a custom date string in SQL. I need to convert the date into custom format. Below is the example for it.
SQL Input : 'January-2019' (Month Name-Year format)
Output : '201901' (Year-Month Number format, Month Number must be two digit)

Since you're using SQL 2008 you can use CONVERT(DATE, ...) + CONVERT(VARCHAR, ...):
DECLARE #input AS VARCHAR(20) = 'January-2019'
SELECT CONVERT(VARCHAR(6), CONVERT(DATE, '01-' + #input), 112)
Note that you need to prepend 01- to the string.

You can also try this.
DECLARE #input AS VARCHAR(20) = 'January-2019'
Select Convert(Varchar(6), Cast(Replace(#input, '-', ' 01 ') as DATE), 112)
Live Demo

Related

Converting a FLOAT into DATE in SQL?

I wanted to know how to properly convert FLOAT value into a DATE?
The data we receive has a value oriented as such: YYYYMM ex. 201911 (today's Year + Month). However, all the values passing under the YYYYMM column signifies the first of the month ex. 201911 = 11/01/2019.
RIGHT([DATE],2) + '/01/' + LEFT([DATE],4) AS [DATE]
When I try converting it, it doesn't put it in a date format because I tried using it in a DATEADD function and it errored on the field I converted.
If your value is YYYYMM, then one simple method is to convert to a string and then a date:
select convert(date, convert(varchar(255), yyyymm) + '01')
Or, use datefromparts():
select datefromparts(floor(yyyymm / 100), yyyymm % 100, 1)
Please check this :
DECLARE #Date AS FLOAT;
SET #Date = 201911;
SELECT CONVERT(VARCHAR, CAST(CAST(LEFT(#Date,4) AS VARCHAR(4)) + '/01' + '/' + CAST(RIGHT(#Date,2) AS VARCHAR(2)) AS DATE) , 103) As CREATEDDATE
Will output 11/01/2019

How to concatenate declare date variables

How do I concatenate declare date variables so they are in one column? I need to show the dates as between dates. When I run the following I get an error message.
Declare #startdate date = '20180101'
Declare #enddate date = '20180731'
SELECT
'Dates' = #startdate+' - '+#enddate
FROM TABLE
Error Message:
The data types date and varchar are incompatible in the add operator.
Convert them to strings before concatenating them. For the default format on your system:
select dates = convert(varchar(255), #startdate) + ' - ' + convert(varchar(255), #enddate)
To specifically convert to YYYYMMDD use format 112:
select dates = convert(varchar(255), #startdate, 112) + ' - ' + convert(varchar(255), #enddate, 112)
You can use concat() :
SELECT CONCAT(#startdate, ' - ', #enddate) AS Dates
FROM TABLE;

Datetime Format in Month and Year

I want to format a datetime column like so: "March 2004." Currently, I tried
DECLARE #Date VARCHAR(20) = '2004-03-05 01:00'
SELECT CONVERT(VARCHAR(20),CAST(#Date AS DATETIME),13) AS DateFormats
but not getting the right result.
You should really apply formatting when you present your data, not in the query, but to answer the question, if you're using SQL Server 2012 or above you can use FORMAT
DECLARE #Date datetime = '2004-03-05 01:00'
SELECT FORMAT( #Date, 'MMMM yyyy' ) AS DateFormats
Something like:
DECLARE #Date VARCHAR(20) = '2004-03-05 01:00'
SELECT DATENAME(MONTH, #Date) + ' ' + DATENAME(YEAR, #Date)
More on DATENAME can be found here https://msdn.microsoft.com/en-gb/library/ms174395.aspx
Essentially it gets the month part of the date and the year part of the date and concatenate them together.
None of the built-in formats include the full month. Instead, use datename():
select datename(month, #date) + ' ' + datename(year, #date)

SQL VarChar to Date

hi
i am trying to convert a VarChar date field (e.g. 20100320) to a real date field like
'dd/mm/yyyy' (e.g. 20/03/2010).
I have tried two ways:
a)
(SELECT MIN(CAST(A.DateOfAction AS Date)) AS Expr1
FROM ResAdm.Action A
WHERE (A.PersonID = P.PersonID))
AS 'Period From',
b)
(SELECT MIN(CONVERT(DATE, A.DateOfAction, 103)) AS Expr1
FROM ResAdm.Action A
WHERE (A.PersonID = P.PersonID))
AS 'Period From',
both producing the result like
yyyy-mm-dd (e.g. 2010-03-20)
but i want the result like
dd/mm/yyyy (e.g. 20/03/2010)
any help will be appreciated.
thanks.
Try this:
select convert(varchar(8), convert(datetime, min(a.DateOfAction), 112), 103)
Your problem is that once you have a date format, SQL Server will dump it out in its default date format, which you've discovered is yyyy-mm-dd. You need to convert from date to varchar to get the format you want. But to convert from date, you need to first convert to date! 112 is the format for yyyymmdd, and 103 is the format for dd/mm/yyyy, so this is why you need these formats. (Books Online reference for date formats)
Declare #date nvarchar(100)
set #date = '20100320'
select convert(varchar, CONVERT(datetime, #date, 109), 103)
You can use
convert(varchar, CONVERT(datetime, A.DateOfAction, 109), 103)

Convert SQL DateTime format

How can I display a DATETIME value (2010-12-02 15:20:17.000) as 02/12-2010 15:20?
For SQL Server:
select stuff(convert(varchar, getdate(), 105), 3, 1, '/') + ' ' + left(convert(varchar, getdate(), 8), 5)
DateTime is a DateTime is a DateTime - it just holds a date and time and doesn't have any string representation, really.
See the CAST and CONVERT topic in the SQL Server Books Online for details - it shows all supported date formats that SQL Server supports.
For your source format (2010-12-02 15:20:17.000) you could probably use style no. 121
DECLARE #source VARCHAR(50)
SET #source = '2010-12-02 15:20:17.000'
DECLARE #Date DATETIME
SELECT #Date = CONVERT(DATETIME, #source, 121)
SELECT #Date
but your target format is a bit odd..... I don't see any "out of the box" style that would match your needs. You'll need to use some string manipulation code to get that exact format.
Use MSSQL's build-in function to convert datetime to string with format,
SELECT CONVERT(VARCHAR(8), GETDATE(), 1) AS [MM/DD/YY] --2/5/12
SELECT CONVERT(VARCHAR(10), GETDATE(), 103) AS [DD/MM/YYYY] --5/2/2012
You need to create custom function to get various format to use like this;
SELECT dbo.ufn_FormatDateTime(GETDATE(),'YYYY-MM-DD HH:mm:SS tt')
--Output : 2012-02-05 01:58:38 AM
SELECT dbo.ufn_FormatDateTime(GETDATE(),'(dddd) mmmm dd, yyyy hh:mm:ss.fff tt')
--Output : (Sunday) February 05, 2012 01:58:38.723 AM
SELECT dbo.ufn_FormatDateTime(GETDATE(),'dd/MM/yyyy')
--Output : 05/02/2012
SELECT dbo.ufn_FormatDateTime(GETDATE(),'yyyy MMM, dd (ddd) hh:mm:ss tt')
-- Output : 2012 Feb, 05 (Sun) 01:58:38 AM
Get the code snippet from this link.
http://www.tainyan.com/codesnippets/entry-62/sql-server-date-time-format-function.html
http://msdn.microsoft.com/en-us/library/ms189491.aspx
Is this what you're looking for?
Assuming Oracle:
select TO_CHAR(SYSDATE, "dd/mm-yyyy HH24:mi")
from DUAL;
Assuming SQL Server:
select STR(DATEPART(DAY, GETDATE()), 2)
+ '/'
+ STR(DATEPART(MONTH, GETDATE()), 2)
+ '-'
+ STR(DATEPART(YEAR, GETDATE()), 4)
+ ' '
+ STR(DATEPART(HOUR, GETDATE()), 2)
+ ':'
+ STR(DATEPART(MINUTE, GETDATE()), 2);
Little example I use for Germany and Switzerland: dd.mm.yyyy hh:mm
SELECT CONVERT(varchar, GETDATE(), 104) + ' ' + LEFT(CONVERT(varchar, GETDATE(), 108), 5)