Sunday, February 12, 2012

Convert Datetime.

Hello All,

In following statement in SQL, I'm first converting 'IssueDate' with style 101 into 'nvarchar' then converting to Datatime.

convert(datetime,convert (nvarchar,Cert_WarehouseDetails.IssuedDateX,101)) <= '3/29/2004')

Which is right in below one.

1. Do I need to first convert into nvarchar then datetime.
e.g. convert(datetime,convert (nvarchar,Cert_WarehouseDetails.IssuedDateX,101)) <= '3/29/2004')

2. Otherwise can I directly convert into datetime.

convert(datetime,Cert_WarehouseDetails.IssuedDateX ,101) <= '3/29/2004')

Please reply to me asap.

Regards,
M. G.Do this

WHERE DATEDIFF(d,'3/29/2004',IssuedDateX) < 0|||I would use:Convert(CHAR(10), Cert_WarehouseDetails.IssuedDateX, 121)if you intend to convert it back to a datetime (to strip off the time).

-PatP|||Doooh...

Either way it's a scan...

USE Northwind
GO

--Type [CTRL]+K

--Index Scan

SELECT OrderDate FROM Orders
WHERE DATEDIFF(d,'7/11/1996',OrderDate) < 0

--Index Seek

SELECT OrderDate FROM Orders
WHERE OrderDate <= '7/11/1996'
GO

No comments:

Post a Comment