SQL Date Format 107 Without Leading Zeros For The Day - sql

I am needing to convert a date/time stamp via SQL to the following format:
April 4, 2016
Using
SELECT
DATENAME(MM, GETDATE()) +
RIGHT(CONVERT(VARCHAR(12), GETDATE(), 107), 9) AS [PaymentDate]
gets me almost what I need, however, it puts a leading 0 on the day, leaving me with:
April 04, 2016
How can I get rid of that leading 0 on the day?

Alternatively, if you are using SQL Server 2012 or later, you can use the FORMAT() function for this:
Select Format(GetDate(), 'MMMM d, yyyy')
April 4, 2016

this will work:
QUERY:
SELECT DATENAME(MM, GETDATE())+' '+DATENAME(dd, GETDATE())+' '+DATENAME(yy, GETDATE()) AS [PaymentDate];
RESULT:

Related

Specific Date but increase Year based on GETDATE()

I'm using SQL Server.
I have a specific date (in dd/mm/yyyy format) i.e. 06/04/2020
However, in a T-SQL View, it needs to be always 1 year from now i.e. if I run today it would return 06/04/2021. And if it executed in 2021 it would return 06/04/2022 - how would I do this?
So I can run the below:
SELECT CONVERT(DATE, DATEADD(year, 1, '06/04/2020'), 103) as MyDate;
Which will give me:
2021-06-04
However, how do I make it self-maintaining?
You can make it generic by extracting the year from GETDATE(), adding 1 and concatenating that to 06/04 (or the date as required), and then converting. For example:
SELECT CONVERT(DATE, CONCAT('06/04/', DATEPART(YEAR, GETDATE()) + 1), 103)
Output:
2021-04-06

yyyy-mm-dd to mon dd yyyy

I am using SQL Server and I need to convert this date value 2018-02-23
to something like this Feb 23 2018.
Any ideas?
SQL Server 2012+ has a FORMAT() function which could help you to format the date
SELECT FORMAT(getdate(), 'MMM dd yyyy')
You could also use conversion function convert()
SELECT CONVERT(varchar(12), getdate(), 100)
Try it this way:
SELECT dbo.fnFormatDate (getdate(), ‘MON DD, YYYY’) — JAN 03, 2012
See the link below.
https://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005/

Convert text field to Date/time field ms access query

Hi guys can i ask if it's possible to convert a text field to a date/time field?
for example i have a field called Month and the field is a short text data type.
Here is an example:
Month
Sep 2016
Nov 2016
Dec 2016
is it possible that i could convert this one to a date time?
Try this
DateValue("1 " & Month)
I'm assuming you want to put the first of the month in the date.
Output should be
9/1/2016
11/1/2016
12/1/2016
in your case
SELECT convert(datetime, 'Oct 2015 12', 0) or
SELECT convert(datetime, 'Oct 2015', 0) or
SELECT convert(datetime, 'Oct 2015')
It's so easy that it can't be simpler:
FirstOfMonth: CDate([Month])
The values will be primo of [Month].

SQL Select - Current Date - 7 Year

Hi I am trying to get (current Datetime - Years).
Below is my Query..
print getdate()
print (getdate()-(365.25*7))
Result:
Dec 30 2013 10:47AM
Dec 30 2006 4:52PM
is giving correct result.
But When i try
print getdate()
print (getdate()-year(7))
Result:
Dec 30 2013 10:52AM
Oct 17 2008 10:52AM
Can anyone please tell what is wrong in it?
Rather use DATEADD with the datepart set to YEAR.
Returns a specified date with the specified number interval (signed
integer) added to a specified datepart of that date.
Something like
SELECT GETDATE() Today, DATEADD(YEAR,-7,GETDATE()) Today7YearsAgo
SQL Fiddle DEMO
The year(7) part return 1900, which is the year part of 1900-01-08 00:00:00.000 (CAST(7 AS DATETIME)). Then getdate()-year(7) equates to getdate()-1900, which is a day subtraction.
Try this
print getdate()
print DATEADD(Year, -7, GETDATE())
DATE ADD() Returns a specified date with the specified number interval (signed integer) added to a specified datepart of that date.
print getdate()
print DATEADD(Year, -7, GETDATE())
Try MSSQL DATEADD function
select DATEADD(Year,-7,GETDATE());

Find between with separated date fields (year,month,day)

I have the following dates in my table in separated fields. How can I write a query to show the values between two dates.
For example: values between 2/1/2011 and 2/6/2011:
day month year value
--------------------------------------------------
2 6 2011 120
3 7 2011 130
5 5 2011 100
6 1 2011 50
As others have said, my first suggestion would be to use Date. Or if you need more detailed information than your example, Datetime or Timestamp with Time Zone.
But in case you actually have to work with this data, I think something like this should work, depending on your flavor of SQL:
SELECT value, CONVERT(DATE,CONCAT(CONCAT(CONCAT(CONCAT(day, "-"), month), "-"), year), 105) date FROM table_name where (2/1/2011) <= date and date <= (2/6/2011);
(with Oracle SQL, you can use to_date instead of convert and optionally use the || concatenation operator; with SQL server you have to use the + concatenation operator; with MySQL this should be right)
(2/1/2011) and (2/6/2011) could either be strings that you convert similar to the other convert, or inputted using a PreparedStatement or something like it as dates directly (this would be preferable).
I had the same scenario but with Month column displaying Month name . With slight modification on the given query i was able to fetch the data.
SELECT *
FROM Table_Name AS Tbl_Date
WHERE (Year * 10000 + DATEPART(mm, CAST(Month + Year AS DATETIME)) * 100 + 1
BETWEEN 20111101 AND 20121201)
Hope this will help
To convert to Date for easier comparisons without worrying about dmy or mdy, in a standard fashion:
DATEADD(year, year-1900, DATEADD(month, month-1, DATEADD(day, day-1, 0)))
So, something like this. The safest date format to use is yyyymmdd (especially with SQL Server)
SELECT
value,
DATEADD(year, year-1900, DATEADD(month, month-1, DATEADD(day, day-1, 0))) AS realdate
FROM Mytable_name
WHERE
'20110201' <= DATEADD(year, year-1900, DATEADD(month, month-1, DATEADD(day, day-1, 0)))
and
DATEADD(year, year-1900, DATEADD(month, month-1, DATEADD(day, day-1, 0))) <= '20110206'
if you are using oracle database then you can use TO_DATE and TO_CHAR functions to achive this target...
as follow-
select * from table_name
where to_date(day||month||year,'DDMMYYYY')
between &mindate and &maxdate
min date you can put 2-jan-2011 and max date as 2-jun-2011
I hope it should work for you :)
well i found the answer that i wanted thanks guys
SELECT Tbl_Date.Value,Tbl_Date.Year,Tbl_Date.Month,Tbl_Date.Day from Tbl_Date
where ((Tbl_Date.Year*10000)+(Tbl_Date.Month*100)+Tbl_Date.Day) between 110102 and 110602