What is the default format that is used when I cast date or datetime into varchar? What settings affect the result? Did I come to the right conclusion that date always converts into YYYY-MM-DD and datetime is only affected by language?
declare #myDatetime as datetime = dateadd(day,3,getutcdate());
declare #myDate as date = #myDatetime;
set dateformat ymd; --no difference?
set language us_english; --affects datetime only?
select
cast(#myDate as varchar(max)) --2017-10-13
, cast(#myDatetime as varchar(max)) --Oct 13 2017 1:47PM
;
set dateformat dmy; --no difference?
select
cast(#myDate as varchar(max)) --2017-10-13
, cast(#myDatetime as varchar(max)) --Oct 13 2017 1:47PM
;
set language Lithuanian; --affects datetime only?
select
cast(#myDate as varchar(max)) --2017-10-13
, cast(#myDatetime as varchar(max)) --spl 13 2017 1:56PM
;
Related
I have a Column date of datatype datetime which has values (2017-05-17 00:00:00.000) now i want to save this datetime as 20170517 to a variable.
i tried this:
declare #AAA VARCHAR(50)
select #AAA = convert(varchar, #Date, 112)
but it returns May 17 2017 12:00AM
I tried this and it worked fine:
declare #aaa varchar(50)
SELECT #AAA= FORMAT( #Date, 'yyyyMMdd', 'en-US' )
select #AAA= REPLACE(#AAA,'-','')
I want a datetime variable which will be having 12 hour datetime format.
Below example converts date in 12 hour but not helpful because it is in VARCHAR format, if tries to convert in datetime it shows like 2016-03-08 00:00:00.000-
declare #t datetime = NULL
set #t = '2016-03-08 00:00:00'
SELECT CONVERT(VARCHAR, #t, 100) AS DateTime_In_12h_Format
I want a variable which will be holding 12 hour format something like this
declare #t datetime = NULL
set #t = '2016-03-08 00:00:00'
set #t = CONVERT(datetime, #t, 100)
select #t -> this should be -> Mar 8 2016 12:00AM
If you want to convert the current datetime for example:
SELECT CONVERT(VARCHAR, getdate(), 100) AS DateTime_In_12h_Format
Instead of getdate() you can put your desired column in a query (such as tdate in your example). If you want JUST the time in 12h and not the date and time use substring/right to separate them. It seems that you already know how to =).
This page lists every datetime conversion. It's really handy if you need other types of conversions.
Declare one more varchar to save new datetime
declare #t datetime = NULL
declare #t1 varchar(20) = NULL
set #t = '2016-03-08 00:00:00'
set #t1 = CONVERT(varchar(20), #t, 100)
select #t1 as DateTime_In_12h_Format
I have a string '2016-03-05' which I want convert to a datetime.
Here is my code:
Declare #period as nvarchar(10)
Set #period = '2016-03-05'
Select Convert(Datetime, #period, 112).
Running that I receive the error
Conversion failed when converting datetime from character string.
Conversion format 112 you've used assumes input '20160305' (without dashes as date parts separator).
So either do
select convert(Datetime, '20160305', 112)
or (if your input really contains dashes then just remove them like:
select convert(Datetime, replace('2016-03-05', '-', ''), 112)
That's because the style you're passing to the CONVERT function does not support that format. You can do two things...
1- Drop the dashes
Declare #period as nvarchar(10)
Set #period = '20160305' -- I've dropped the dashes here
Select Convert(Datetime, #period, 112)
2- Change the style to something that supports this format
Declare #period as nvarchar(10)
Set #period = '2016-03-05'
Select Convert(Datetime, #period, 21) -- I've changed the style here
For a complete reference, read the MSDN documentation
SELECT LEFT(201605,4) + '-' + RIGHT(201605,2)
{OR}
SELECT RIGHT(201605,2) + '-' + LEFT(201605,4)
Try this it will work I guess according to your need
Declare #period as nvarchar(10)
Set #period = '2016-03-05'
select CAST(#period as datetime)
I want to have a function within the SQL query which will bring out the current week in SSRS 2005 using parameters. I have declared two variables #FromDate and #ToDate. I am using these two variables in the where clause below
where SH.[] between CONVERT(datetime, #FromDate, 105) and CONVERT(datetime, #ToDate, 105)
How do I bring out the current week using #FromDat and #ToDate? Can I have any help?
Thankss
CREATE TABLE dbo.TestDates (
SaleID TINYINT IDENTITY(1,1),
Amount NUMERIC(9,2),
SaleDate DATETIME
);
GO
INSERT INTO dbo.TestDates (Amount, SaleDate)
VALUES (200.00, '2014-12-15'),
(130.00, '2014-12-16'),
(40.00, '2014-12-17'),
(70.00, '2014-12-18'),
(590.00, '2014-12-19')
-----------------------------------------
CREATE FUNCTION dbo.uspGetCurrentWeek(#From VARCHAR(100)
RETURNS VARCHAR(100)
AS
BEGIN
DECLARE #StartCounter TINYINT = 1
DECLARE #EndCounter TINYINT = 5
WHILE #StartCounter <= #EndCounter
BEGIN
DECLARE #String VARCHAR(100) = ( SELECT 'Week' +
CAST(DATEPART(WEEK, SaleDate) AS VARCHAR(2))
FROM dbo.TestDates
WHERE SaleDate = #From )
SET #StartCounter = #StartCounter +1
END
RETURN #String
END
--------------
-----Test data ----
SELECT *, dbo.uspGetCurrentWeek('2014-12-15')
FROM dbo.TestDates;
---------------------------------
How can I create a method to subtract two dates and this is equal to in real date as format datetime2(7) in sql server 2008.
For example ,I create this method:
Delete from TblMessage
Where MDateTime<((SELECT TOP (1)MDateTime FROM TblMessage ORDER BY MDateTime DESC)- ('2013-10-04 16:47:56.0000000'))
but it is not working .I want to result of subtract two date like this:
MDateTime1:2013-10-05 16:47:56.0000000
MDateTime2:2013-09-04 16:47:56.0000000
Result:2013-01-01 00:00:00.0000000
Result=MDateTime1-MDateTime2
How can I do this. Thanks...
Perhaps you are looking for this?
select DATEADD( day, datediff(day,GETDATE(), getdate() - 10), GETDATE() ) ;
DATEDIFF(dayofyear,MDateTime1,MDateTime2) AS Result
http://msdn.microsoft.com/en-us/library/ms189794.aspx
DECLARE
#DATE1 DATETIME = '2013-10-05 16:47:56.000',
#DATE2 DATETIME = '2013-09-04 17:37:42.000',
#DATEDIFF AS INT,
#BASEDATE DATETIME;
-- USE WHAT EVER DATE YOU WISH TO BE YOUR BASE DATE
SET #BASEDATE = '1/1/1900';
SET #DATEDIFF = DATEDIFF(SECOND, #DATE2, #DATE1);
SELECT #DATE1,#DATE2,#DATEDIFF, DATEADD(SECOND,#DATEDIFF,#BASEDATE)
Thus a scalar function could be created like this...
CREATE FUNCTION dbo.sf_GetMeasureDate
(
#EndDate DATETIME,
#StartDate DATETIME,
#BaseDate DATETIME
)
RETURNS DATETIME
AS
BEGIN
DECLARE #DATEDIFF AS INT
SET #DATEDIFF = DATEDIFF(SECOND, #StartDate, #EndDate);
Return DATEADD(SECOND,#DATEDIFF,#BASEDATE)
END
GO
Then within you regular SELECT statement you can call the function as such.
SELECT dbo.sf_GetMeasureDate('2013-10-05 16:47:56.000','2013-09-04 17:37:42.000','1/1/1900')
or within an existing query:
SELECT dbo.sf_GetMeasureDate([fld1],[fld2],'1/1/1900')