Manipulating the GETDATE function to display DD MM YY - sql

I have been trying to manipluate the getDatefunction in SQL but I want the date to come out as todays date and look exactly like this 07-Jan-15 but I cant't get it to work and I have tried everything!
What I have at the minute is:
convert(varchar(104), GETDATE()) as [Date]
but that is giving me Jan 7 2015 1:52PM and thats not what I'm looking for! I have tried all the convert options! So any help with this matter would be appreciated

Try
SELECT replace(convert(NVARCHAR, GETDATE(), 106), ' ', '-') as [Date]
Output:- 07-Jan-2015
OR
SELECT replace(LEFT(convert(NVARCHAR, getdate(), 106),6) + '-' +
RIGHT(year(convert(NVARCHAR, getdate(), 106)),2), ' ', '-') as [Date]
Output:- 07-Jan-15

Related

Varchar date conversion in SQL

My date field has value format to be : Feb 15 2019. Is there a way to convert this to MMDDYYYY format?
Desired output: 02152019
Query I tried: SELECT convert(varchar, getdate(), 112) - this query is showing YYYYMMDD format :(
Any help?
select format( convert(date, 'Feb 15 2019'), 'MMddyyyy')
-- results to: 02152019
-- But again, your application is to take care about format!!!
Try this query,
select replace(convert(varchar, getdate(),101),'/','')
You can try this
Select convert(varchar(12), convert(date, 'Feb 15 2019'), 112)
or
SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 101), '/', '') AS [MMDDYYYY]
This will give output - 20190215.
You can pass different value like 101. For different output see Here.

how to get today's date in sql with specific format

I currently have this format in my table:
2015-03-19 10:33:16.983
but I would like to convert it into this format:
3/18/2015 12:00:00 AM.
How can I get that format?
select myDate from myTable
You are looking for the Convert() function.
Something like
SELECT CONVERT(varchar(10),GETDATE(),3) + ' ' + CONVERT(varchar(15),CAST(getdate() AS TIME),100)
You can change 3 as per your local. 3 here will mean it in DD/MM/YYYY format i.e, British and French local.
Copy and paste this and alter as you need:
DECLARE #StartTimestamp datetime
SET #StartTimestamp = CAST((CONVERT(varchar(11), DATEADD(DAY, -1, GETUTCDATE()), 106)) AS datetime)
SELECT CONVERT(varchar, #StartTimestamp, 103) + CONVERT(varchar, #StartTimestamp, 108)
Use Convert
SELECT CONVERT(VARCHAR(25), myDate, 101) + ' ' + CONVERT(VARCHAR(25), myDate, 108) + ' ' + RIGHT(CONVERT(VARCHAR(19),myDate),2)
FROM myTable
This would give you the exact output you requested
mm/dd/yyyy hh:mi:ss AM
(Or PM depending on time)
you need to use two converts with a cast and a right function:
SELECT CONVERT(VARCHAR(10), getdate(), 101) +
' ' + CONVERT(VARCHAR(15), getdate(), 108) +
' ' + RIGHT(CONVERT(VARCHAR(20),getdate()),2)
OUTPUT: 03/19/2015 18:44:19 PM

Date Conversion in SQL Server

I have a date in following format
'2013-09-22 00:00:00.000'
I need this in 22-09-2013, Sunday and 22-Sep-2013, Sunday formats. I dint find any convert function to get days along with.
Thanks
I think you need to use CONVERT() and DATENAME() functions like below :
1) select CONVERT(varchar(20), getdate(),105) + ',' + datename(DW, getdate())
2) select REPLACE(CONVERT(varchar(20), getdate(),106),' ','-') + ',' + datename(DW, getdate())
Result is
07-11-2013, Thursday and 07-Nov-2013, Thursday
SELECT REPLACE(REPLACE(CONVERT(VARCHAR(11), GETDATE(), 103), ' ', '-'), '/', '-') + ', ' + datename(dw,getdate()) AS [DD-Mon-YYYY];
And
SELECT REPLACE(REPLACE(CONVERT(VARCHAR(11), GETDATE(), 106), ' ', '-'), '/', '-') + ', ' + datename(dw,getdate()) AS [DD-Mon-YYYY];
Output:
07-11-2013, Thursday and 07-Nov-2013, Thursday
(You might not need the first REPLACE function. Without it, the second date for me showed up as 07/11/2013, Thursday. The replace simply replaces the '/' with '-' like you specified).
I agree with others suggesting that the conversion should be handled in the client application, though. Especially if the application will be used in different timezones. Hard-coding the date format in SQL then would possibly lead to confusion.
You can write as:
SELECT CONVERT(VARCHAR(10), GETDATE(), 105) + ', '+ DATENAME (weekday ,GETDATE()) AS [DD-MM-YYYY, DAY],
REPLACE(CONVERT(VARCHAR(11), GETDATE(), 106), ' ', '-') + ', '+ DATENAME (weekday ,GETDATE()) AS [DD-Mon-YYYY, DAY]
Dates stored in SQL-Server without any format. It is client application (e.g. ssms) formats a date.
If you need to format date on server side you can convert it into varchar using CAST or CONVERT functions.
SELECT CONVERT(varchar(50),#date,110)+', '+ DATENAME(weekday, GETDATE())

Get 2 Digit Number For The Month

I have an integer column "Month"
I would like to get 2 digit number for month.
This is what I have tried: DATEPART(mm, #Date)
It returns one digit for months January to September
I am using SQL Server 2008
Anyone has suggestion?
Function
FORMAT(date,'MM')
will do the job with two digit.
there are different ways of doing it
Using RTRIM and specifing the range:
like
SELECT RIGHT('0' + RTRIM(MONTH('12-31-2012')), 2);
Using Substring to just extract the month part after converting the date into text
like
SELECT SUBSTRING(CONVERT(nvarchar(6),getdate(), 112),5,2)
see Fiddle
There may be other ways to get this.
Pinal Dave has a nice article with some examples on how to add trailing 0s to SQL numbers.
One way is using the RIGHT function, which would make the statement something like the following:
SELECT RIGHT('00' + CAST(DATEPART(mm, #date) AS varchar(2)), 2)
Another simple trick:
SELECT CONVERT(char(2), cast('2015-01-01' as datetime), 101) -- month with 2 digits
SELECT CONVERT(char(6), cast('2015-01-01' as datetime), 112) -- year (yyyy) and month (mm)
Outputs:
01
201501
CONVERT(char(2), getdate(), 101)
Alternative to DATEPART
SELECT LEFT(CONVERT(CHAR(20), GETDATE(), 101), 2)
SQLFiddle Demo
append 0 before it by checking if the value falls between 1 and 9 by first casting it to varchar
select case when DATEPART(month, getdate()) between 1 and 9
then '0' else '' end + cast(DATEPART(month, getdate()) as varchar(2))
For me the quickest solution was
DATE_FORMAT(CURDATE(),'%m')
Simply can be used:
SELECT RIGHT('0' + CAST(MONTH(#Date) AS NVARCHAR(2)), 2)
Try:
select right ('0'+convert(nvarchar(2), DATEPART(mm, getdate())),2 )
My way of doing it is:
right('0'+right(datepart(month,[StartDate]),2),2)
The reason for the internal 'right' function is to prevent SQL from doing it as math add - which will leave us with one digit again.
SELECT REPLACE(CONVERT(varchar, MONTH(GetDate()) * 0.01), '0.', '')

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)