Date and time conversion from SQL server - sql

I am working with a query where I fetch date with a sql query. And wondering how to use the CONVERT function to not show todays date, but convert the date saved in the database which is named as: routines.date. The goal is to sort it as D-M-Y and now its saved as Y-M-D
This didn't work:
CONVERT(date,routines.date,105) as Date

Try this
SELECT CONVERT(VARCHAR(10), CAST(routines.date AS DATETIME), 105)
OR
SELECT CONVERT(varchar, DATEPART(yyyy, #routines.date)) + '-' + CONVERT(varchar, DATEPART(mm, #routines.date)) + '-' + CONVERT(varchar, DATEPART(dd, #routines.date))

Related

SQL Server - converting date to D-MMM-YYYY format

How can I get the string from GETDATE() in D-MMM-YYYY format, e.g 3 May 2016
If I use CONVERT(VARCHAR, GETDATE(), 106), I would get a leading zero on day which is not what I want.
If you are on SQL Server 2012 or later, use FORMAT:
SELECT FORMAT(GETDATE(), 'd MMM yyyy')
Edit: some of the answers below are just flat-out wrong so I'm adding a solution for older versions of SQL Server. 2005 is the earliest that I can get my hands on:
SELECT CASE
WHEN CONVERT(varchar(20), GETDATE(), 106) LIKE '0%'
THEN SUBSTRING(CONVERT(varchar(20), GETDATE(), 106), 2, 20)
ELSE CONVERT(varchar(20), GETDATE(), 106)
END
SELECT case when left(convert(varchar(20),[DateColumn],106),1) ='0'
then right(convert(varchar(20),[DateColumn],106),len(convert(varchar(20),[DateColumn],106))-1)
else convert(varchar(20),[DateColumn],106)
end
FROM [DB].[dbo].[Table]
Some sample output :
29 Apr 2016
2 Apr 2016
If you cannot use FORMAT (Below SQL Server 2012)
DECLARE #date DATE = '20160503'
SELECT REPLACE(DATEPART(DAY, #date),' 0','') + ' ' +
CONVERT(CHAR(3), #date, 0) + ' ' +
CAST(DATEPART(YEAR, #date) AS CHAR(4))

how to Select getdate () in yyyy/M format

I want to select getdate() in the format yyyy/M. I tried to write a query
SELECT FORMAT(GETDATE(), 'yyyy/M')
but it is throwing an error.
I am a beginner in SQL. How do I get the yyyy/m format if there is only single digit month? E.g. the query should return 2016/1 when there is only one digit month (it should not return 2016/01) and should return 2016/10 when the month has two digits
How about getting the YEAR and MONTH part of the date and just concatenate them:
SELECT
CAST(DATEPART(YEAR, GETDATE()) AS VARCHAR(4)) + '/' +
CAST(DATEPART(MONTH, GETDATE()) AS VARCHAR(2))
Try this:
SELECT FORMAT(GETDATE(),'yyyy/MM')
SELECT FORMAT(CAST('2015-11-15' AS smalldatetime),'yyyy/M'),
FORMAT(CAST('2015-01-15' AS smalldatetime),'yyyy/M')
Gives:
2015/11 | 2015/1
SELECT CONVERT(VARCHAR(7), GETDATE(), 111) AS [YYYY/MM]
OR
SELECT CONVERT(VARCHAR(7), GETDATE(), 111)

SQL DateFormat Function

Is there a function in SQL that formats a date when given the date and format string?
Similar to how .NET's DateTime.ToString(string format) method works?
I'd like to be able to call something like FORMAT(#myDateTime, 'ddMMMyyyy') and have it give me a string formatted as such.
I know the convert function works but it doesn't quite cover all formats. As an example, SQL can do "dd MMM yyyy" but not "ddMMMyyyy"
As far as I know there is no direct function that will convert date in format passed by you if you are on older SQL version. Format function is available starting from SQL 2012.
You can use convert function but it may not always suit your need.
Refer this: https://msdn.microsoft.com/en-IN/library/ms186724.aspx
To answer your question, this will give you date in DD-MMM-YYYY Format:
select REPLACE(REPLACE(CONVERT(VARCHAR,getdate(),106), ' ','-'), ',','')
For DDMMMYYYY format:
select REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR,getdate(),106), ' ','-'), ',',''),'-','')
Also, you can check the date formats supported by convert function using this:
select convert(varchar, getdate(), 101)
select convert(varchar, getdate(), 102)
select convert(varchar, getdate(), 103)
select convert(varchar, getdate(), 104)
select convert(varchar, getdate(), 105)
select convert(varchar, getdate(), 106)
select convert(varchar, getdate(), 107)
select convert(varchar, getdate(), 108)
select convert(varchar, getdate(), 109)
select convert(varchar, getdate(), 110)
select convert(varchar, getdate(), 111)
select convert(varchar, getdate(), 112)
select convert(varchar, getdate(), 113)

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

SQL Server: Use different date format in Select when selected date equals current date

I have a table with a column "modTime" formatted as datetime.
To fetch dates from this column I use the following line in my Select which returns a date in the format DD MMM YYYY:
CONVERT(VARCHAR(11), G.modTime, 106) AS modTime
Is there a way that I can return the date in a different format (like the following) when it matches the current date and only otherwise use the above format ? This returns the date as Today at hh:mm.
('Today at ' + CONVERT(VARCHAR(5), G.modTime, 108)) AS modTime
Both ways work when I use them separately but I couldn't find a way to combine them using CASE etc.
You can try this:
select iif(G.modTime=getdate(),('Today at ' + CONVERT(VARCHAR(5), G.modTime, 108)),CONVERT(VARCHAR(11),G.modTime, 106) ) from <table name>
Please note that IIF works only with SQL Server 2012 or later.
http://msdn.microsoft.com/en-IN/library/hh213574.aspx
For older versions, this post might help you:
SQL Server 2008 IIF statement does not seem enabled
you will not match to getdate() using equals, and you need to set getdate()'s time to midnight
select
case when G.modTime >= dateadd(day, datediff(day,0, getdate() ), 0)
then ('Today at ' + CONVERT(VARCHAR(5), G.modTime, 108))
else
CONVERT(VARCHAR(11), G.modTime, 106)
end AS modTime
from G
Above I have used: dateadd(day, datediff(day,0, getdate() )
Instead you could use: cast(getdate() as date)
both, have the effect of giving you "today" at 00:00:00