SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Sql query - select * and apply dateadd to 2 column
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mehow
Starting Member

6 Posts

Posted - 03/06/2013 :  05:35:22  Show Profile  Reply with Quote
Hello.
I am a proper beginner in the world of sql, so i will be really thankful if someone can help me with my problem.
I am trying to display all columns from a table and apply some formatting(formula) to only 1 maybe 2 of them.
logic example:

apply: DATEADD(day,CURDATE/1440, '01/01/1988')

SELECT * and apply FROM tableONE

I hope this makes sense! Cheers

MIK_2008
Flowing Fount of Yak Knowledge

Pakistan
1052 Posts

Posted - 03/06/2013 :  05:59:00  Show Profile  Reply with Quote
SELECT DATEADD(day,CURDATE/1440, '01/01/1988'), * FROM TableOne;


If CurDate/1440 results in a figure with decimal places then they will automatically be ignored .. e.g. if the result of CurDate/1440 = 1.6 .. it will be considered as 1. Thanks!

Cheers
MIK
Go to Top of Page

mehow
Starting Member

6 Posts

Posted - 03/06/2013 :  06:04:45  Show Profile  Reply with Quote
the result of your query its that 1st column (A) stores the results of DATEADD and then from column 2(B) on theres is the rest of columns including CURDATE

dateadd returns a date stored in database as BIGINT converted to a DATE format that excel reads as dd/mm/yyyy


what i am trying to do its to display all columns but in place of CURDATE I'd like to have values already converted.

So thank you for trying but this was not the solution :(
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 03/06/2013 :  06:07:14  Show Profile  Reply with Quote
quote:
Originally posted by mehow

the result of your query its that 1st column (A) stores the results of DATEADD and then from column 2(B) on theres is the rest of columns including CURDATE

dateadd returns a date stored in database as BIGINT converted to a DATE format that excel reads as dd/mm/yyyy


what i am trying to do its to display all columns but in place of CURDATE I'd like to have values already converted.

So thank you for trying but this was not the solution :(


How do you want values to get converted? whats the output format you're looking at?

Also why is base datatype bigint if you're storing date values? ALways try to use proper datatype to avoid unnecessary conversion operations

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

Go to Top of Page

mehow
Starting Member

6 Posts

Posted - 03/06/2013 :  06:28:21  Show Profile  Reply with Quote
I have not designed the database I am working with. I am using 'Priority 15's database.

the conversion is done with this statement : DATEADD(day,CURDATE/1440, '01/01/1988')

I will try to explain this again.

example:
There is a table called TableOne
There are 3 columns in TableOne - NAMEA, CURDATE, NAMEB
NAMEA is VARCHAR(20)
CURDATE is BIGINT datatype ( value 13163040 represents 10/01/2013 )
NAMEB is VARCHAR(20)

Now, if I wanted to display all columns from TableOne so SELECT * FROM TableOne
But, if I wanted to apply this: DATEADD(day,CURDATE/1440, '01/01/1988') to column CURDATE and then display all columns - then how do i do that?
Go to Top of Page

MIK_2008
Flowing Fount of Yak Knowledge

Pakistan
1052 Posts

Posted - 03/06/2013 :  06:44:45  Show Profile  Reply with Quote
How you find that the value "13163040" represents 10/01/2013?

Cheers
MIK
Go to Top of Page

mehow
Starting Member

6 Posts

Posted - 03/06/2013 :  06:47:47  Show Profile  Reply with Quote
DATEADD(day,CURDATE/1440, '01/01/1988') <- converts to date


database im using uses BIGINT to represent dates. So 0 is 01/01/1988 then next day you add 0 + 1440.

Edited by - mehow on 03/06/2013 06:49:33
Go to Top of Page

MIK_2008
Flowing Fount of Yak Knowledge

Pakistan
1052 Posts

Posted - 03/06/2013 :  06:57:31  Show Profile  Reply with Quote
Do you mean not to pull CurDate instead of it the "DATEADD(day,CURDATE/1440, '01/01/1988')"? If so may be this is something you're looking for

SELECT NameA,DATEADD(day,CURDATE/1440, '01/01/1988'),NameB FROM TableOne;

By the way there is no direct connection of CurDate with a date. You're take a raference '01/01/1988' and simply adding no of days to this date using the DateAdd function. Read this http://msdn.microsoft.com/en-us/library/ms186819.aspx and undestand the logic. I hope you'll be able to solve your problem yourself in case my guess is still not correct.

Cheers
MIK
Go to Top of Page

mehow
Starting Member

6 Posts

Posted - 03/06/2013 :  06:57:46  Show Profile  Reply with Quote
This is what I want: SELECT NAMEA, DATEADD(day,CURDATE/1440, '01/01/1988'), NAMEB FROM TableOne
but in reallife scenario there are 200 columns and I want to skip listing all 200 just to convert one column.
Go to Top of Page

mehow
Starting Member

6 Posts

Posted - 03/06/2013 :  07:07:17  Show Profile  Reply with Quote
MIK, I know im using a reference - that's what i want to use cause I can't modify the database ( its got its own user interface and changing datatypes,logically thinking, is not allowed )

I am pulling this data out of the database into an excel spreadsheet, and thats why I need to convert the 1234567 to a normal format date.

so once again: This is what I want: SELECT NAMEA, DATEADD(day,CURDATE/1440, '01/01/1988'), NAMEB FROM TableOne
but in reallife scenario there are 200 columns and I want to skip listing all 200 just to convert one column.

* If CURDATE is column number 55 then I was thinking of using aliases to name first 54 (ex. Batch1 ) then CURDATE then the remaining 56-200 (ex. Batch2 )
so, then
SELECT batch1, DATEADD(day,CURDATE/1440, '01/01/1988'), batch2


BUT theres gotta be an easier, neater, and better developed way of doing this!

Go to Top of Page

MIK_2008
Flowing Fount of Yak Knowledge

Pakistan
1052 Posts

Posted - 03/06/2013 :  07:08:34  Show Profile  Reply with Quote
just explicitly name them instead using * ..

SELECT Column1,Column2,...,ColumnN
Form TableName

Cheers
MIK
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 03/06/2013 :  10:36:55  Show Profile  Reply with Quote
quote:
Originally posted by mehow

MIK, I know im using a reference - that's what i want to use cause I can't modify the database ( its got its own user interface and changing datatypes,logically thinking, is not allowed )

I am pulling this data out of the database into an excel spreadsheet, and thats why I need to convert the 1234567 to a normal format date.

so once again: This is what I want: SELECT NAMEA, DATEADD(day,CURDATE/1440, '01/01/1988'), NAMEB FROM TableOne
but in reallife scenario there are 200 columns and I want to skip listing all 200 just to convert one column.

* If CURDATE is column number 55 then I was thinking of using aliases to name first 54 (ex. Batch1 ) then CURDATE then the remaining 56-200 (ex. Batch2 )
so, then
SELECT batch1, DATEADD(day,CURDATE/1440, '01/01/1988'), batch2


BUT theres gotta be an easier, neater, and better developed way of doing this!




you cant use alias to replace group of columns. You've to individually list them out!

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

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000