Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to convert into current date?

Author  Topic 

tantcu
Yak Posting Veteran

58 Posts

Posted - 2012-07-05 : 09:24:46
Hi guys,

I'm trying to convert this table from the database into the current date. However, the problem I encounter is this table contains 'acutal date' data instead of a string text.This is what I have tried so far but it did not work.
[P21].[dbo].[p21_view_inv_loc].date_created BETWEEN LEFT(CONVERT(CHAR(8), DATEADD(MM, -4, getdate()),112),6) AND LEFT(CONVERT(CHAR(8), DATEADD(MM, -1, getdate()),112),6)

But if I use the same function for this table, it works because it contains a 'text' not actual date.

[P21].[dbo].[p21_inventory_usage_all_view].year_and_period BETWEEN LEFT(CONVERT(CHAR(8), DATEADD(MM, -4, getdate()),112),6) AND LEFT(CONVERT(CHAR(8), DATEADD(MM, -1, getdate()),112),6)

I have to use the 'date.created' table to get the correct data. So if you guys know how to convert it, please let me know.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-05 : 09:31:46
it should be

[P21].[dbo].[p21_view_inv_loc].date_created >= DATEADD(mm,DATEDIFF(mm,0,GETDATE())-4,0)
AND [P21].[dbo].[p21_view_inv_loc].date_created < DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)

also learn to use short aliases rather than repeating four part names everywhere


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

tantcu
Yak Posting Veteran

58 Posts

Posted - 2012-07-05 : 09:38:31
Thank you
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-05 : 09:43:01
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -