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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Case

Author  Topic 

Exir
Posting Yak Master

151 Posts

Posted - 2009-06-02 : 02:10:59
Hi
Is it a true query for changing @SMonth ?
If @SMonth=1 i want it to change to 01 , if @SMonth=2 then change to 02 and so on

SELECT @SMonth=
CASE
WHEN @SMonth=1 THEN 01
WHEN @SMonth=2 THEN 02
WHEN @SMonth=3 THEN 03
WHEN @SMonth=4 THEN 04
END

thank you

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-06-02 : 02:13:46
If @SMONTH is not a char or varchar then leading zero will always go away...
If @SMONTH is char or varchar then it should be like
WHEN @SMonth='1' THEN '01'


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-06-02 : 02:16:25
But then you can do
@SMONTH = right('00'+ltrim(rtrim(@MONTH)),2) -- if @SMONTH is char or varchar


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-06-02 : 02:17:16
quote:
Originally posted by Exir

Hi
Is it a true query for changing @SMonth ?
If @SMonth=1 i want it to change to 01 , if @SMonth=2 then change to 02 and so on

SELECT @SMonth=
CASE
WHEN @SMonth=1 THEN 01
WHEN @SMonth=2 THEN 02
WHEN @SMonth=3 THEN 03
WHEN @SMonth=4 THEN 04
END

thank you



what is the purpose of this ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Exir
Posting Yak Master

151 Posts

Posted - 2009-06-02 : 02:35:17
quote:
Originally posted by khtan

quote:
Originally posted by Exir

Hi
Is it a true query for changing @SMonth ?
If @SMonth=1 i want it to change to 01 , if @SMonth=2 then change to 02 and so on

SELECT @SMonth=
CASE
WHEN @SMonth=1 THEN 01
WHEN @SMonth=2 THEN 02
WHEN @SMonth=3 THEN 03
WHEN @SMonth=4 THEN 04
END

thank you



what is the purpose of this ?


KH
[spoiler]Time is always against us[/spoiler]





I want to have some calculation on the date, so the month should be saved like 02 not 2
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-06-02 : 02:39:08
Having the month in 02 and not 2 means it will be in string / varchar. How are you going to perform any calculation on the date ?

Anyway "02 not 2" is more of a presentation issue and should be handled in your client application


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-06-02 : 02:39:16
@SMONTH = right('00' + convert(varchar(2),@MONTH),2) -- if @SMONTH is numeric



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Exir
Posting Yak Master

151 Posts

Posted - 2009-06-02 : 02:41:05
I inserted it to my code but after insertion it returns null :(
Can u undrestand where is the problem?

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER FUNCTION [dbo].[MiladiTOShamsi] (@MDate DateTime)
RETURNS Varchar(10)
AS
BEGIN
DECLARE @SYear as Integer
DECLARE @SMonth as nvarchar(2)
DECLARE @SDay as nvarchar(2)
DECLARE @AllDays as float
DECLARE @ShiftDays as float
DECLARE @OneYear as float
DECLARE @LeftDays as float
DECLARE @YearDay as Integer
DECLARE @Farsi_Date as Varchar(100)
SET @MDate=@MDate-CONVERT(char,@MDate,114)

SET @ShiftDays=466699 +2
SET @OneYear= 365.24199


SET @SYear = 0
SET @SMonth = 0
SET @SDay = 0
SET @AllDays = CAst(@Mdate as Real)

SET @AllDays = @AllDays + @ShiftDays

SET @SYear = (@AllDays / @OneYear) --trunc
SET @LeftDays = @AllDays - @SYear * @OneYear

if (@LeftDays < 0.5)
begin
SET @SYear=@SYear+1
SET @LeftDays = @AllDays - @SYear * @OneYear
end;

SET @YearDay = @LeftDays --trunc
if (@LeftDays - @YearDay) >= 0.5
SET @YearDay=@YearDay+1

if ((@YearDay / 31) > 6 )
begin
SET @SMonth = 6
SET @YearDay=@YearDay-(6 * 31)
SET @SMonth= @SMonth+( @YearDay / 30)
if (@YearDay % 30) <> 0
SET @SMonth=@SMonth+1
SET @YearDay=@YearDay-((@SMonth - 7) * 30)
end
else
begin
SET @SMonth = @YearDay / 31
if (@YearDay % 31) <> 0
SET @SMonth=@SMonth+1
SET @YearDay=@YearDay-((@SMonth - 1) * 31)
end
SET @SDay = @YearDay
SET @SYear=@SYear+1

//------------------INSERTED PART
select @SDay=
CASE
WHEN @SDay='1' THEN '01'
WHEN @SDay='2' THEN '02'
WHEN @SDay='3' THEN '03'
WHEN @SDay='4' THEN '04'
WHEN @SDay='5' THEN '05'
WHEN @SDay='6' THEN '06'
WHEN @SDay='7' THEN '07'
WHEN @SDay='8' THEN '08'
WHEN @SDay='9' THEN '09'
END

select @SMonth=
CASE
WHEN @SMonth='1' THEN '01'
WHEN @SMonth='2' THEN '02'
WHEN @SMonth='3' THEN '03'
WHEN @SMonth='4' THEN '04'
WHEN @SMonth='5' THEN '05'
WHEN @SMonth='6' THEN '06'
WHEN @SMonth='7' THEN '07'
WHEN @SMonth='8' THEN '08'
WHEN @SMonth='9' THEN '09'
END
//------------------------------

SET @Farsi_Date = CAST (@SYear as VarChar(10)) + '/' + CAST (@SMonth as VarChar(10)) + '/' + CAST (@SDay as VarChar(10))
Return @Farsi_Date



END

Go to Top of Page

Exir
Posting Yak Master

151 Posts

Posted - 2009-06-02 : 02:46:29
quote:
Originally posted by khtan

Having the month in 02 and not 2 means it will be in string / varchar. How are you going to perform any calculation on the date ?

Anyway "02 not 2" is more of a presentation issue and should be handled in your client application


KH
[spoiler]Time is always against us[/spoiler]





when i omit '/' from the date , it become like a number, then i can compare dates with this method.
I can not use date/time , becouse my country date is not the date which getdate() returns and i have to convert it.

i hope u undrestand my problem
Go to Top of Page

Exir
Posting Yak Master

151 Posts

Posted - 2009-06-02 : 03:23:38
I used if instead of case and it worked, but still dont know why the case command was not working
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-06-02 : 03:30:52
quote:
I can not use date/time , becouse my country date is not the date which getdate() returns and i have to convert it.

You should use datetime. getdate() returns datetime data type. A datetime data type is not in any specific format like YYYY-MM-DD or DD-MM-YYYY or MM-DD-YYYY. Datetime is just a data type for storing date & time.

Your function should return datetime data type and not string.

I have not idea what your function is doing but whatever it is, once you calculated the final result in Year, Month and Day use the function here http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=22339 to convert to datetime and return.


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-06-02 : 03:34:12
quote:
DECLARE @SYear as Integer
DECLARE @SMonth as nvarchar(2)
DECLARE @SDay as nvarchar(2)


Why @SMonth & @SDate is in nvarchar() ? shouldn't these be Integer ?

quote:
SET @MDate=@MDate-CONVERT(char,@MDate,114)

it is better to use this.
select @MDate = dateadd(day, datediff(day, 0, @MDate), 0)

quote:
SET @AllDays = CAst(@Mdate as Real)

it is same as
select @AllDays = datediff(day, 0, @MDate)



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -