How to format datetime as M/D/YYYY in SQL Server? - sql

To convert a datetime to MM/DD/YYYY, this works:
declare #datetime datetime = '2015-01-01'
select convert(varchar(10),convert(date,#datetime),101)
This evaluates to 01/01/2015. How can I have the date convert to 1/1/2015 instead?
Nothing on http://www.sql-server-helper.com/tips/date-formats.aspx matches the M/D/YYYY format.

I think the only possibility you have is to do something like this:
DECLARE #datetime DATETIME = '2015-01-01'
SELECT LTRIM(STR(MONTH(#datetime))) + '/' +
LTRIM(STR(DAY(#datetime))) + '/' +
STR(YEAR(#datetime), 4)
With SQL Server 2012 and above, you can do this:
SELECT FORMAT(#datetime, 'M/d/yyyy')

DECLARE #datetime DATETIME = '2015-01-01';
SELECT STUFF(REPLACE('/' + CONVERT(CHAR(10), #datetime, 101),'/0','/'),1,1,'')
This is how it works:
First CONVERT the DATETIME to CHAR
Then Add a '/' character at the begining
REPLACE all '/0' with '/'
With STUFF, get rid of the first '/'

There's no convert style that uses single digit day or month. This could work, though.
declare #datetime datetime = '2015-01-01'
select
cast(month(#datetime) as varchar(2)) + '/' +
cast(day(#datetime) as varchar(2)) + '/' +
cast(year(#datetime) as varchar(4))

Related

Change date format dd/mm/yyyy to yyyy-mm-dd

am working in SQL Server 2008, while merging I got error like
conversion failed when converting datetime from character string
select *
from table_name
where cast(f_datetime as date) <=
cast(cast(datepart(year,cast(convert(varchar(250),#Year,103) as date) )as varchar(250))+ '-'+ cast(datepart(MM,cast(convert(varchar(10),#month,103) as varchar(50))+'-01' as date)
I cannot speak to the cast() on f_datetime. But for the rest, you can do:
where cast(f_datetime as date) <= convert(date, convert(varchar(250), #year * 10000 + #month * 100 + 1))
This simplifies the calculation, and prevents things like #year from being treated as a date due to the convert() function.
I assume your f_datetime field format is "dd/mm/yyyy". If yes you can easily convert this field instead of trying to merge and convert #year and #month fields. check this query :
SELECT *
FROM table_name
WHERE CONVERT(DATE,f_datetime,103)<= CAST(CONVERT(VARCHAR, #year) + '-' + CONVERT(VARCHAR, #month) + '-' + '01' AS DATE)

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;

Getting Datetime by Year, Month, Date

I want to get date by Using Year, Month and Day functions. Example
declare #Date smalldatetime
select #Date = Year('20140530') + Month('20140530') + Day('20140530')
What I want is to assign #Date = '20140530' as smalldatetime. But I want to do this by means of somwething similar to above expression. How can Ido this. Thanks in advance.
Instead try something like below
declare #Date varchar(20)
select #Date = cast(Year('20140530') as varchar) +
cast(Month('20140530') as varchar) +
cast(Day('20140530') as varchar)
select #Date
results in: 2014530.
(OR) like below
declare #Date VARCHAR(20)
select #Date = cast(Year('20140530') as varchar) + '-' +
cast(Month('20140530') as varchar) + '-' +
cast(Day('20140530') as varchar)
select cast(#Date as smalldatetime)
results in: 2014-05-30 00:00:00
Year()/Month()/DaY() functions returns the year/Month/Day as Integer. What you are actually doing can be simulated as below
declare #Date smalldatetime
set #Date = 2049
select #Date
which will result in : 1905-08-12 00:00:00
Use Like, Set will give the Date in #Date instead of select
declare #Date smalldatetime
set #Date = Year('20140530') + Month('20140530') + Day('20140530')
print #Date

How to get month and year from the date

Using SQL Server 2000
Date like 20120101, 20120201, 20120301...
Fomat is yyyymmdd
I want to display month and year from the date like 01/2012, 02/2012, 03/2012...
Expected Output
01/2012
02/2012
03/2012
I don't want 1/2012, 2/2012, 3/2012...
Can any one give me a idea or query help....
You can use something like this:
DECLARE #input VARCHAR(20)
SET #input = '20120101'
-- convert the 20120101 to a DATETIME
DECLARE #thedate DATETIME
SELECT #thedate = CONVERT(DATETIME, #input, 112)
-- reformat that DATETIME to your needs
DECLARE #output VARCHAR(20)
SET #output = RIGHT('00' + CAST(DATEPART(MONTH, #thedate) AS VARCHAR(2)), 2) +
'/' + CAST(DATEPART(YEAR, #thedate) AS VARCHAR(4))
SELECT #output
You could "hide" this functionality into a user-defined function to make it more easily usable in your code:
CREATE FUNCTION dbo.MonthYearFromDate (#input VARCHAR(20))
RETURNS VARCHAR(20)
AS BEGIN
DECLARE #thedate DATETIME
SELECT #thedate = CONVERT(DATETIME, #input, 112)
DECLARE #output VARCHAR(20)
SET #output = RIGHT('00' + CAST(DATEPART(MONTH, #thedate) AS VARCHAR(2)), 2) + '/' + CAST(DATEPART(YEAR, #thedate) AS VARCHAR(4))
RETURN #output
END
and then you can call this like so:
SELECT dbo.MonthYearFromDate('20120515')
and get the output
05/2012
SELECT CAST(datepart(month,getdate()) AS CHAR) + '/' + CAST(datepart(year,getdate()) AS CHAR)
Instead of the function getdate() you need your date field.
have a look at the SUBSTRING function http://msdn.microsoft.com/en-us/library/ms187748.aspx
You should also consider storing your dates as a DATETIME (I assume you are using VARCHAR?)
Search on extract() function. It will solve your problem.

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)