TSQL question: How can I perform a calculation on on multiple values?

Posted on 2010-03-25
Medium Priority
Last Modified: 2012-05-09
Hello Experts,

I need to return values like this '131531013' into a date format like 2007/1/5. I have the math for it:
declare @tsdate int
set @tsdate = 131531013
declare @day int      
declare @month int      
declare @year int      

select @day = @tsdate % 256      
select @month = (@tsdate % 65536) / 256      
select @year = @tsdate / 65536      

but I'm stuck on getting it into a multi-row dataset.

Question by:glo-dba
LVL 22

Accepted Solution

Om Prakash earned 1000 total points
ID: 28552996
Please check the code
Create table myDate (dtString varchar(20))
--Consider you have created above table
insert into myDate values ('131531013')
--And inserted date you mentioend
select * from #myDate 
--Select Query
select cast((dtString  % 256) as varchar(2)) + '/' + cast(((dtString  % 65536) / 256) as varchar(2)) + '/' + cast((dtString / 65536) as varchar(4)) as [myDate] from #myDate 

Open in new window

LVL 53

Expert Comment

ID: 28601522
First, what database are you using: MS SQL Server or MySQL? The question tags list both types ...

> but I'm stuck on getting it into a multi-row dataset

If you mean run that logic on a column of values, yes ... you could put the logic into a UDF. Then call the UDF from your query.  Example in MS SQL:

    SELECT    dbo.ConvertIntToDate( SomeIntegerCol ) FROM  TheTable ....


Also, is your int value ("131531013") some sort of epoch? Just wondering if there's an easier way to do the conversion.

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this article, we’ll look at how to deploy ProxySQL.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
Suggested Courses

594 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question