DCSIMG
February 2008 - Posts - Natalie Reznik

Natalie Reznik

"It's not a bug, it's a feature !" Unknown Programmer

February 2008 - Posts

SQL - how to convert date that is nvarchar into datetime type and another format

This is unbelievable! I was afraid that 10 wins in a row is kinda not real and might make the players believe they are undefeatable. This had to stop some time , but the cup's final? After we lead 22 points??? ouch, it hurts...

So I cannot sleep tonight, so I desided to bring out something positive out of this "bizayon" - I cannot find another word.

Today I had an interesting task : there was a table where one field was a date but saved as a nvarchar, and was american date style MM/dd/yy h:/mm:ss PM/AM which I needed to insert into another field but as datetime and in an appropriate for Israel 113 format - dd/MM/yyyy hh:mm:ss.

 tbl_SomeTable
|---------------------------|-----------------|
|    nvarchar_timefield     |  DateTimeField  |
|---------------------------|-----------------|
|  "11/26/2005 5:45:30 PM"  |       ?         |
|---------------------------|-----------------|

My first thought was - ah , this is going to be hard , with dateparts and things like that. Thank god, I first tried something easier that I thought might work. Laziness is the engine of progress - that's what my grandfather always said, 'cause a hardworking person would never come up with a washing mashine! So take a look, isn't it nice?

UPDATE    tbl_SomeTable
SET       DateTimeField = Convert(datetime,Convert(datetime, nvarchar_timefield ),113)
Where
nvarchar_timefield is not null

What basically happens here is :  convert nvarchar_timefield to datetime type and than turn it into format 113. That's it! Hope it helps someone.

And congrats to hapoel and their fans, well, you deserve it.

SQL select on several different tables that are in different databases and on different servers that are not in the same subnet mask

Recently I needed to run a select on 5 different tables that sat on different servers spread all over the internet. At a certain point i almost gave up, as i thought it can't be done. But i believed in the abilities of MS SQLServer 2005 that helped me out so many times and kept searching and trying all sorts of tricks. Here's the final result:

1. When you want to connect from one server to another you need to make sure that the Sql Server Agent is with a green arrow on the server you connect to:

2. go to the server objects  --->  linked servers add new linked server .

When you add a new linked server you add it by its name (and not by IP), which would be just fine if those servers are in the same subnet mask. But in my case they were not, and because of that they couldn't find each other.

3. to overcome this problem you need to "tell" the system where to look for that server and that you do in the file named "hosts" that can be found at:

C:\WINDOWS\system32\drivers\etc

this a text file and all you need to do is add at the bottom of this file "registration" for your server like this:

123.45.6.789  MyServer

where 123.45.6.789 is the IP of the server and "MyServer" is its name.
after that servers start to find each other even by name.

I suppose that this is a pretty trivial thing for someone who knows SQLServer well , but for me and my friends it was something new, so i hope it will help someone.

And off course a lot of credit goes to my genius boyfriend Adlai Mashiach whom many of you know and love. Without his help i wouldn't solve this. Don't forget to come to his lecture on LINQ the day after tomorrow. I've seen the lecture - it's really good!