Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Cast() within isnull or outside?
Message
From
14/09/2007 12:57:45
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Environment versions
SQL Server:
SQL Server 2005
Miscellaneous
Thread ID:
01254374
Message ID:
01254382
Views:
24
This message has been marked as the solution to the initial question of the thread.
>>Hi All,
>>
>>I have a select statement (huge) and I need to change data type DT to basically time, but since SQL Server does not do time, to a character string. I was thinking that I could just do a cast() on it, but since I already have an ISnull() around it, I am not sure where to put the cast() or convert().
>>
>>
>>  isnull(tab1.RunSrv_TimeAtSrv,'') AS AM_RunSrv_TimeAtSrv,
>>
>>
>>so maybe:
>>
>>  isnull(convert(char(8),tab1.RunSrv_TimeAtSrv, 108),'') AS AM_RunSrv_TimeAtSrv,
>>
>>
>>this gettes me hh:mm:ss, what I need is hh:mm AM/PM
>>
>>Anyone know how I can get that?
>>
>>TIA
>>Beth
>
>
>DECLARE @Test DATETIME
>SET @Test = GETDATE()
>SELECT ISNULL(SUBSTRING(CONVERT(char(27), @test, 100),14,6),'')
>
>
>ISNULL must be the first function. Try this to see what happens:
>
>
>DECLARE @Test DATETIME
>--SET @Test = GETDATE()
>SELECT ISNULL(SUBSTRING(CONVERT(char(27), @test, 100),14,6),'')
>
>SELECT SUBSTRING(CONVERT(char(27), ISNULL(@test,''), 100),14,6)
>
Hi Borislav,
I tried your example, but had to change it slightly, to get the correct result.
DECLARE @Test DATETIME
SET @Test = GETDATE()
SELECT ISNULL(SUBSTRING(CONVERT(char(27), @test, 100),14,6),'')

SELECT SUBSTRING(CONVERT(char(27), ISNULL(@test,''), 100),13,7)
not sure why it was 1 character off like that. But thanks for the help.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform