Monday, March 19, 2012
Date issue
sql server it is noted as 02/10/2005 17:40:00. everything fine so far. I
want to compare it to the current time. This doesn't work. When i print the
current datetime i get " 4 Nov 2005 5:40PM". How can i set sql so it returns
the current date as 02/10/2005 17:40:00.
Your help is much appreciated.
Regards Ron WallegieRon Wallegie (wallegie@.planet.nl) writes:
> I have got an issue with dateformats. When i retrieve a date field from
> the sql server it is noted as 02/10/2005 17:40:00. everything fine so
> far. I want to compare it to the current time. This doesn't work. When i
> print the current datetime i get " 4 Nov 2005 5:40PM". How can i set sql
> so it returns the current date as 02/10/2005 17:40:00.
Preferrably you should return datetime value as-is from SQL Server, that
is no convert. The date value is then passed to the client as a binary
value, and the client will format it according to regional settings.
Same thing applies when you pass dates to SQL Server - pass them as
datetime values in parameters, and conversion will be performed by the
client API, again according to regional settings.
If you for some reason must work with date literals in SQL Server, use
the the format YYYYMMDD. This format is safe from different interpretations.
Note that SQL Server does never look at regional settings in Windows,
and has its own settings. And due to legacy, the safe format in Windows,
YYYY-MM-DD is not safe in SQL Server.
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||If you just want to get the presentation do display as you specifiied, you
can format it to "print" by converting it to a varchar. Look up CAST and
CONVERT in BoL.
SELECT CONVERT(varchar(10), GetDate(), 101) + ' ' + CONVERT(varchar(8),
GetDate(), 108)
HTH,
John Scragg
"Ron Wallegie" wrote:
> I have got an issue with dateformats. When i retrieve a date field from th
e
> sql server it is noted as 02/10/2005 17:40:00. everything fine so far. I
> want to compare it to the current time. This doesn't work. When i print th
e
> current datetime i get " 4 Nov 2005 5:40PM". How can i set sql so it retur
ns
> the current date as 02/10/2005 17:40:00.
> Your help is much appreciated.
> Regards Ron Wallegie
>
>
Date issue
sql server it is noted as 02/10/2005 17:40:00. everything fine so far. I
want to compare it to the current time. This doesn't work. When i print the
current datetime i get " 4 Nov 2005 5:40PM". How can i set sql so it returns
the current date as 02/10/2005 17:40:00.
Your help is much appreciated.
Regards Ron WallegiePrint converts to a string. What do you get when you say SELECT GETDATE()?
Different, no?
Keep in mind that I am having problems understanding your date for the very
reason that you chose an ambiguous date format. I suggest using YYYYMMDD or
YYYY-MM-DDTHH:MM:SS. These are ISO standards and are truthfully the only
safe formats you can use to represent dates in SQL Server. Anything else
can be misinterpreted by the database depending on regional settings,
dateformat, language, etc.
What do you mean by "compare it to the current time"? Compare what? A
value in a table?
"Ron Wallegie" <wallegie@.hotmail.com> wrote in message
news:eWk8U$V4FHA.2432@.TK2MSFTNGP10.phx.gbl...
>I have got an issue with dateformats. When i retrieve a date field from the
>sql server it is noted as 02/10/2005 17:40:00. everything fine so far. I
>want to compare it to the current time. This doesn't work. When i print the
>current datetime i get " 4 Nov 2005 5:40PM". How can i set sql so it
>returns the current date as 02/10/2005 17:40:00.
> Your help is much appreciated.
> Regards Ron Wallegie
>|||If you want to compare two dates to see if they are the same.
SELECT
(CASE WHEN DATEDIFF( ms , myDateField , GETDATE() ) = 0 THEN 'Same' ELSE
'Different' END) AS DateCompare
FROM
myTable
If you're using SMALLDATETIME you can use DATEDIFF( mi , ... ), because it
is only accurate to a minute.
"Ron Wallegie" <wallegie@.hotmail.com> wrote in message
news:eWk8U$V4FHA.2432@.TK2MSFTNGP10.phx.gbl...
> I have got an issue with dateformats. When i retrieve a date field from
the
> sql server it is noted as 02/10/2005 17:40:00. everything fine so far. I
> want to compare it to the current time. This doesn't work. When i print
the
> current datetime i get " 4 Nov 2005 5:40PM". How can i set sql so it
returns
> the current date as 02/10/2005 17:40:00.
> Your help is much appreciated.
> Regards Ron Wallegie
>|||Try using the convert function below.
By using the Convert function. The syntax for using the convert function
is: CONVERT ( data_type [ ( length ) ] , expression [ , style ] )
select convert(varchar,DateColumn,100) --> Nov 4 2005 11:08AM
or
I recommend converting both the date from the table and getdate()
using the function below in military time.
select convert(varchar,DateColumn,120) --> 2005-11-04 11:10:00
Select *
from table
where convert(varchar,DateColumn,120)=convert(
varchar,getdate(),120)
KT
"Ron Wallegie" <wallegie@.hotmail.com> wrote in message
news:eWk8U$V4FHA.2432@.TK2MSFTNGP10.phx.gbl...
> I have got an issue with dateformats. When i retrieve a date field from
the
> sql server it is noted as 02/10/2005 17:40:00. everything fine so far. I
> want to compare it to the current time. This doesn't work. When i print
the
> current datetime i get " 4 Nov 2005 5:40PM". How can i set sql so it
returns
> the current date as 02/10/2005 17:40:00.
> Your help is much appreciated.
> Regards Ron Wallegie
>|||Hey Ron,
There are many ways to build up date strings, please google for them as MSDN
is quite limited in what it suggests. There is also a DB or Server setting
that controls this but I'm not sure what it is right now. Here are some
examples to get you started.
SELECT SUBSTRING(CONVERT(CHAR(12),GETDATE(),1),
1,8) + ' ' +
CONVERT(CHAR(12),GETDATE(),8)
select convert(smalldatetime,convert(char(12),g
etdate(),13))
select convert(datetime,convert(char(30),getdat
e(),21))
select convert(datetime,getdate(),10)|||Thx for your reply,
Hopefully this will help.
When printing getdate() i get " 4 Nov 2005 5:40PM". When retrieving the
table value i get 02/10/2005 17:40:00.
I created a script which works fine op a other server. When i do a select
getdate() on that server i get 02/10/2005 17:40:00 date format. How can i
change the format on the other server?
Regards Ron
"Aaron Bertrand [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in message
news:exxqlFW4FHA.3292@.tk2msftngp13.phx.gbl...
> Print converts to a string. What do you get when you say SELECT
GETDATE()?
> Different, no?
> Keep in mind that I am having problems understanding your date for the
very
> reason that you chose an ambiguous date format. I suggest using YYYYMMDD
or
> YYYY-MM-DDTHH:MM:SS. These are ISO standards and are truthfully the only
> safe formats you can use to represent dates in SQL Server. Anything else
> can be misinterpreted by the database depending on regional settings,
> dateformat, language, etc.
> What do you mean by "compare it to the current time"? Compare what? A
> value in a table?
>
>
> "Ron Wallegie" <wallegie@.hotmail.com> wrote in message
> news:eWk8U$V4FHA.2432@.TK2MSFTNGP10.phx.gbl...
the
the
>|||Just to clairify...
SET DATEFORMAT DMY -- i am assuming it's 2nd oct '05
DECLARE @.ClientDate SMALLDATETIME , @.TimeNow SMALLDATETIME
SELECT @.ClientDate = '02/10/2005 17:40:00' , @.TimeNow = GETDATE()
SELECT
@.ClientDate
, @.TimeNow
, (CASE
WHEN DATEDIFF( mi , @.ClientDate , @.TimeNow ) = 0 THEN 'Same'
ELSE 'Different'
END) AS DateCompare
"Rebecca York" <rebecca.york {at} 2ndbyte.com> wrote in message
news:436b8e34$0$140$7b0f0fd3@.mistral.news.newnet.co.uk...
> If you want to compare two dates to see if they are the same.
> SELECT
> (CASE WHEN DATEDIFF( ms , myDateField , GETDATE() ) = 0 THEN 'Same'
ELSE
> 'Different' END) AS DateCompare
> FROM
> myTable
> If you're using SMALLDATETIME you can use DATEDIFF( mi , ... ), because it
> is only accurate to a minute.
>
> "Ron Wallegie" <wallegie@.hotmail.com> wrote in message
> news:eWk8U$V4FHA.2432@.TK2MSFTNGP10.phx.gbl...
> the
> the
> returns
>|||Why do you think you need to format dates to compare them? What exactly are
you trying to do?
ML|||When starting a procedure iwant to pass all the records in a table before
the getdate() value. After the procedure stops it deletes all value before
the getdat value.
I know there is a db setting which controls the format you get when rinting
getdate(). I don't know how...
"ML" <ML@.discussions.microsoft.com> wrote in message
news:0F8F8DE5-4774-491B-A86E-41381F563B9F@.microsoft.com...
> Why do you think you need to format dates to compare them? What exactly
are
> you trying to do?
>
> ML|||Please stop thinking about the format. ***Format is irrelevant.*** You are
dealing with datetime values here, not strings. Not sure why you need to
print or select the date at all.
DECLARE @.dt SMALLDATETIME
SET @.dt = CURRENT_TIMESTAMP
-- "pass" all the rows, I have no idea what this means!
-- SELECT, maybe?
SELECT cols FROM table WHERE dt_column <= @.dt
DELETE table WHERE dt_column <= @.dt
ok?
"Ron Wallegie" <wallegie@.planet.nl> wrote in message
news:usyM%23bW4FHA.3460@.TK2MSFTNGP12.phx.gbl...
> When starting a procedure iwant to pass all the records in a table before
> the getdate() value. After the procedure stops it deletes all value before
> the getdat value.
> I know there is a db setting which controls the format you get when
> rinting
> getdate(). I don't know how...
> "ML" <ML@.discussions.microsoft.com> wrote in message
> news:0F8F8DE5-4774-491B-A86E-41381F563B9F@.microsoft.com...
> are
>
Sunday, March 11, 2012
Date function for calculated member?
Hello,
In AS2000 I was using a VBA function Date to retrieve the current date and use it as part as my calculated member formula.
In AS2005, you cannot use VBA function and I do not find a way to retrieve the current date (without the time or with the time set to 00:00:00)
I tried to create a Named query in the DS but I cannot seem to be able to use this in the calculated member.
Any Idea?
Thanks,
Philippe
You can use the "Now()" function. Here is an example that I use to create a member based on the current system day:
--Create a date string using the following format: "9-Jun-06"
CREATE HIDDEN [As of Date String] =
Format(Now(),"d-") +
Left(Format(Now(),"m"),3) + "-" +
Right(CStr(DatePart("yyyy",Now())),2);
CREATE MEMBER CURRENTCUBE.[Date].[Calendar].[As of Date]
AS
StrToMember("[Date].[Calendar].[" + [As of Date String] + "]");
HTH,
Steve
|||Steve,
Very useful! However, I am not able to recreate this in my cube. I added the following code to my cube's calculations (I'm using SSAS 2005 btw):
CREATE HIDDEN [Current Date String] =
CStr(Format(Now(), "MM/")) +
CStr(Format(Now(), "dd/")) +
CStr(DatePart("yyyy",Now()));
CREATE MEMBER CURRENTCUBE.[Time].[Fiscal Time].[Current Week]
AS
StrToMember("[Date].[Fiscal Time].[Actual Date].[" + [Current Date String] + "].Parent");
So when I browse my cube, I see the "Current Week" calculated member under the [Time].[Fiscal Time] members. However, when I drop it in the filters section of the browser, nothing happens. Am I missing a step?
|||Todd,
If you are testing this using the browser in BIDS, the filter set will not work with a calculated member. You can test by simply using the same MDX to create a "set" for testing purposes. Try adding the following and then using the set in your filter statement:
CREATE SET CURRENTCUBE.[Current Week Test Set]
AS
StrToSet("[Date].[Fiscal Time].[Actual Date].[" + [Current Date String] + "].Parent");
HTH,
Steve
Date function
SELECT fname, lname, hire_date
FROM EMPLOYEE
WHERE
DATEDIFF(yy, hire_date, GETDATE()+90) >
DATEDIFF(yy, hire_date, GETDATE())
but it does not return any result. How should I rewrite the statement?
use the following query..
Code Snippet
SELECT fname, lname, hire_date
FROM EMPLOYEE
WHERE
DATEDIFF(yy, hire_date, GETDATE()) <= 90
|||This is not quite what I needed. I want to find employees who would celebrate their anniversary date within the next 30 days.For example, if today is 2007-05-29, the following employees would appear on the list:
John Doe, hire_date 1992-06-07
Jane Doe, hire_date 1995-06-25
The following employees would not appear on the list:
James Doe, hire_date 1996-05-28
Jill Doe, hire_date 1997-06-29
|||
SELECT fname
, lname
, hire_date
FROM EMPLOYEE
WHERE DATEPART(month,hire_date) BETWEEN DATEPART(month,GETDATE() AND DATEPART(month,DATEADD(day, 30, getdate()))|||
Ok.. Use the following query..
Code Snippet
Select *
From
(
SELECT
fname,
lname,
hire_date,
DateAdd(yy, Datediff(YY,hire_date,Getdate()),hire_date) CelebrateOn
FROM
EMPLOYEE
) as Data
Where
CelebrateOn Between
Cast(Convert(varchar,Getdate(),101) as Datetime) -- To eliminate the Time from Getdate()
And Cast(Convert(varchar,Getdate(),101) as Datetime) + 30 -- To eliminate the Time from Getdate()
|||i'm sorry, i think this one is betterSELECT fname
, lname
, hire_date
FROM EMPLOYEE
WHERE DATEADD(year,DATEDIFF(year,hire_date,GETDATE()),hire_date) BETWEEN GETDATE() AND DATEADD(day, 30, getdate())|||
Very nicely done Rhamille.
I think that if you make the following modification, the query will be able to use any indexing on HireDate. (The way it is currently written would negate the possibility of using indexing.
(This example uses AdventureWorks.)
Code Snippet
SELECT
e.EmployeeID,
c.FirstName,
c.LastName,
e.HireDate,
FROM HumanResources.Employee e
JOIN Person.Contact c
ON e.ContactID = c.contactID
WHERE e.HireDate BETWEEN ( dateadd( year, datediff( year, getdate(), e.HireDate ), getdate()))
AND ( dateadd( year, datediff( year, getdate(), e.HireDate ), getdate()) + 30 )
Arnie Rowland wrote:
Very nicely done Rhamille.
I think that if you make the following modification, the query will be able to use any indexing on HireDate. (The way it is currently written would negate the possibility of using indexing.
(This example uses AdventureWorks.)
Code Snippet
SELECT
e.EmployeeID,
c.FirstName,
c.LastName,
e.HireDate,
FROM HumanResources.Employee e
JOIN Person.Contact c
ON e.ContactID = c.contactID
WHERE e.HireDate BETWEEN ( dateadd( year, datediff( year, getdate(), e.HireDate ), getdate()))
AND ( dateadd( year, datediff( year, getdate(), e.HireDate ), getdate()) + 30 )
good idea arnie.. instead of moving the HireDate to the present date, we should move the current date back to year of the HireDate, so that HireDate can be indexed. i'll take note of this one.. and make it to practice of always considering indexed columns
|||Thanks, Rhamille, Arnie. Both work as intended.
|||This query rewrite will not help if there is an index on HireDate. You still reference the column on both sides of the predicate. You need to have at least some known value on the right-hand side or column from another table for this to work. The above WHERE clause needs to look at every row due to reference to HireDate on either sides of the predicate.|||
Umachandar,
Initially, I had the same impression as you, that having the HireDate field on the right side of the predicate would be significantly different than having a constant value. However, the execution plans are the same in both situations -both require an index scan and seek. (You'll notice that I used both a variable and a constant value on the right side of the predicate.
So it seems to me that there is no penalty for using the HireDate field on the right side of the predicate. It still requires an index scan/seek operation. Granted, there will be a small additional effort to apply the function to the HireDate value as the scan is occurring.
Perhaps I'm missing something...
StmtText
-
SELECT
e.EmployeeID,
c.FirstName,
c.LastName,
e.HireDate
FROM HumanResources.Employee e
JOIN Person.Contact c
ON e.ContactID = c.contactID
WHERE e.HireDate BETWEEN ( dateadd( year, datediff( year, getdate(), e.HireDate ), getdate()))
AND ( dateadd( year, datediff( year, getdate(), e.HireDate ), getdate()) + 30 )
StmtText
-
|--Nested Loops(Inner Join, OUTER REFERENCES.[ContactID], [Expr1008]) WITH UNORDERED PREFETCH)
Loops(Inner Join, OUTER REFERENCES.[ContactID], [Expr1008]) WITH UNORDERED PREFETCH)
|--Clustered Index Scan(OBJECT[AdventureWorks].[HumanResources].[Employee].[PK_Employee_EmployeeID] AS ), WHERE[AdventureWorks].[HumanResources].[Employee].[HireDate] as .[HireDate]>=dateadd(year,datediff(year,getdate(),[AdventureWorks].[HumanResources].[Employee].[HireDate] as .[HireDate]),getdate()) AND [AdventureWorks].[HumanResources].[Employee].[HireDate] as .[HireDate]<=(dateadd(year,datediff(year,getdate(),[AdventureWorks].[HumanResources].[Employee].[HireDate] as .[HireDate]),getdate())+'1900-01-31 00:00:00.000')))
|--Clustered Index Seek(OBJECT[AdventureWorks].[Person].[Contact].[PK_Contact_ContactID] AS ), SEEK.[ContactID]=[AdventureWorks].[HumanResources].[Employee].[ContactID] as .[ContactID]) ORDERED FORWARD)
StmtText
-
DECLARE @.HireDate datetime
SET @.HireDate = '1999/06/04'
SELECT
e.EmployeeID,
c.FirstName,
c.LastName,
e.HireDate
FROM HumanResources.Employee e
JOIN Person.Contact c
ON e.ContactID = c.contactID
WHERE e.HireDate BETWEEN @.HireDate
AND @.HireDate + 30
StmtText
-
|--Nested Loops(Inner Join, OUTER REFERENCES.[ContactID], [Expr1005]) WITH UNORDERED PREFETCH)
|--Clustered Index Scan(OBJECT[AdventureWorks].[HumanResources].[Employee].[PK_Employee_EmployeeID] AS ), WHERE[AdventureWorks].[HumanResources].[Employee].[HireDate] as .[HireDate]>=[@.HireDate] AND [AdventureWorks].[HumanResources].[Employee].[HireDate] as .[HireDate]<=[@.HireDate]+'1900-01-31 00:00:00.000'))
|--Clustered Index Seek(OBJECT[AdventureWorks].[Person].[Contact].[PK_Contact_ContactID] AS ), SEEK.[ContactID]=[AdventureWorks].[HumanResources].[Employee].[ContactID] as .[ContactID]) ORDERED FORWARD)
StmtText
-
SELECT
e.EmployeeID,
c.FirstName,
c.LastName,
e.HireDate
FROM HumanResources.Employee e
JOIN Person.Contact c
ON e.ContactID = c.contactID
WHERE e.HireDate BETWEEN '1999/06/04'
AND '1999/07/04'
StmtText
-
|--Nested Loops(Inner Join, OUTER REFERENCES.[ContactID]))
|--Clustered Index Scan(OBJECT[AdventureWorks].[HumanResources].[Employee].[PK_Employee_EmployeeID] AS ), WHERE[AdventureWorks].[HumanResources].[Employee].[HireDate] as .[HireDate]>='1999-06-04 00:00:00.000' AND [AdventureWorks].[HumanResources].[Employee].[HireDate] as .[HireDate]<='1999-07-04 00:00:00.000'))
|--Clustered Index Seek(OBJECT[AdventureWorks].[Person].[Contact].[PK_Contact_ContactID] AS ), SEEK.[ContactID]=[AdventureWorks].[HumanResources].[Employee].[ContactID] as .[ContactID]) ORDERED FORWARD)
Wednesday, March 7, 2012
Date format
i am storing dates as datetime, so let us say the date is 2004-07-15 11:42:43.780. Now i want to retrieve it exactly like this. Unfortunately it doesnot come out like this.
It is coming out like this Jul 15 2004 11:44AM and i'm losing the seconds
and milliseconds.
Could anyone please advise me how to get 2004-07-15 11:42:43.780.
Thanks in advance.
-ssSelect Convert (varchar, Getdate(), 121)|||Thanks you very much|||Hi experts
Can any one help me !!!
I had also having the same type or problem !
select msgdate from emc_messageinfo
Sql server 2000 returns the following
2004-07-01 15:04:10.000
and i need only the current date means "01-07-2004" ,but not in varchar format i need in date format only so that i can use it further
Thanks and regards
Manish Kaushik|||MS-SQL doesn't distinguish between date and time, there is a single datatype that is DATETIME (and a sub-set of it that is SMALLDATETIME). You can truncate the value to either a date or a time to make computation easier, but you can't truly isolate the date from the time. Try using:SELECT Convert(CHAR(10), Getdate(), 121)-PatP
Sunday, February 19, 2012
Date conversion.
)?SELECT
DATEPART(MONTH, GETDATE()) [MonthColumn]
,DATEPART(YEAR, GETDATE()) [YearColumn]
,CAST(DATEPART(MONTH, GETDATE()) AS VARCHAR(2)) + '/' +
CAST(DATEPART(MONTH, GETDATE()) AS Char(4)) [MonthAndYear]
Simon Worth
Arul wrote:
> Is there a function I can use to retrieve the month and year of say getdate()?[/co
lor]|||Sorry, typo
SELECT
DATEPART(MONTH, GETDATE()) [MonthColumn]
,DATEPART(YEAR, GETDATE()) [YearColumn]
,CAST(DATEPART(MONTH, GETDATE()) AS VARCHAR(2)) + '/' +
CAST(DATEPART(YEAR, GETDATE()) AS Char(4)) [MonthAndYear]
Simon Worth
Simon Worth wrote:
> SELECT
> DATEPART(MONTH, GETDATE()) [MonthColumn]
> ,DATEPART(YEAR, GETDATE()) [YearColumn]
> ,CAST(DATEPART(MONTH, GETDATE()) AS VARCHAR(2)) + '/' +
> CAST(DATEPART(MONTH, GETDATE()) AS Char(4)) [MonthAndYear]
> Simon Worth
> Arul wrote:
>|||Did you get any error?
Madhivanan|||Did I get an error?
No.
SELECT
DATEPART(MONTH, GETDATE()) [MonthColumn]
,DATEPART(YEAR, GETDATE()) [YearColumn]
,CAST(DATEPART(MONTH, GETDATE()) AS VARCHAR(2)) + '/' +
CAST(DATEPART(YEAR, GETDATE()) AS Char(4)) [MonthAndYear]
MonthColumn,YearColumn,MonthAndYear
3,2005,3/2005
(1 row(s) affected)
Simon Worth
Madhivanan wrote:
> Did you get any error?
> Madhivanan
>