I have this statement in my SQL Server Mgt Studio script. The Date output is Dec 28 2015 12:00AM etc. how to change it to 20151228 (YYYYMMDD format). Pls help. Thank you.
+'|'+ Cast(dateadd(wk,DATEDIFF(Wk,7,GETDATE()),0) as varchar) AS [FIRSTDAY|],
+'|'+ CAST(DATEADD(wk,DATEDIFF(wk,7,GETDATE()),6)AS varchar) as [LASTTDAY|]
Prior to Sql Server 2012:
CONVERT(varchar(8), dateadd(wk,DATEDIFF(Wk,7,GETDATE()),0), 112)
Sql Server 2012 and later:
FORMAT(dateadd(wk,DATEDIFF(Wk,7,GETDATE()),0), 'yyyyMMdd')
But you should really ask yourself if SQL is the correct place to do this at all. You'll often have better results by just returning a DateTime value and letting client code handle the formatting.
Use the convert function instead of cast. Use style 112. https://msdn.microsoft.com/en-us/library/ms187928.aspx
Please try this.
declare #dt nvarchar(50) = 'Dec 28 2015 12:00AM'
select convert(varchar(4),datepart(yy,#dt))+convert(varchar(2),datepart(mm,#dt))+convert(varchar(2),datepart(dd,#dt))
Related
I have the date in this format 2017-02-03 and I want that my date should be in this format 01-Mar-2016 while making select command.
I am using SQL Server 2012
Check this website. This will be helpful.
select replace(convert(varchar, getdate(), 106),' ','-')
What is done here is: First convert it into '01 Mar 2016' and then replace white spaces(' ') with '-'. Which will give desired output as '01-Mar-2016'
Use SQL Server Format function where you can specify .NET format string
For instance for current date
SELECT FORMAT(GETDATE(), 'dd-MMM-yyyy');
Also you can specify culture here if needed
SELECT FORMAT(GETDATE(), 'dd-MMM-yyyy', 'en-US');
If you dates are stored as string you'd better fix this but you can just CAST or CONVERT them instead. I.e.
SELECT FORMAT(CAST('2017-10-11' AS date), 'dd-MMM-yyyy');
In my aplication I use a DateEdit control and show the date using this format dd/MM/yyyy so the date is save correctly when I use mssql-server 2014 (spanish) but whe I use mssql-server 2008 (english) the date is stored with this fomat MM/dd/yyyy so what I need to know is if there is a way to store the date with the format dd/MM/yyyy no matter the versiĆ³n of mssql-server I use or the lenguage I use?
EDIT: to explain better this is the problem, this text 03/07/2017 is stored in the sqlserver 2014 as july 3 of 2017 and in the sqlserver 2008 is stored as march 7 of 2017.
Convert.ToDateTime(String).ToString("dd-MM-yyyy")
Check the defualt language on your MS SQL Server 2008, and if its possible, you can change to Spanish: How to change Date Format after installing SQL Server
Also, you can format your query output with the CONVERT function (#ZLK post the link) using the format 103: CONVERT (DATETIME,[YOURDATE],103)
Following solution has two options. You can use one as per your requirements.
DECLARE #Date AS TABLE(val VARCHAR(30));
INSERT INTO #Date VALUES('12/04/2016');
INSERT INTO #Date VALUES('10/01/2015');
INSERT INTO #Date VALUES('17/03/2017');
--Option 1
SELECT TRY_PARSE(val AS DATE USING 'en-gb') 'Option 1' FROM #Date;
--Option 2
SELECT CONVERT(VARCHAR(10),TRY_PARSE(val AS DATE USING 'en-gb'),103) 'Option 2' FROM #Date;
And the outputs are:
As others said: SQL doe not store dates as we humans do. But thats another matter.
I think what you need is to inform SQL the format your date has.
The best option is to use ISO 8601 format: yyyy-mm-ddThh:mi:ss.mmm.
So, if before saving the date you just format it with this format, SQL (no matter the version or the language) will recognize it as begining with a year, followed by a month and so on. The key of this subject is using first 4 digits.
This way, 2017-01-03 will always be January 03, 2017 and never March 01, 2017. Hope this help you.
Using this you can change the date Format
select FORMAT(GETDATE(), 'dd/MMM/yyyy', 'en-us')
if any date time value provided to sql server can i get it's midnight value with some function in sql server.. for example if i provide 2013/07/03 01:34AM , i want to get it to 2013/07/03 12:00 AM.Is there a way to do it?
SQL Server 2008+
SELECT CAST(CAST('2013/07/03 01:34AM' AS date) AS datetime)
For older versions, see this Best approach to remove time part of datetime in SQL Server Never use anything that requires float or int or varchar conversions
This should give you what you need:
SELECT DATEADD(DAY, DATEDIFF(DAY, 0, InputDateField), 0)
Should be slightly quicker than cast:
Most efficient way in SQL Server to get date from date+time?
How to get format like 19 Dec 2012 from getdate() function in tsql
select convert(varchar(11),getdate(),?)
select convert(varchar(11),getdate(),106)
Cast and Convert lists the available formats:
106 dd mon yy
looks about right.
I'd generally avoid doing any formatting on the database. Keep dates as dates as long as possible - only convert at the last moment in displaying it to the user (similarly, for any input, prefer to convert it from string into its proper data type as early as possible)
-- Can use if SQL 2008 and above (precision to nanoseconds)
SELECT CONVERT(VARCHAR(12), SYSDATETIME(), 106)
-- otherwise (precision to miliseconds)
SELECT CONVERT(VARCHAR(12), GETDATE(), 106)
How can I get a date like 'dd.mm.yyyy HH:MM:SS'
I tried the following but it's not exactly what I'm looking for...
select convert(varchar(20),getdate(),113)
result: 14 Jul 2011 09:23:57
Thanks a lot
Largo
See http://msdn.microsoft.com/en-us/library/ms187928.aspx
You can concatenate it:
SELECT CONVERT(VARCHAR(10), GETDATE(), 104) + ' ' + CONVERT(VARCHAR(8), GETDATE(), 108)
A quick way to do it in sql server 2012 is as follows:
SELECT FORMAT(GETDATE() , 'dd/MM/yyyy HH:mm:ss')
You can learn datetime formatting in sql server here
http://www.sql-server-helper.com/tips/date-formats.aspx
http://yrbyogi.wordpress.com/2009/11/16/date-and-time-types-in-sql-server/
CONVERT(VARCHAR,GETDATE(),120)